首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

log4j日记整合输出(slf4j+commonslog+log4j+jdklogger)

2013-02-17 
log4j日志整合输出(slf4j+commonslog+log4j+jdklogger)propertiesslf4j.version1.7.2/slf4j.version

log4j日志整合输出(slf4j+commonslog+log4j+jdklogger)
<properties><slf4j.version>1.7.2</slf4j.version><log4j.version>1.2.17</log4j.version></properties><!-- LOGGING begin --><!-- slf4j --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><!-- slf4j-log4j绑定 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- common-logging 实际调用slf4j --><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><!-- java.util.logging 实际调用slf4j --><dependency><groupId>org.slf4j</groupId><artifactId>jul-to-slf4j</artifactId><version>${slf4j.version}</version></dependency><!-- log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- LOGGING end -->

?

?

2.log4j配置

log4j的配置较简单,使用通常的log4j.properties文件配置就可以,一般放到classes下就OK,你可以在web.xml中加入常量配置配置文件路径。该常量用于filter加载的时候加载日志配置。

?

?

<!--tomcat启动,filter载入的Log4j配置文件位置 --><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param><!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond, 用于在线动态修改log4j日志 --><context-param><param-name>log4jRefreshInterval</param-name><param-value>30000</param-value></context-param>

? ?一般情况下,使用Spring提供的org.springframework.web.util.Log4jConfigListener负载在tomcat启动的时候加载log4j配置托管spring的日志输出配置。

?

? ?log4j.properties

?

log4j.rootCategory=INFO,stdout,logfile# Spring framework log levellog4j.logger.org.springframework=INFO# spring modules log level#log4j.logger.org.springmodules=DEBUG# Hibernate log level# log4j.logger.org.hibernate=DEBUGlog4j.logger.org.hibernate.SQL=DEBUG# Quartz log level#log4j.logger.org.quartz=INFO# Tomcat,commons... etc log level#log4j.logger.org.apache=INFO# Application log level#sql and execute time# log4jdbclog4j.logger.jdbc.sqltiming=INFOlog4j.logger.jdbc.audit=OFFlog4j.logger.jdbc.resultset=OFFlog4j.logger.jdbc.sqlonly=INFOlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n#\u4e3b\u65e5\u5fd7\u6587\u4ef6log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.logfile.File=logs/intergration.loglog4j.appender.logfile.DatePattern=.yyyy-MM-ddlog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n

?

?

?

?

3. 整合日志

    slf4j的日志会自动根据slf4j-log4j12-xxx.jar输出日志到log4j,无需配置。commons-log的日志会自动根据jcl-over-slf4j转到log4j输出,无需配置public class CommonLogAndLog4j {private static final Log log = LogFactory.getLog(CommonLogAndLog4j.class);private static final Logger log4jLogger = Logger.getLogger(CommonLogAndLog4j.class);private static final java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(CommonLogAndLog4j.class.getName());private static final org.slf4j.Logger slf4jLogger = org.slf4j.LoggerFactory.getLogger(CommonLogAndLog4j.class);// -Djava.util.logging.config.file=D:/workshop/log-intergration/src/logging.propertiespublic static void main(String[] args) {installJulToSlf4jBridge();log.info("CommonsLog日志输出");log4jLogger.info("Log4j日志输出");jdkLogger.info("JDK14日志输出");slf4jLogger.info("slft4j日志输出");}public static void installJulToSlf4jBridge() {SLF4JBridgeHandler.removeHandlersForRootLogger();SLF4JBridgeHandler.install();}}

    ?

    ?

    /** * 扩展Spring的Log4jConfigListener,在容器启动的时候,桥接JDK14的输出到slf4j-logger * * @author zhangpu */public class LoggerIntergrationListener extends Log4jConfigListener {@Overridepublic void contextInitialized(ServletContextEvent event) {installJulToSlf4jBridge();event.getServletContext().log("Install Jdk-util-logger to slf4j success.");super.contextInitialized(event);}private void installJulToSlf4jBridge() {SLF4JBridgeHandler.removeHandlersForRootLogger();SLF4JBridgeHandler.install();}}

    <!--Intergration log4j/slf4j/commons-logger/jdk14-logger to log4j --><listener><listener-class>com.feinno.framework.common.web.support.LoggerIntergrationListener</listener-class></listener>

    OK,over!~

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

热点排行