Log4J动态创建日志文件 [英] Log4J dynamically create log file
问题描述
我已经在每个进程运行时成功创建了一个日志.我现在遇到的问题是,所调用的不是当前运行进程的子级的任何程序包都不会将其日志写入我的文件.例如,我创建了一个名为running-.log的新日志文件.正在运行的进程是此类中的com.me.foo,在com.you中有一个方法的调用,在com.zee中有一个方法的调用.我想将com.you和com.zee日志写入运行-.log而不是控制台日志.这并不只是将getLogger()方法更改为com.me.foo的子项那样简单.一些日志是从第三方jars中写出来的.我很茫然.如果您需要查看更多代码或其他信息,请告诉我.必须有另一种方式来处理此问题.
I have successfully created a log at each process run time. The issue I am having now is any packages that are called that are not children of the current running process does not write its log to my file. For example I create a new log file called running-.log. The process that is running is com.me.foo inside of this class there is a call to a method in com.you and another one in com.zee . I would like to have com.you and com.zee logs write to the running-.log and not to the console log. It isn't as simple as just changing the getLogger() method to be a child of com.me.foo. Some of the logs are written out from third party jars. I am at a loss. If you need to see more code or some additional info, please let me know. There has to be another way to handle this.
谢谢
用于动态创建日志文件的代码
Code to create the log file dynamically
public void createLogInstance(String packaging,String appenderName, String logFileName){
Logger logger = Logger.getLogger(packaging);
Appender fileAppender = logger.getAppender(appenderName);
if(fileAppender != null){
logger.removeAppender(fileAppender);
}
//Create the root appender
ConsoleAppender console = new ConsoleAppender();
String pattern = ....;
console.setLayout(new PatternLayout(pattern));
console.setThreshold(Level.FATAL);
console.activateOptions();
logger.addAppender(console);
FileAppender fa = new FileAppender();
fa.setName(appenderName);
fa.setFile(logFileName);
fa.setLayout(new PatternLayout(..));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
logger.setAdditivity(false);
logger.addAppender(fa);
}
com.zee日志
private static Logger logger = LoggerFactory.getLogger(Zee.class);
com.you登录
private static Logger logger = LoggerFactory.getLogger(You.class);
推荐答案
我缺少rootLogger.更改了这一行
I was missing the rootLogger. Changed this line
Logger logger = Logger.getLogger(packaging);
到
Logger logger = Logger.getRootLogger();
如果有人有更好的方法,请告诉我.
If someone has a better way please let me know.
这篇关于Log4J动态创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!