β

写个log4j.properties和logback.xml留着方便复制

lenglingx的个人页面 106 阅读

写个log4j.properties和logback.xml留着方便复制

log4j.properties

log4j.rootLogger = debug, Console, Debug, Info, Warn, Error
#log4j.additivity.org.apache=true

log4j.appender.Console = org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target = System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

log4j.appender.Debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Debug.File = /logs/debug.log
log4j.appender.Debug.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.Debug.Append = true
log4j.appender.Debug.Threshold = DEBUG
log4j.appender.Debug.layout = org.apache.log4j.PatternLayout
log4j.appender.Debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.appender.Debug.Encoding=UTF-8

log4j.appender.Info = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Info.File = /logs/info.log
log4j.appender.Info.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.Info.Append = true
log4j.appender.Info.Threshold = INFO
log4j.appender.Info.layout = org.apache.log4j.PatternLayout
log4j.appender.Info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.appender.Info.Encoding=UTF-8

log4j.appender.Warn = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Warn.File = /logs/warn.log
log4j.appender.Warn.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.Warn.Append = true
log4j.appender.Warn.Threshold = WARN
log4j.appender.Warn.layout = org.apache.log4j.PatternLayout
log4j.appender.Warn.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.appender.Warn.Encoding=UTF-8

log4j.appender.Error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Error.File = /logs/error.log
log4j.appender.Error.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.Error.Append = true
log4j.appender.Error.Threshold = ERROR
log4j.appender.Error.layout = org.apache.log4j.PatternLayout
log4j.appender.Error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.appender.Error.Encoding=UTF-8





log4j.logger.mainLog=debug, main_debug, main_info, main_warn, main_error
log4j.additivity.main = false

log4j.appender.main_debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.main_debug.Threshold=debug
log4j.appender.main_debug.Append=true
log4j.appender.main_debug.File=/logs/main_debug.log
log4j.appender.main_debug.layout=org.apache.log4j.PatternLayout
log4j.appender.main_debug.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p <%c> - [%m]%n

log4j.appender.main_info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.main_info.Threshold=info
log4j.appender.main_info.Append=true
log4j.appender.main_info.File=/logs/main_info.log
log4j.appender.main_info.layout=org.apache.log4j.PatternLayout
log4j.appender.main_info.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p <%c> - [%m]%n

log4j.appender.main_warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.main_warn.Threshold=warn
log4j.appender.main_warn.Append=true
log4j.appender.main_warn.File=/logs/main_warn.log
log4j.appender.main_warn.layout=org.apache.log4j.PatternLayout
log4j.appender.main_warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p <%c> - [%m]%n

log4j.appender.main_error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.main_error.Threshold=error
log4j.appender.main_error.Append=true
log4j.appender.main_error.File=/logs/main_error.log
log4j.appender.main_error.layout=org.apache.log4j.PatternLayout
log4j.appender.main_error.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p <%c> - [%m]%n

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
	<property name="LOG_HOME" value="/usr/local/log/testlog" />
	<!-- 控制台输出 -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<!-- 日志输出编码 -->
		<Encoding>UTF-8</Encoding>
		<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} %thread %-5level %X{invokeNo} %logger
				%msg%n</pattern>
		</encoder>
	</appender>

	<!-- 按照每天生成日志文件 (debug) -->
	<appender name="debugAppender"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Encoding>UTF-8</Encoding>
		<file>${LOG_HOME}/debug.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/testlog-debug-%d{yyyy-MM-dd}-%i.log
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<!-- 按时间回滚的同时,按文件大小来回滚 -->
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder><!-- 必须指定,否则不会往文件输出内容 -->
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<!-- 按照每天生成日志文件 (error) -->
	<appender name="errorAppender"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Encoding>UTF-8</Encoding>
		<file>${LOG_HOME}/error.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!--日志文件输出的文件名 -->
			<FileNamePattern>${LOG_HOME}/testlog-error-%d{yyyy-MM-dd}-%i.log
			</FileNamePattern>
			<maxHistory>7</maxHistory>
			<!-- 按时间回滚的同时,按文件大小来回滚 -->
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<!-- 按照每天生成日志文件 (info) -->
	<appender name="infoAppender"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Encoding>UTF-8</Encoding>
		<file>${LOG_HOME}/info.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/testlog-info-%d{yyyy-MM-dd}-%i.log
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<!-- 按时间回滚的同时,按文件大小来回滚 -->
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder><!-- 必须指定,否则不会往文件输出内容 -->
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>
	<!-- 按照每天生成日志文件 (WARN) -->
	<appender name="warnAppender"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Encoding>UTF-8</Encoding>
		<file>${LOG_HOME}/warn.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/testlog-warn-%d{yyyy-MM-dd}-%i.log
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<!-- 按时间回滚的同时,按文件大小来回滚 -->
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder><!-- 必须指定,否则不会往文件输出内容 -->
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
			<level>WARN</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>

	<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">  
		<smtpHost>smtp.163.com</smtpHost>  
        <smtpPort>25</smtpPort>  
        <username>testlog@163.com</username>  
        <password>testlogpasswd</password>  
        <SSL>false</SSL>  
        <to>123456@qq.com</to>
        <to>123456@sina.com</to>
        <from>testlog@163.com</from>  
        <subject>【Error】: %logger</subject>  
        <layout class="ch.qos.logback.classic.html.HTMLLayout" >  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
        </layout>  
	</appender> 
	
	<!-- 日志输出级别 -->
	<root level="INFO">
		<appender-ref ref="EMAIL" />
		<appender-ref ref="STDOUT" />
		<appender-ref ref="errorAppender" />
		<appender-ref ref="infoAppender" />
		<appender-ref ref="warnAppender" />
		<appender-ref ref="debugAppender" />
	</root>

	
</configuration>
作者:lenglingx的个人页面
独钓渔的博客
原文地址:写个log4j.properties和logback.xml留着方便复制, 感谢原作者分享。

发表评论