How to make log4j record to a file and print to console

徘徊边缘 提交于 2019-12-24 01:27:45

问题


I can make the log to go the console but I cant seem to make it go to a log file. Here is my properties file.

log4j.rootLogger=DEBUG, LOG , stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO

log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=C:\dev\harry\data\logs\core.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.Append=true
log4j.appender.LOG.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO

回答1:


The problem is that your single \ should be \\. This is true in most properties files.




回答2:


There are some issues with the \ in the file path that need to be fixed. The syntax is a bit cryptic but the way to log to multiple locations is to attach a named logging appender to the root logger. In this example this is:

log4j.rootLogger=DEBUG, LOG , stdout

DEBUG is a logging level (threshold filter) to use for the root logger.

LOG is the name of a logging appender

stdout is the name of a second appender

The logging appenders are specified by

log4j.appender.{logging-appender-name}={some.log4j.appender.class}
log4j.appender.{logging-appender-name}.{some-other-property}=...

Where {logging-appender-name} is a name selected by yourself (in this case LOG and stdout) and {some.log4j.appender.class} is one of the many log4j logging appender classes such as DailyRollingFileAppender or ConsoleAppender.




回答3:


I would have added: log4j.appender.LOG.Threshold=ALL

I'm not sure what the default is.



来源:https://stackoverflow.com/questions/5942478/how-to-make-log4j-record-to-a-file-and-print-to-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!