How to make log4j record to a file and print to console
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.PatternL开发者_StackOverflow中文版ayout
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
The problem is that your single \ should be \\. This is true in most properties files.
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.
I would have added: log4j.appender.LOG.Threshold=ALL
I'm not sure what the default is.
精彩评论