private static Appender buildAppender(String name, String fileName, String pattern) { DailyRollingFileAppender appender = new DailyRollingFileAppender(); appender.setName(name); appender.setAppend(true); appender.setEncoding("GBK"); appender.setLayout(new PatternLayout(pattern)); appender.setFile(new File(getLogPath(), fileName).getAbsolutePath()); appender.activateOptions(); // 很重要,否则原有日志内容会被清空 return appender; }
public static Logger getLogger(String file) { Logger log = logCache.get(file); if (null != log) { return log; } else { log = Logger.getLogger(file); DailyRollingFileAppender appender = new DailyRollingFileAppender(); Layout layout = new PatternLayout(); appender.setFile(file); appender.setEncoding("UTF-8"); appender.activateOptions(); appender.setDatePattern("'.'yyyy-MM-dd"); appender.setLayout(layout); log.addAppender(appender); logCache.put(file, log); } return log; }