在添加log4j到我的应用程序后,每次执行我的应用程序时,我都会得到以下输出:
log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.
这似乎意味着一个配置文件丢失了。
这个配置文件应该放在哪里,一个好的开始内容是什么?
我正在使用纯java开发桌面应用程序。所以没有网络服务器等等…
另一种不将属性文件放在类路径上的方法是直接从java代码中设置属性。下面是示例代码。
public class Log4JSample {
public static void main(String[] args) {
Properties properties=new Properties();
properties.setProperty("log4j.rootLogger","TRACE,stdout,MyFile");
properties.setProperty("log4j.rootCategory","TRACE");
properties.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
properties.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
properties.setProperty("log4j.appender.stdout.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");
properties.setProperty("log4j.appender.MyFile", "org.apache.log4j.RollingFileAppender");
properties.setProperty("log4j.appender.MyFile.File", "my_example.log");
properties.setProperty("log4j.appender.MyFile.MaxFileSize", "100KB");
properties.setProperty("log4j.appender.MyFile.MaxBackupIndex", "1");
properties.setProperty("log4j.appender.MyFile.layout", "org.apache.log4j.PatternLayout");
properties.setProperty("log4j.appender.MyFile.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");
PropertyConfigurator.configure(properties);
Logger logger = Logger.getLogger("MyFile");
logger.fatal("This is a FATAL message.");
logger.error("This is an ERROR message.");
logger.warn("This is a WARN message.");
logger.info("This is an INFO message.");
logger.debug("This is a DEBUG message.");
logger.trace("This is a TRACE message.");
}
}