From: http://dingjun1.javaeye.com/blog/413993
Reprinted:
Http://wangjc-opal.javaeye.com/blog/309924
There is no doubt about the powerful functions of log4j, but in actual applications, it is inevitable that an independent log file needs to be output for a function. How can we separate the required content from the original log, what about creating a separate log file? In fact, you can easily implement this function with a slight configuration based on the existing log4j.
First look at a common log4j. properties file, which records logs in the console and myweb. log file:
Log4j. rootlogger = debug, stdout, logfile
Log4j.category.org. springframework = Error
Log4j.category.org. Apache = info
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % d % P [% C]-% m % N
Log4j. appender. logfile = org. Apache. log4j. rollingfileappender
Log4j. appender. logfile. File =$ {myweb. Root}/WEB-INF/log/myweb. Log
Log4j. appender. logfile. maxfilesize = 512kb
Log4j. appender. logfile. maxbackupindex = 5
Log4j. appender. logfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. logfile. layout. conversionpattern = % d % P [% C]-% m % N
If you want to output different files for different classes (using cn.com. Test as an example), you must first define in test. Java:
Private Static log logger = logfactory. getlog (test. Class );
Then add the following to log4j. properties:
Log4j.logger.cn.com. test = debug, test
Log4j. appender. test = org. Apache. log4j. fileappender
Log4j. appender. Test. File =$ {myweb. Root}/WEB-INF/log/test. Log
Log4j. appender. Test. layout = org. Apache. log4j. patternlayout
Log4j. appender. Test. layout. conversionpattern = % d % P [% C]-% m % N
That is, let logger in cn.com. test use the configuration made by log4j. appender. Test.
However, what if multiple log files need to be output in the same category? In fact, the truth is the same. First, we define it in test. Java:
Private Static log logger1 = logfactory. getlog ("mytest1 ");
Private Static log logger2 = logfactory. getlog ("mytest2 ");
Then add the following to log4j. properties:
Log4j. Logger. mytest1 = debug, test1
Log4j. appender. test1 = org. Apache. log4j. fileappender
Log4j. appender. test1.file =$ {myweb. Root}/WEB-INF/log/test1.log
Log4j. appender. test1.layout = org. Apache. log4j. patternlayout
Log4j. appender. test1.layout. conversionpattern = % d % P [% C]-% m % N
Log4j. Logger. mytest2 = debug, Test2
Log4j. appender. Test2 = org. Apache. log4j. fileappender
Log4j. appender. test2.file =$ {myweb. Root}/WEB-INF/log/test2.log
Log4j. appender. test2.layout = org. Apache. log4j. patternlayout
Log4j. appender. test2.layout. conversionpattern = % d % P [% C]-% m % N
That is, when logger is used, give it a custom name (for example, "mytest1"), and then configure it in log4j. properties. Do not forget to use different logger for different logs (for example, logger1.info ("ABC") for output to test1.log ")).
Another problem is that these custom logs are output to the logs configured by log4j. rootlogger at the same time by default. How can they only be output to the specified logs? Don't worry. Here is a switch:
Log4j. additi.pdf. mytest1 = false
It is used to set whether to output data to the log configured by log4j. rootlogger at the same time. If it is set to false, it will not be output to other places! Note that "mytest1" here is the custom name you gave logger in the program!