码迷,mamicode.com
首页 > 其他好文 > 详细

logback + slf4j 学习

时间:2015-08-12 19:14:00      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

1:在web.xml文件配置Listener

<context-param>

<param-name>logbackConfigLocation</param-name>
<param-value>classpath:logback.xml</param-value> <!-- classpath是指 WEB-INF文件夹下的classes目录 -->
</context-param>
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>

2:logback.xml文件内容

<configuration>
    <property name="LoggingDir" value="logs/" />
    <property name="ROOT_LEVEL" value="TRACE" />
    <timestamp key="byDate" datePattern="yyyyMMdd"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{ HH:mm:ss.SSS} %-5level %marker %logger - %msg%n</Pattern>
        </layout>
    </appender>
    <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <file>${LoggingDir}debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LoggingDir}history/debug.%d{yyyy-MM-dd}.zip</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} level:%level %marker thread:%thread logger:%logger message:%msg%n
            </pattern>
        </encoder>
    </appender>
 
	 <appender name="USER" class="ch.qos.logback.core.rolling.RollingFileAppender">
	        <append>true</append>
	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	            <level>INFO</level>
	        </filter>
	        <file>${LoggingDir}user.log</file>
	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	            <fileNamePattern>${LoggingDir}history/user.%d{yyyy-MM-dd}.zip</fileNamePattern>
	            <maxHistory>30</maxHistory>
	        </rollingPolicy>
	        <encoder>
	            <pattern>%d{HH:mm:ss.SSS} %level %marker thread:%thread logger:%logger message:%msg%n
	            </pattern>
	        </encoder>
	 </appender>
 
 	<appender name="OPEARTION" class="ch.qos.logback.core.rolling.RollingFileAppender">
	        <append>true</append>
	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	            <level>INFO</level>
	        </filter>
	        <file>${LoggingDir}opeartion.log</file>
	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	            <fileNamePattern>${LoggingDir}history/opeartion.%d{yyyy-MM-dd}.zip</fileNamePattern>
	            <maxHistory>30</maxHistory>
	        </rollingPolicy>
	        <encoder>
	            <pattern>%d{HH:mm:ss.SSS} %level %marker thread:%thread logger:%logger message:%msg%n
	            </pattern>
	        </encoder>
	 </appender>
	 
	<appender name="WECHAT" class="ch.qos.logback.core.rolling.RollingFileAppender">
	        <append>true</append>
	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	            <level>INFO</level>
	        </filter>
	        <file>${LoggingDir}wechat.log</file>
	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	            <fileNamePattern>${LoggingDir}history/wechat.%d{yyyy-MM-dd}.zip</fileNamePattern>
	            <maxHistory>30</maxHistory>
	        </rollingPolicy>
	        <encoder>
	            <pattern>%d{HH:mm:ss.SSS} %level %marker thread:%thread logger:%logger message:%msg%n
	            </pattern>
	        </encoder>
	 </appender>
 
 	<appender name="INTERFACE" class="ch.qos.logback.core.rolling.RollingFileAppender">
	        <append>true</append>
	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	            <level>INFO</level>
	        </filter>
	        <file>${LoggingDir}interface.log</file>
	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	            <fileNamePattern>${LoggingDir}history/interface.%d{yyyy-MM-dd}.zip</fileNamePattern>
	            <maxHistory>30</maxHistory>
	        </rollingPolicy>
	        <encoder>
	            <pattern>%d{HH:mm:ss.SSS} %level %marker thread:%thread logger:%logger message:%msg%n
	            </pattern>
	        </encoder>
	 </appender>
	 
	 <appender name="BUSINESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
	        <append>true</append>
	        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
	            <level>INFO</level>
	        </filter>
	        <file>${LoggingDir}business.log</file>
	        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	            <fileNamePattern>${LoggingDir}history/business.%d{yyyy-MM-dd}.zip</fileNamePattern>
	            <maxHistory>30</maxHistory>
	        </rollingPolicy>
	        <encoder>
	            <pattern>%d{HH:mm:ss.SSS} %level %marker thread:%thread logger:%logger message:%msg%n
	            </pattern>
	        </encoder>
	 </appender>
 
 	<appender name="ASYNC_STDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT"/>
    </appender>
    
    <appender name="ASYNC_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="DEBUG"/>
    </appender>
 
 	<appender name="ASYNC_USER" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="USER"/>
    </appender>
 
 	<appender name="ASYNC_OPEARTION" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="OPEARTION"/>
    </appender>
    
    <appender name="ASYNC_WECHAT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="WECHAT"/>
    </appender>
    
    <appender name="ASYNC_INTERFACE" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="INTERFACE"/>
    </appender>
 
    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>org.domain.library</Marker>
        <OnMatch>ACCEPT</OnMatch>
    </turboFilter>
    <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
        <Marker>org.apache.http</Marker>
        <OnMatch>DENY</OnMatch>
    </turboFilter>
    <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
        <MDCKey>username</MDCKey>
        <Value>sebastien</Value>
        <OnMatch>ACCEPT</OnMatch>
    </turboFilter>
 
 
    <!-- This file appender is used to output aggregated performance statistics -->
    <appender name="perf4jFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LoggingDir}perf4j.log</File>
        <encoder>
            <Pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n</Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LoggingDir}perf4j.%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>
    </appender>
 
    <logger name="org.perf4j.TimingLogger" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="perf4jFileAppender"/>
    </logger>
 
    <logger name="org.springframework" level="INFO">
        <appender-ref ref="ASYNC_STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </logger>
 
    <logger name="com.amazonaws" level="INFO">
        <appender-ref ref="ASYNC_STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </logger>
 
    <logger name="org.apache.http" level="INFO">
        <appender-ref ref="ASYNC_STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </logger>
 
    <logger name="org.apache.commons.configuration" level="INFO">
        <appender-ref ref="ASYNC_STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </logger>

    <logger name="freemarker" level="INFO">
        <appender-ref ref="ASYNC_STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </logger>
 
 	<logger name="debugLogger" level="INFO">
 		<appender-ref ref="ASYNC_DEBUG"/>
 	</logger>
 
 	<logger name="userLogger" level="INFO">
 		<appender-ref ref="ASYNC_USER"/>
 	</logger>
 	
 	<logger name="opeartionLogger" level="INFO">
 		<appender-ref ref="ASYNC_OPEARTION"/>
 	</logger>
 	
 	<logger name="wechatLogger" level="INFO">
 		<appender-ref ref="ASYNC_WECHAT"/>
 	</logger>
 
 	<logger name="interfaceLogger" level="INFO">
 		<appender-ref ref="ASYNC_INTERFACE"/>
 	</logger>
 	
 	<logger name="businessLogger" level="INFO">
 		<appender-ref ref="BUSINESS"/>
 	</logger>
 	
    <root level="${ROOT_LEVEL}">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="ASYNC_DEBUG"/>
    </root>
    
