我是 log4j 的新手。我创建了一个在其中实现 log4j 的示例 java 程序。
下面是java程序:
package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
/**
* @param args the command line arguments
*/
private static Logger logger = Logger.getLogger(Logging.class);
public static void main(String[] args) {
BasicConfigurator.configure();
logger.trace("This is a Trace");
logger.debug("This is a Debug");
logger.info("This is an Info");
logger.warn("This is a Warn");
logger.error("This is an Error");
logger.fatal("This is a Fatal");
}
}
我在控制台屏幕上得到输出。但是 没有生成日志文件。我还使用以下链接在 Eclipse neon 中配置了我的项目: Configuration
我已经做好了一切。但是日志文件没有生成。当我以编程方式实现 log4j 时,文件正在生成。以下是我的 属性文件:
#root
log4j.logger.com.apress.logging.log4j=debug,dest
log4j.additivity.com.apress.logging.log4j=false
#define the appender
log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender
#set the name of the file
log4j.appender.dest.File=${user.home}/log.out
#setting the immediate flush to true (default)
log4j.appender.dest.ImmediateFlush=true
#setting the threshold
log4j.appender.dest.Threshold=ERROR
#setting the append to false, overwrite
log4j.appender.dest.Append=true
#set the DatePattern
log4j.appender.dest.DatePattern='.' yyyy-MM-dd
我需要做什么才能让 Log4J 写入日志文件?
原文由 Hema Chandra 发布,翻译遵循 CC BY-SA 4.0 许可协议
确保 log4j.properties 在默认包中