In this post, I'll discuss how you can create a dedicated appender that can be used to generate the separate/second log file independent of the root logger.
1) The first thing you need to do is, define a custom logger configuration which we'll be using later (promise, I'll show how?). Below is the sample configuration:
(this should go to your log4j.properties file)
log4j.logger.myLogger=DEBUG, mylogger
log4j.appender.mylogger=org.apache.log4j.RollingFileAppender
log4j.appender.mylogger.maxFileSize=5000KB
log4j.appender.mylogger.layout=org.apache.log4j.PatternLayout
log4j.appender.mylogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c : %m%n
log4j.appender.mylogger.File=c:/seconf_file.log
Note the first line, we have given a name (myLogger) to our logger and we'll be using this name in our code to get an instance of our custom logger (mylogger).
2) Second, to use this appender you have to get an instance of it. Below is the sample code which shows how to do it (I completed my promise):
private or public (whatever you want)
private static final Logger log = Logger.getLogger("myLogger");
with the "log" instance you can send your log statements to a selected logger (in this case mylogger, which will go to seconf_file.log)
Similarly, you can define your own loggers and use them as and when needed.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.