</configuration>

  3:java代码

package com.guoxinet.o2o.core.log;

/**
 * @ClassName: LogFactory
 * @Description: 定义日志类型
 * @author wanghuajin@guoxinet.com
 * @date 2015-6-20 下午2:23:34
 *
 */
public interface LogType {
	final String DEBUG ="debugLogger.";
	final String USER ="userLogger.";
	final String OPEARTION ="opeartionLogger.";
	final String WECHAT ="wechatLogger.";
	final String INTERFACE ="interfaceLogger.";
	final String BUSINESS ="businessLogger.";
}

  

package com.guoxinet.o2o.core.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: LogFactory
 * @Description: 对日志进行业务划分
 * @author wanghuajin@guoxinet.com
 * @date 2015-6-20 下午2:23:34
 *
 */
public class LogFactory implements LogType{
	
	/**
	 * 获取调试日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getDebugLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(DEBUG).append(clz.getName()).toString());
	}
	
	/**
	 * 获取用户日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getUserLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(USER).append(clz.getName()).toString());
	}
	
	/**
	 * 获取操作日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getOpeartionLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(OPEARTION).append(clz.getName()).toString());
	}
	
	/**
	 * 获取微信日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getWechatLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(WECHAT).append(clz.getName()).toString());
	}
	
	/**
	 * 获取接口日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getInterfaceLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(INTERFACE).append(clz.getName()).toString());
	}
	
	/**
	 * 获取业务日志输出
	 * @param clz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static final Logger getBusinessLogger(Class clz){
		return LoggerFactory.getLogger(new StringBuffer(BUSINESS).append(clz.getName()).toString());
	}
}

  4:调用方式

  protected Logger debugLogger = LogFactory.getDebugLogger(getClass());

  debugLogger.info("Request ip info, hostIP===>" + this.hIp+ "   hostPort===>" + this.hPort);

logback + slf4j 学习

标签:

原文地址:http://www.cnblogs.com/working/p/4724893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!