The log4j jar package contains the preference default configuration, and the user can specify their own configuration by Log4j.xml or log4j.properties. XML is preferred over properties. Note also that Java reads the properties file only by means of the = sign to split the key-value pairs, so the # comment cannot be placed on the same line as the key-value pair definition.
The configuration files are specified in the following ways:
The 1.log4j.jar package will use system first. GetProperty ("log4j. XML or Configuration") to see if there are JVM parameters, we can use Java -dlog4j.configuration=file:/full_path/ Log4j.properties to specify (file: The prefix cannot be saved).
2. Use propertyconfigurator.configure (param) in the code; to specify. The more confusing may be the problem of relative paths, which can be obtained in the IDE or JAR package via getClassLoader (). GetResource ("Log4j.properties"); to obtain a resource file that exists in the classpath.
Multiple jar packages with one Log4j.jar can be implemented with different output levels for different jar packages.
If you set Rootlogger to the info level, set your own package to the debug level:
log4j.logger.包名=DEBUG
You can also specify that each jar package output to a different file:
Log4j.logger. Package name 1=debug,jar1logger
Log4j.appender.jar1logger=org.apache.log4j.rollingfileappender
Log4j.appender.jar1logger.layout=org.apache.log4j.patternlayout
log4j.appender.jar1logger.layout.conversionpattern=%d [%t]<%-5p>%c--%m%n
Log4j.appender.jar1logger.file=/var/log/mylog/jar1.log
Log4j.appender.jar1logger.append=true
log4j.appender.jar1logger.maxfilesize=1024kb
Log4j.appender.jar1logger.maxbackupindex=5
The following is the simplest configuration:
# # # Set log levels, Stdout,d,e is his own name # # # #设置rootLogger级别为INFO则屏蔽debug信息, level all<debug <Info< Warn < error < fatal<Offlog4j.rootlogger=INFO, stdout### output to console # # #log4j. Appender.stdout=Org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern= [Fyk]%d{absolute}%p [%c{1}.%m]:%l-%m%n### output to log file # # # #log4j. APPENDER.D=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.d.file= logs/All.loglog4j.appender.d.append=true# # Output Debug level above the log log4j.appender.d.threshold=Debuglog4j.appender.d.layout=Org.apache.log4j.patternlayoutlog4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-DDHH:MM:SS} [%t:%r][%c{1}.%M]-[%p]%m%n# #ERRORlog4j. APPENDER.E=Org.apache.log4j.dailyrollingfileappenderlog4j.appender.e.file=logs/Error.loglog4j.appender.e.append=trueLog4j.appender.e.threshold=Errorlog4j.appender.e.layout=Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern=%-d{yyyy-mm-DDHH:MM:SS} [%t:%r][%c{1}.%M]-[%p]%m%n
log4j configuration file Loading