没时间解释了,快上车,老司机先看代码

LogBack.xml

<configuration>

    <property name="ROOT_LEVEL" value="INFO" />
    <timestamp key="byDate" datePattern="yyyy-MM-dd"/>
    <property name="MESSAGE_FILE_PATTERN" value="%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level %marker %thread %logger %msg%n" />
    <property name="maxhistory" value="40"/>
 
    <appender name="STDOUT1" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>${MESSAGE_FILE_PATTERN}</Pattern>
        </layout>
    </appender>

    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
                <level>INFO</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
        <file>${catalina.base}/logs/vod.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/vod-info-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${maxhistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${MESSAGE_FILE_PATTERN}
            </pattern>
        </encoder>
    </appender>
    <appender name="musicAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
                <level>INFO</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
        <file>${catalina.base}/logs/vod-music.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/vod-music-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${maxhistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${MESSAGE_FILE_PATTERN}
            </pattern>
        </encoder>
    </appender>

    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <file>${catalina.base}/logs/vod-error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/vod-error-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${maxhistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${MESSAGE_FILE_PATTERN}
            </pattern>
        </encoder>
    </appender>
    <appender name="doubanAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
                <level>INFO</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
        <file>${catalina.base}/logs/vod-douban.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/vod-douban-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>${maxhistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${MESSAGE_FILE_PATTERN}
            </pattern>
        </encoder>
    </appender>
   
    
    <logger name="org.springframework" level="INFO">
    </logger>

    <logger name="org.hibernate" level="INFO">
    </logger>

    <logger name="org.apache.http" level="INFO">
    </logger>

    <logger name="org.apache.commons" level="INFO">
    </logger>

    <logger name="freemarker" level="INFO">
    </logger>

    <logger name="io.netty" level="INFO">
    </logger>

    <logger name="org.jooby" level="INFO">
    </logger>
    
    <logger name="com.vsoontech.music" level="INFO" additivity="false"> 
        <appender-ref ref="musicAppender"/>
        <appender-ref ref="ERROR"/>
    </logger>
    
    <logger name="com.vod.utils" level="INFO" additivity="false"> 
        <appender-ref ref="doubanAppender"/>
        <appender-ref ref="ERROR"/>
    </logger>
    
    <root level="${ROOT_LEVEL}">
        <appender-ref ref="INFO"/>
        <appender-ref ref="ERROR"/>
        <appender-ref ref="STDOUT1"/>
    </root>
</configuration>

上述实现了
1、将DEBUG、INFO、WRAN/ERROR日志分开打印到不同文件
2、通过包名来按模块打印到特定文件
3、日志文件每天rolling、保存最大历史日志文件数

几个注意点:
1、日志打印格式如下:%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level %marker %thread %logger %msg%n
没有打印具体的方法名。如果要打印方法名:加个%M
%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level %marker %thread %logger.%M %msg%n
但是打印方法名可能会加大日志开销,从而影响速度问题。
说明见:http://stackoverflow.com/ques...

Generating the method name is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.

2、additivity的作用
additivity的作用在于children-logger是否使用 rootLogger配置的appender进行输出。
false:表示只用当前logger的appender-ref。
true:表示当前logger的appender-ref和rootLogger的appender-ref都有效。也就是说会打印至少两遍。

3、filter的作用
过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一。返回DENY,日志将立即被抛弃不再经过其他过滤器;返回NEUTRAL,有序列表里的下个过滤器过接着处理日志;返回ACCEPT,日志会被立即处理,不再经过剩余过滤器。
过滤器被添加到<Appender> 中,为<Appender> 添加一个或多个过滤器后,可以用任意条件对日志进行过滤。<Appender> 有多个过滤器时,按照配置顺序执行。

几个常用的过滤器:
LevelFilter: 级别过滤器,根据日志级别进行过滤。如果日志级别等于配置级别,过滤器会根据onMath 和 onMismatch接收或拒绝日志。有以下子节点:
<level>:设置过滤级别
<onMatch>:用于配置符合过滤条件的操作
<onMismatch>:用于配置不符合过滤条件的操作

例如:将过滤器的日志级别配置为INFO,所有INFO级别的日志交给appender处理,非INFO级别的日志,被过滤掉。

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">   
    <filter class="ch.qos.logback.classic.filter.LevelFilter">   
      <level>INFO</level>   
      <onMatch>ACCEPT</onMatch>   
      <onMismatch>DENY</onMismatch>   
    </filter>   
    <encoder>   
      <pattern>   
        %-4relative [%thread] %-5level %logger{30} - %msg%n   
      </pattern>   
    </encoder>   
</appender> 

ThresholdFilter: 临界值过滤器,过滤掉低于指定临界值的日志。当日志级别等于或高于临界值时,过滤器返回NEUTRAL;当日志级别低于临界值时,日志会被拒绝。
例如:过滤掉所有低于INFO级别的日志。

<appender name="CONSOLE"   
    class="ch.qos.logback.core.ConsoleAppender">   
    <!-- 过滤掉 TRACE 和 DEBUG 级别的日志-->   
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">   
      <level>INFO</level>   
    </filter>   
    <encoder>   
      <pattern>   
        %-4relative [%thread] %-5level %logger{30} - %msg%n   
      </pattern>   
    </encoder>   
  </appender> 

EvaluatorFilter: 求值过滤器,评估、鉴别日志是否符合指定条件。需要额外的两个JAR包,commons-compiler.jar和janino.jar有以下子节点:
<evaluator>:
鉴别器,常用的鉴别器是JaninoEventEvaluato,也是默认的鉴别器,它以任意的java布尔值表达式作为求值条件,求值条件在配置文件解释过成功被动态编译,布尔值表达式返回true就表示符合过滤条件。evaluator有个子标签<expression>,用于配置求值条件。
<onMatch>:用于配置符合过滤条件的操作
<onMismatch>:用于配置不符合过滤条件的操作

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">   
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">         
      <evaluator> <!-- 默认为 ch.qos.logback.classic.boolex.JaninoEventEvaluator -->   
        <expression>return message.contains("billing");</expression>   
      </evaluator>   
      <OnMatch>ACCEPT </OnMatch>  
      <OnMismatch>DENY</OnMismatch>  
    </filter>   
    <encoder>   
      <pattern>   
        %-4relative [%thread] %-5level %logger - %msg%n   
      </pattern>   
    </encoder>   
  </appender> 

参考:
https://github.com/makotan/lo...
http://stackoverflow.com/ques...
http://czj4451.iteye.com/blog...
http://aub.iteye.com/blog/111...

Log4j.properties配置

# Output pattern : date [thread] priority category - message
log4j.rootLogger=INFO, Console, RollingFile,Error

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[shopping] %-d{HH:mm:ss} %p [%c{1}.%M(%L)]  %m%n

#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=${catalina.base}/logs/shopping/shopping.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=[shopping] %-d{HH:mm:ss} %p [%c{1}.%M(%L)]  %m%n
log4j.appender.RollingFile.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.RollingFile.filter.a.LevelMin=INFO
log4j.appender.RollingFile.filter.a.LevelMax=INFO
log4j.appender.RollingFile.filter.a.acceptOnMatch=true
 
#ShoppingUtil
log4j.appender.ShoppingUtil=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ShoppingUtil.File=${catalina.base}/logs/shopping/shopping-util.log
log4j.appender.ShoppingUtil.layout=org.apache.log4j.PatternLayout
log4j.appender.ShoppingUtil.layout.ConversionPattern=[shopping] %-d{HH:mm:ss} %p [%c{1}.%M(%L)]  %m%n
log4j.appender.ShoppingUtil.filter.aa=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.ShoppingUtil.filter.aa.LevelMin=INFO
log4j.appender.ShoppingUtil.filter.aa.LevelMax=INFO
log4j.appender.ShoppingUtil.filter.aa.acceptOnMatch=true
 

#error
log4j.appender.Error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Error.File=${catalina.base}/logs/shopping/shopping-error.log
log4j.appender.Error.layout=org.apache.log4j.PatternLayout
log4j.appender.Error.layout.ConversionPattern=[shopping] %-d{HH:mm:ss} %p [%c{1}.%M(%L)]  %m%n
log4j.appender.Error.Threshold=WARN


#Project defalult level
log4j.logger.org.springside.examples.miniweb=INFO

log4j.logger.com.opensymphony.xwork2.interceptor.ParametersInterceptor=DEBUG

log4j.logger.org.springside= INFO

log4j.logger.com.shopping= INFO

log4j.logger.com.shopping.shop.utility=INFO,Console,ShoppingUtil,Error
log4j.additivity.com.shopping.shop.utility=false 

参考:
http://stackoverflow.com/ques...
http://www.cnblogs.com/0201zc...
http://blog.csdn.net/liu251/a...


xbynet
1k 声望124 粉丝

不雨花犹落,无风絮自飞