关闭

Log4j配置方式

标签: log4jXML配置log4j方式properties配置log4j方式
593人阅读 评论(0) 收藏 举报
分类:


# ${webapp.root} : 日志文件存储到项目目录下 ${catalina.home} : 日志文件存储到Tomcat目录下


XML配置方式1

配置maxBackupIndex最大记录日志文件数为20,每个日志文件大小为10MB。将不同级别的日志信息保存到不同文件中:debug.log、info.log、warn.log、error.log等文件。

当日志文件数量达到20个之后,此种配置方式会覆盖重写第一个日志文件,将新的日志信息写到第一个日志文件,第一个写满后,重写第二个,按顺序循环进行。

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
	<!-- 日志记录的优先级,由高到低为OFF, FITAL(致命), ERROR(错误), WARN(警告), INFO(信息), DEBUG(调试), TRACE(跟踪), ALL(全部)。 -->
	
	<!-- 控制台标准输出日志 -->
	<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	<!-- traceLog4Debug日志 -->
	<appender name="traceLog4Debug" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/traceLog4Debug.log"/>
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	<!-- <appender name="allLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/all.log"/>
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="1MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="ALL" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender> -->

	<!-- trace跟踪日志 -->
	<appender name="traceLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/trace/trace.log"/>
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="TRACE" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMax" value="TRACE" />
			<param name="LevelMin" value="TRACE" />
		</filter>
	</appender>

	<!-- debug调试日志 -->
	<appender name="debugLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/debug/debug.log"/>
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="DEBUG" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
		<!-- filter配置LevelMax、LevelMin限制输出到文件的日志级别 -->
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMax" value="DEBUG" />
			<param name="LevelMin" value="DEBUG" />
		</filter>
	</appender>
	
	<!-- info信息日志 -->
	<appender name="infoLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/info/info.log"/>
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="INFO" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMax" value="INFO" />
			<param name="LevelMin" value="INFO" />
       	</filter>
	</appender>
	
	<!-- warn警告日志 -->
	<appender name="warnLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/warn/warn.log"/>
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="WARN" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="LevelMax" value="WARN" />
         	<param name="LevelMin" value="WARN" />
       	</filter>
	</appender>

	<!-- error错误日志 -->
	<appender name="errorLog" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${webapp.root}/logs1/error/error.log" />
		<param name="encoding" value="GBK" />
		<param name="maxFileSize" value="10MB" />
		<param name="maxBackupIndex" value="20" />
		<param name="threshold" value="ERROR" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
    	<filter class="org.apache.log4j.varia.LevelRangeFilter">
        	<param name="LevelMax" value="ERROR" />
        	<param name="LevelMin" value="ERROR" />
    	</filter>
	</appender>


	<!-- 记录该包下所有日志 -->
	<logger name="com.yd">
		<level value="ALL"/>
		<appender-ref ref="stdout" />
		<appender-ref ref="traceLog4Debug" />
		<!-- <appender-ref ref="allLog" /> -->
		<appender-ref ref="traceLog" />
		<appender-ref ref="debugLog" />
		
		<appender-ref ref="infoLog" />
		<appender-ref ref="warnLog" />
		<appender-ref ref="errorLog" />
	</logger>

	<logger name="org.hibernate.SQL">
		<level value="DEBUG" />
		<appender-ref ref="traceLog4Debug"/>
		<appender-ref ref="stdout" />
	</logger>
	
	<logger name="org.hibernate">
		<level value="error" />
		<appender-ref ref="traceLog4Debug"/>
		<appender-ref ref="stdout" />
	</logger>
	
	<logger name="org.springframework">
		<level value="error" />
		<appender-ref ref="traceLog4Debug" />
		<appender-ref ref="stdout" />
	</logger>
</log4j:configuration>


XML配置方式2

每天都会产生日志文件,用日期命名日志文件,日志文件没有大小和数量限制。

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	<!-- 控制台STDOUT -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="encoding" value="GBK" />
		<param name="target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	<!-- 公共Appender -->
	<appender name="DEFAULT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="${webapp.root}/logs/common-default-today.log" />
		<param name="Append" value="true" />
		<param name="encoding" value="GBK" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	 <!-- 错误日志APPENDER -->
	<appender name="ERROR-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="${webapp.root}/logs/common-error.log" />
		<param name="Append" value="true" />
		<param name="encoding" value="GBK" />
		<param name="threshold" value="error" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	<!-- 组件日志APPENDER -->
	<appender name="COMPONENT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="${webapp.root}/logs/logistics-component.log" />
		<param name="Append" value="true" />
		<param name="encoding" value="GBK" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />
		</layout>
	</appender>
	
	<!-- 组件日志 -->
	<logger name="LOGISTICS-COMPONENT">
		<level value="INFO" />
		<appender-ref ref="COMPONENT-APPENDER" />
		<appender-ref ref="ERROR-APPENDER" />
	</logger>
	
	<!-- Root Logger -->
	<root>
		<!-- 日志记录的优先级,由高到低为OFF, FITAL(致命), ERROR(错误), WARN(警告), INFO(信息), DEBUG(调试), TRACE(跟踪), ALL(全部)。 -->
		<level value="DEBUG" /> 
		<appender-ref ref="DEFAULT-APPENDER" />
		<appender-ref ref="ERROR-APPENDER" />
		<appender-ref ref="console" />
		<appender-ref ref="DEFAULT-APPENDER" />
	</root>
</log4j:configuration>


properties配置方式1

log4j.properties

log4j.rootLogger=info,A1,console,D


log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.A1.Threshold=info
log4j.appender.A1.Append=true 
log4j.appender.A1.File=${catalina.home}/system/logs/info/xymgr_info_log4j.log 
log4j.appender.A1.DatePattern = '.'yyyy-MM-dd'.log' 
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=[%-5p] %X{userid} %X{ip} %d{yyyy-MM-dd HH\:mm\:ss}  Method\: %l%n%m%n 


log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=info
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=---[%-5p] %X{userid} %X{ip} %d{yyyy-MM-dd HH\:mm\:ss} \: %m class\:[%c] %n


log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${catalina.home}/system/logs/error/xymgr_error_log4j.log 
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern =[%-5p] %-d{yyyy-MM-dd HH:mm:ss} %X{userid} %X{ip} [ %t:%r ] - [ %p ]  %m%n%l%n

log4j.logger.org.springframework=INFO
log4j.logger.com.wenyuan.sms=INFO

新新:http://blog.csdn.net/xinxin19881112/article/details/46833693

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:993471次
    • 积分:7513
    • 等级:
    • 排名:第2967名
    • 原创:113篇
    • 转载:35篇
    • 译文:0篇
    • 评论:118条
    最新评论