简体   繁体   中英

How to get logger information by using Log4j in java

In my application we are using different log files. But I was unable to find the way the log messages are going to different log files. I know we can specify the packages in log4j.properties by using category. Is there any way to specify the logger to go to some specific file and to get some information about logger means the file in which the log messages are rendering.

Here's a quick example, how to log different messages (errors and events) in different files:

log4j.logger.EventLogger=debug,events
log4j.logger.ErrorLogger=debug,errors

log4j.appender.events=org.apache.log4j.FileAppender
log4j.appender.events.File=events.log
log4j.appender.events.append=false
log4j.appender.events.layout=org.apache.log4j.PatternLayout
log4j.appender.events.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %F:%L - %m%n

log4j.appender.errors=org.apache.log4j.FileAppender
log4j.appender.errors.File=errors.log
log4j.appender.errors.append=false
log4j.appender.errors.layout=org.apache.log4j.PatternLayout
log4j.appender.errors.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %F:%L - %m%n

Now you only have to create two loggers:

Logger eventLogger = Logger.getLogger("EventLogger");
Logger errorLogger = Logger.getLogger("ErrorLogger");

Regarding your second question - yes, you can get information about the loggers at runtime:

Finding Log4J log file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM