分享

log4j logback slf4j项目配置中的简单总结

 CevenCheng 2010-09-19

一. Log4j+commons-logging 
JAR包 
commons-logging-1.1.jar 
log4j-1.2.15.jar 
配置文件 
commons-logging.properties 
log4j.xml 
commons-logging.jar包读取commons-logging.properties中指定的log,自动加载日志配置 
在commons-logging+log4j组合中commons-logging.properties指定的日志为: 
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 
log4j指定具体日志输出配置 
如果未选用commons-logging,可以在启动项目中,主动加载log4j的配置文件 
二. Logback+slf4j 
JAR包 
slf4j-api-1.5.2.jar 
logback-classic-0.9.9.jar 
logback-core-0.9.9.jar 
配置文件 
Logback.xml 
logback的加载采用编码加载

Java代码 
1.String logbackCfg = "/config/logback.xml";   
2.URL logURL = new ClassPathResource(logbackCfg).getURL();   
3.ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();   
4.LoggerContext loggerContext = (LoggerContext) loggerFactory;   
5.loggerContext.shutdownAndReset();   
6.JoranConfigurator configurator = new JoranConfigurator();   
7.configurator.setContext(loggerContext);   
8.configurator.doConfigure(logURL); 
String logbackCfg = "/config/logback.xml";
URL logURL = new ClassPathResource(logbackCfg).getURL();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
LoggerContext loggerContext = (LoggerContext) loggerFactory;
loggerContext.shutdownAndReset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(logURL);
三. Slf4j+log4j 
JAR包 
slf4j-api-1.5.2.jar 
slf4j-log4j12-1.5.2.jar 
log4j-1.2.15.jar 
配置文件 
Logback.xml 
四. Slf4j+logback+logback 
注:这种情况下是项目中有log4j输出的日志以及logback数据的日志.老项目改造,log4j和logback共存的情况下.(当选用spring+slf4j+logback的时候,spring的日志输出是log4j的,这个时候采用这种配置.) 
JARbao 
log4j-over-slf4j-1.5.2.jar 
logback-classic-0.9.9.jar 
logback-core-0.9.9.jar 
slf4j-api-1.5.2.jar 
配置文件 
Logback.xml 
配置加载

Java代码 
1.String logbackCfg = "/config/logback.xml";   
2.URL logURL = new ClassPathResource(logbackCfg).getURL();   
3.ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();   
4.LoggerContext loggerContext = (LoggerContext) loggerFactory;   
5.loggerContext.shutdownAndReset();   
6.JoranConfigurator configurator = new JoranConfigurator();   
7.configurator.setContext(loggerContext);   
8.configurator.doConfigure(logURL); 
String logbackCfg = "/config/logback.xml";
URL logURL = new ClassPathResource(logbackCfg).getURL();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
LoggerContext loggerContext = (LoggerContext) loggerFactory;
loggerContext.shutdownAndReset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(logURL);
五. Log4j配置文件示例

