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的配置文件
二. slf4j+Logback
JAR包
slf4j-api-1.5.2.jar
logback-classic-0.9.9.jar
logback-core-0.9.9.jar
配置文件
Logback.xml
logback的加载采用编码加载
- 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+log4j
注:这种情况下是项目中有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
配置加载
- 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配置文件示例
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/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配置文件示例
- <?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来访问日记的功能。