1. 程式人生 > >log4j.properties,log4j.xml配置檔案的配置示例

log4j.properties,log4j.xml配置檔案的配置示例

log4j.properties日誌檔案的配置一般放在src源目錄下就可以了,下面是具體的配置示例:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout, file

根據個人習慣,有的人喜歡用log4j.xml的配置形式:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	<appender name="appender1" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="${catalina.home}/logs/mylog1.log" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p [%t - %F:%L] %m%n" />
		</layout>
	</appender>
       <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %t %5p [%c:%M:%L] - %m%n"/>
        </layout>
    </appender>
	<appender name="appender2" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="${catalina.home}/logs/mylog2.log" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p [%t - %F:%L] %m%n" />
		</layout>
	</appender>

	<logger name="fly.sun.service.demo" additivity="false">
		<level value="ERROR" />
		<appender-ref ref="appender1" />
	</logger>
	<logger name="fly.sun.dao" additivity="false">
		<level value="ERROR" />
		<appender-ref ref="appender1"/>
	</logger>
	<root>
		<level value="ERROR" />
		<appender-ref ref="appender2" />
	</root>
</log4j:configuration>