Java代码 
1.<?xml version="1.0" encoding="UTF-8"?>   
2.<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">   
3.<log4j:configuration xmlns:log4j="
http://jakarta./log4j/">   
4. 
5.    <!-- 控制台输出-->   
6.    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">   
7.        <param name="Target" value="System.out" />   
8.        <param name="Threshold" value="INFO" />   
9.        <layout class="org.apache.log4j.PatternLayout">   
10.            <param name="ConversionPattern" 
11.                value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />   
12.        </layout>   
13. 
14.<!— 以下是过滤一些不需要输出的log信息 -->   
15.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
16.            <param name="StringToMatch" value="new a seq no spend" />   
17.            <param name="AcceptOnMatch" value="false" />   
18.        </filter>   
19.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
20.            <param name="StringToMatch" value="new hi[type=" />   
21.            <param name="AcceptOnMatch" value="false" />   
22.        </filter>   
23.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
24.            <param name="StringToMatch" value="FileTransfer.execCommand()" />   
25.            <param name="AcceptOnMatch" value="false" />   
26.        </filter>   
27.    </appender>   
28. 
29.    <!--文件输出 按照文件大小滚动文件-->   
30.    <appender name="xxxx_file" class="org.apache.log4j.RollingFileAppender">   
31.        <param name="Append" value="true" />   
32.        <param name="Threshold" value="INFO" />   
33.        <param name="File" value="xxxx.log" />   
34.        <param name="MaxFileSize" value="100MB" />   
35.        <param name="MaxBackupIndex" value="15" />   
36.        <layout class="org.apache.log4j.PatternLayout">   
37.            <param name="ConversionPattern" 
38.                value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />   
39.        </layout>   
40.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
41.            <param name="StringToMatch" value="new a seq no spend" />   
42.            <param name="AcceptOnMatch" value="false" />   
43.        </filter>   
44.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
45.            <param name="StringToMatch" value="new hi[type=" />   
46.            <param name="AcceptOnMatch" value="false" />   
47.        </filter>   
48.        <filter class="org.apache.log4j.varia.StringMatchFilter">   
49.            <param name="StringToMatch" value="FileTransfer.execCommand()" />   
50.            <param name="AcceptOnMatch" value="false" />   
51.        </filter>   
52.    </appender>   
53.       
54.    <!-- 错误输出 过滤出错误日志信息-->   
55.    <appender name="xxxx_error" class="org.apache.log4j.RollingFileAppender">   
56.        <param name="Append" value="true" />   
57.        <param name="Threshold" value="ERROR" />   
58.        <param name="File" value="xxxx-error.log" />   
59.        <param name="MaxFileSize" value="100MB" />   
60.        <param name="MaxBackupIndex" value="4" />   
61.        <layout class="org.apache.log4j.PatternLayout">   
62.            <param name="ConversionPattern" 
63.                value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />   
64.        </layout>   
65.    </appender>   
66.      
67. <!-- 根loggr -->   
68.    <root>   
69.        <appender-ref ref="CONSOLE" />   
70.        <appender-ref ref="xxxx_file" />   
71.        <appender-ref ref="xxxx_file_error" />   
72.    </root>   
73.</log4j:configuration> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="
http://jakarta./log4j/">

<!-- 控制台输出-->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
   <param name="Target" value="System.out" />
   <param name="Threshold" value="INFO" />
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
     value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />
   </layout>

<!— 以下是过滤一些不需要输出的log信息 -->
   <filter class="org.apache.log4j.varia.StringMatchFilter">
    <param name="StringToMatch" value="new a seq no spend" />
    <param name="AcceptOnMatch" value="false" />
   </filter>
   <filter class="org.apache.log4j.varia.StringMatchFilter">
    <param name="StringToMatch" value="new hi[type=" />
    <param name="AcceptOnMatch" value="false" />
   </filter>
   <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="FileTransfer.execCommand()" />
            <param name="AcceptOnMatch" value="false" />
        </filter>
</appender>

<!--文件输出 按照文件大小滚动文件-->
<appender name="xxxx_file" class="org.apache.log4j.RollingFileAppender">
   <param name="Append" value="true" />
   <param name="Threshold" value="INFO" />
   <param name="File" value="xxxx.log" />
   <param name="MaxFileSize" value="100MB" />
   <param name="MaxBackupIndex" value="15" />
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
     value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />
   </layout>
   <filter class="org.apache.log4j.varia.StringMatchFilter">
    <param name="StringToMatch" value="new a seq no spend" />
    <param name="AcceptOnMatch" value="false" />
   </filter>
   <filter class="org.apache.log4j.varia.StringMatchFilter">
    <param name="StringToMatch" value="new hi[type=" />
    <param name="AcceptOnMatch" value="false" />
   </filter>
   <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="FileTransfer.execCommand()" />
            <param name="AcceptOnMatch" value="false" />
        </filter>
</appender>

<!-- 错误输出 过滤出错误日志信息-->
<appender name="xxxx_error" class="org.apache.log4j.RollingFileAppender">
   <param name="Append" value="true" />
   <param name="Threshold" value="ERROR" />
   <param name="File" value="xxxx-error.log" />
   <param name="MaxFileSize" value="100MB" />
   <param name="MaxBackupIndex" value="4" />
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
     value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" />
   </layout>
