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

2019-07-20 11:42发布

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

标签: java log4j
3条回答
该账号已被封号
2楼-- · 2019-07-20 12:09

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

I'm not sure what the default is.

查看更多
爷、活的狠高调
3楼-- · 2019-07-20 12:13

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

查看更多
Evening l夕情丶
4楼-- · 2019-07-20 12:31

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.

查看更多
登录 后发表回答