A typical requirement in the project is to log different modules in different log files. For instance in this example we have two modules one for the admin and other for reports.
在项目中有一个典型的需求,就是在不同的日志文件里记录不同的模块。例如在本次案例中,我们有两个模块,一个是管理员,另一个是报告。
To do this we create two seperate
FileAppenders and associate them with each package.
为了做这些,我们可以创建两个分开的FileAppenders,并且让他们彼此关联。
Here instead of associating the appenders to the
rootLogger we associate it with different packages. The
AdminFileAppender is linked with the admin package and the
ReportFileAppender is linked with the report package. This means all the log entries in the admin module will be logged in the
admin.log file and all the log entries in the report module will be logged in the
report.log file. You can also set different logger levels for each package, here for the admin package it is set to
WARN and for the report package it is set to
DEBUG. The
rootLogger will be by default set to the logger level
DEBUG, so this statement "
log4j.rootLogger=DEBUG" is there only for your understanding purpose and it is not necessary.
在这里,我们不把appenders关联rootLogger,而是让它关联不同的包。AdminFileAppender与admin包关联,ReportFileAppender与report包关联。这里的意思是在admin模块中的所有日志记录将记录在admin.log文件内,而在report模块中的所有日志记录将记录在report.log文件内。你也可以为每个包设置不同的日志等级,这里我们给admin包设置为WARN,report包设置成DEBUG。rootLogger采用默认的设置DEBUG,所以这里的语句"log4j.rootLogger=DEBUG"只是为了让你理解的目的,并不是必要的。
In this example we have two classes
SampleAdmin class in the
admin package and
SampleReport class in the
report package.
在这个例子中,我们有个两个类 SampleAdmin在admin包中以及 SampleReport 在report包中。
package com.vaannila.admin; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import com.vaannila.report.SampleReport; public class SampleAdmin { static Logger logger = Logger.getLogger(SampleAdmin.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Sample debug message"); logger.info("Sample info message"); logger.warn("Sample warn message"); logger.error("Sample error message"); logger.fatal("Sample fatal message"); SampleReport obj = new SampleReport(); obj.generateReport(); } }
After executing the program, the contents of admin.log file.package com.vaannila.report; import org.apache.log4j.Logger; public class SampleReport { static Logger logger = Logger.getLogger(SampleReport.class); public void generateReport(){ logger.debug("Sample debug message"); logger.info("Sample info message"); logger.warn("Sample warn message"); logger.error("Sample error message"); logger.fatal("Sample fatal message"); } }
执行完这个程序之后,admin.log文件内的内容。
0 [main] WARN com.vaannila.admin.SampleAdmin - Sample warn message
0 [main] ERROR com.vaannila.admin.SampleAdmin - Sample error message
0 [main] FATAL com.vaannila.admin.SampleAdmin - Sample fatal message
The contents of
report.log file.
report.log文件内的内容。
0 [main] DEBUG com.vaannila.report.SampleReport - Sample debug message
0 [main] INFO com.vaannila.report.SampleReport - Sample info message
0 [main] WARN com.vaannila.report.SampleReport - Sample warn message
0 [main] ERROR com.vaannila.report.SampleReport - Sample error message
0 [main] FATAL com.vaannila.report.SampleReport - Sample fatal message
It's not necessary to apply the logger level to each package, by default the
rootLogger level will be inherited.
如果不需要申明日志等级,那么会默认集成rootLogger的等级。
log4j.logger.com.vaannila.admin=,AdminFileAppender
log4j.logger.com.vaannila.report=,ReportFileAppender
Insted of applying a logger level, the field is left blank so the
rootLogger level ("
DEBUG") will be inheritted.
左边设置为空白字符,那么就继承了rootLogger等级"DEBUG"
If you associate a
ConsoleAppender to the
rootLogger then all the log entries in both the
admin and
report packages will be log on the console.
如果你将ConsoleAppender关联到rootLogger,那么所有的包括admin和report的日志记录都会在控制台上打印。
log4j.rootLogger=DEBUG, CA
# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# ReportFileAppender - used to log messages in the report.log file.
log4j.appender.ReportFileAppender=org.apache.log4j.FileAppender
log4j.appender.ReportFileAppender.File=report.log
log4j.appender.ReportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ReportFileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.vaannila.admin=WARN,AdminFileAppender
log4j.logger.com.vaannila.report=DEBUG,ReportFileAppender
On executing the example you will see the following output on the console.
在执行此案例的时候,你将会看到控制上输出以下内容。
0 [main] WARN com.vaannila.admin.SampleAdmin - Sample warn message 0 [main] ERROR com.vaannila.admin.SampleAdmin - Sample error message 0 [main] FATAL com.vaannila.admin.SampleAdmin - Sample fatal message 0 [main] DEBUG com.vaannila.report.SampleReport - Sample debug message 0 [main] INFO com.vaannila.report.SampleReport - Sample info message 0 [main] WARN com.vaannila.report.SampleReport - Sample warn message 0 [main] ERROR com.vaannila.report.SampleReport - Sample error message 16 [main] FATAL com.vaannila.report.SampleReport - Sample fatal message
如果你想记录把所有包的日志那么添加一个类似的appender给rootLogger,如果你想把一个appender关联一个特殊的包那么你就提供一个独立的给它。如果你在两个地方都设置了,那么日志记录就会发生重复.