</appender>
   
<!-- 根loggr -->
<root>
   <appender-ref ref="CONSOLE" />
   <appender-ref ref="xxxx_file" />
   <appender-ref ref="xxxx_file_error" />
</root>
</log4j:configuration>
六. Logback配置文件示例

Java代码 
1.<?xml version="1.0" encoding="UTF-8" ?>   
2.<!DOCTYPE configuration>   
3. 
4.<configuration>   
5.    <jmxConfigurator />   
6.       
7.    <!-- 控制台输出日志 -->   
8.    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">   
9.        <layout class="ch.qos.logback.classic.PatternLayout">   
10.            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>   
11.        </layout>   
12.    </appender>   
13.       
14.    <!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->   
15.    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
16.        <File>xxxx.log</File>   
17.        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">   
18.            <FileNamePattern>xxxx.log.%i.bak</FileNamePattern>   
19.            <MinIndex>1</MinIndex>   
20.            <MaxIndex>12</MaxIndex>   
21.        </rollingPolicy>   
22. 
23.        <triggeringPolicy   
24.            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">   
25.            <MaxFileSize>100MB</MaxFileSize>   
26.        </triggeringPolicy>   
27.        <layout class="ch.qos.logback.classic.PatternLayout">   
28.            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>   
29.        </layout>   
30.    </appender>   
31.     
32.    <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">   
33.         <filter class="ch.qos.logback.classic.filter.LevelFilter">   
34.            <level>ERROR</level>   
35.            <OnMismatch>DENY</OnMismatch>   
36.            <OnMatch>ACCEPT</OnMatch>   
37.        </filter>   
38.           
39.        <File>xxxx-err.log</File>   
40.        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">   
41.            <FileNamePattern>xxxx-err.log.%i.bak</FileNamePattern>   
42.            <MinIndex>1</MinIndex>   
43.            <MaxIndex>3</MaxIndex>   
44.        </rollingPolicy>   
45.        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">   
46.            <MaxFileSize>100MB</MaxFileSize>   
47.        </triggeringPolicy>   
48.        <layout class="ch.qos.logback.classic.PatternLayout">   
49.            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>   
50.        </layout>   
51.    </appender>   
52. 
53. <!--这里指定logger name 是为jmx设置日志级别做铺垫 -->          
54.    <logger name="com.xxx.xxx">   
55.        <level value="INFO" />   
56.        <appender-ref ref="STDOUT" />   
57.        <appender-ref ref="FILE" />   
58.        <appender-ref ref="FILE-ERROR" />   
59.    </logger>   
60. 
61. 
62. 
63.</configuration> 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
    <jmxConfigurator />
    
    <!-- 控制台输出日志 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>
    
    <!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>xxxx.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>xxxx.log.%i.bak</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>12</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <OnMismatch>DENY</OnMismatch>
            <OnMatch>ACCEPT</OnMatch>
        </filter>
        
        <File>xxxx-err.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>xxxx-err.log.%i.bak</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>3</MaxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

<!--这里指定logger name 是为jmx设置日志级别做铺垫 -->      
    <logger name="com.xxx.xxx">
        <level value="INFO" />
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
        <appender-ref ref="FILE-ERROR" />
    </logger>

</configuration>

七. 相关简单说明 
Log4j 
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务 器、NT的事件记录器、UNIX Syslog守护进程等;用户也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,用户能够更加细致地控制日志的生成过程。这些可以通过一个 配置文件来灵活地进行配置,而不需要修改程序代码

Slf4j 
简单日记门面(Facade)SLF4J是为各种loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现。 Logging API实现既可以选择直接实现SLF4J接的loging APIs如: NLOG4J、SimpleLogger。也可以通过SLF4J提供的API实现来开发相应的适配器如Log4jLoggerAdapter、JDK14LoggerAdapter。

Logback 
Logback是由log4j创始人设计的又一个开源日记组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日记系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日记的功能。 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多