Logback 配置文件例子

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="logger" converterClass="com.lee.LoggingConverter" />
<property scope="context" name="varName" value="varValue" />

<if condition='!isDefined("DEV")'>
<!-- local dev env -->
<then>
<appender name="CON" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %-5level %logger{80} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- <prudent>true</prudent> -->
<file>\${user.home}/logs/hk_retail_out.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>\${user.home}/logs/hk_retail_out.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize> 
</triggeringPolicy>
<encoder>
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<appender name="PERFORMANCE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\${user.home}/logs/hk_retail_perform.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>\${user.home}/logs/hk_retail_perform.log.%i.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize> 
</triggeringPolicy>
<encoder>
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<appender name="METHOD" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\${user.home}/logs/hk_retail_method.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>\${user.home}/logs/hk_retail_method.log.%i.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize> 
</triggeringPolicy>
<encoder>
<!-- <pattern>%date [%X{flow}] %-5level %X{ip} %X{method} - %msg%n</pattern> -->
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<appender name="SQL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\${user.home}/logs/hk_retail_sql.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>\${user.home}/logs/hk_retail_sql.log.%i.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>1</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize> 
</triggeringPolicy>
<encoder>
<!-- <pattern>%date [%X{flow}] %-5level %X{ip} %X{method} - %msg%n</pattern> -->
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.lee" level="debug" />
<logger name="PerformanceCounter">
<appender-ref ref="PERFORMANCE"/>
</logger>
<logger name="MethodLogging" level="debug">
<appender-ref ref="METHOD"/>
</logger>
<logger name="org.lee.mapper" level="debug">
<appender-ref ref="SQL"/>
</logger>
<root level="info">
<appender-ref ref="CON" />
<appender-ref ref="FILE" />
</root>
</then>
<else>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\${user.dir}/logs/server1/hk_retail_out.%d{yyyy-MM-dd}${log.suffix}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>


<appender name="PERFORMANCE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\${user.dir}/logs/server1/hk_retail_perform.%d{yyyy-MM}${log.suffix}.log</fileNamePattern>
<maxHistory>6</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>


<appender name="METHOD" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\${user.dir}/logs/server1/hk_retail_method.%d{yyyy-MM}${log.suffix}.log</fileNamePattern>
<maxHistory>6</maxHistory>
</rollingPolicy>
<encoder>
<!-- <pattern>%date [%X{flow}] %-5level %X{ip} %X{method} - %msg%n</pattern> -->
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>


<appender name="SQL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\${user.dir}/logs/server1/hk_retail_sql.%d{yyyy-MM}${log.suffix}.log</fileNamePattern>
<maxHistory>6</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>


<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\${user.dir}/logs/server1/hk_retail_error.%d{yyyy-MM-dd}${log.suffix}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %-4relative [%X{flow}] %-5level %X{ip} %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<smtpHost>mailhost.jp.fid-intl.com</smtpHost>
<to>xxx@xxx.com</to>
<from>xxx@xxx.com</from>
<subject>[${API_ENV}][%-5level][HK-API${log.mail.subject.suffix}] %logger{80}</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger{80}.%M - %message%n</pattern>
</layout>
</appender>


<logger name="com.lee" level="debug" />

<logger name="PerformanceCounter" additivity="false">
<appender-ref ref="PERFORMANCE"/>
</logger>

<logger name="MethodLogging">
<appender-ref ref="METHOD"/>
</logger>


<logger name="com.lee.mapper" additivity="false">
<appender-ref ref="SQL"/>
</logger>


<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="EMAIL" />
<appender-ref ref="ERROR" />
</root>
</else>
</if>


</configuration>


public class LoggingConverter extends LoggerConverter {
protected String getFullyQualifiedName(ILoggingEvent event) {
String loggerText = MDC.get("method");


return ((StringUtils.isBlank(loggerText)) ? super
.getFullyQualifiedName(event) : loggerText);
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值