动态部署Log4j

1. 实例化部署

PatternLayout layout = new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss}][%p][%l] - %m%n");
ConsoleAppender consoleAppender=new ConsoleAppender(layout);
consoleAppender.setTarget("System.out");

DailyRollingFileAppender dailyRollingFileAppender=new DailyRollingFileAppender(layout, "./logs", "'.'yyyy-MM-dd'.log'");
dailyRollingFileAppender.setName("DailyRollingFile");
dailyRollingFileAppender.setAppend(true);

Logger logger=Logger.getLogger(cls.getName());
logger.addAppender(consoleAppender);
logger.addAppender(dailyRollingFileAppender);

logger.debug("ok");

 

2.指定配置文件部署

PropertyConfigurator.configure(".\\src\\main\\conf\\log4j.properties");
Logger logger=Logger.getLogger(cls.getName());
logger.debug("ok");

 

你可能感兴趣的:(log4j,动态部署log4j)