log4j - 配置文件
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="appender" class="org.apache.log4j.FileAppender"> <param name="File" value="xml-log.txt"/> <param name="Append" value="true"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %p - %m%n"/> </layout> </appender> <root> <priority value ="debug"/> <appender-ref ref="appender"/> </root> </log4j:configuration>
log4j-plus.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd"> <!-- 此为网络查找的模版,不确保可用,仅提供参考! --> <!-- 此为网络查找的模版,不确保可用,仅提供参考! --> <!-- 此为网络查找的模版,不确保可用,仅提供参考! --> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- 可以配置多个appender来对应不同的输出,如文件输出,sql输出,控制台输出,邮件输出等 --> <!-- [控制台STDOUT] 不同的输出类型对应着不同的calss,如控制台输出class对应着 org.apache.log4j.ConsoleAppender --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <!-- name提供给logger或者root调用 --> <param name="encoding" value="UTF-8" /> <!-- 保存字符集 --> <param name="target" value="System.out" /> <!-- 输出到控制台 --> <layout class="org.apache.log4j.PatternLayout"> <!-- loyout表示输出方式,可以多种,class值区分,PatternLayout表示自定义格式 --> <param name="ConversionPattern" value="%d{ISO8601} 耗时:%r [日志来自:%-40.40c{3} 日志类型: %-5p 日志内容:%m]%n" /> <!-- 输出格式,后面解释 --> </layout> <!--filter过滤器设置输出的级别:ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF 所有下面输出的是debug到warn不会有error和fatal --> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="levelMin" value="debug" /> <param name="levelMax" value="warn" /> <param name="AcceptOnMatch" value="true" /> <!-- 答案:http://bbs.csdn.net/topics/350195913 --> </filter> </appender> <!-- [公共Appender] 这个class表示输入到文件,并且按日期生成新文件 --> <appender name="DEFAULT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="${webapp.root}/logs/common-default.log" /> <!-- ${webapp.root}项目根路径,自动获得,不用配置,可自己在web.xml中配置 --> <param name="Append" value="true" /> <!-- 是否项目重启继续保存之前日志 --> <param name="encoding" value="UTF-8" /> <param name="threshold" value="all" /> <!-- 记录所有类型日志,记录它和比它等级高的日志all<debug --> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <!-- 日期格式 例子:common-default.log.2015-09-17.log --> <layout class="org.apache.log4j.PatternLayout"> <!-- 输出方式 --> <param name="ConversionPattern" value="%d{ISO8601} 耗时:%r [日志来自:%-40.40c{3} 日志类型: %-5p 日志内容:%m]%n" /> </layout> </appender> <!-- [debug日志APPENDER] --> <appender name="DEBUG-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="${webapp.root}/logs/controller/debug-log.log" /> <param name="Append" value="true" /> <param name="encoding" value="UTF-8" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} 耗时:%r [日志来自:%-40.40c{3} 日志类型: %-5p 日志内容:%m]%n" /> </layout> </appender> <!-- [info日志APPENDER] --> <appender name="INFO-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="${webapp.root}/logs/controller/info-log.log" /> <param name="Append" value="false" /> <param name="encoding" value="UTF-8" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} 耗时:%r [日志来自:%-40.40c{3} 日志类型: %-5p 日志内容:%m]%n" /> </layout> <!-- 我只想记录info日志,就做如下设置 --> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMax" value="info" /> <param name="LevelMin" value="info" /> </filter> </appender> <!-- [组件日志APPENDER] --> <appender name="COMPONENT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="${webapp.root}/logs/logistics-component.log" /> <param name="Append" value="true" /> <param name="encoding" value="UTF-8" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} 耗时:%r [日志来自:%-40.40c{3} 日志类型: %-5p 日志内容:%m]%n" /> </layout> </appender> <!-- debug log --> <!-- name指定的是包名,表示这个logger只记录com.wzw.controller下的所有日志 --> <logger name="com.wzw.controller" additivity="true"> <!-- additivity 是否继承root --> <!-- <level value="${loggingLevel}" /> --> <level value="DEBUG"></level> <!-- 现在级别 --> <appender-ref ref="DEBUG-APPENDER" /> </logger> <!-- info log --> <!-- name指定的是包名,这样只操作此包下的log --> <logger name="com.wzw.controller" additivity="true"> <level value="INFO"></level> <appender-ref ref="INFO-APPENDER" /> <appender-ref ref="console" /> </logger> <!-- Root Logger --> <!-- 所有logger的父类,记录所有的日志。 --> <root> <level value="ALL"></level> <!-- 限定记录等级 --> <appender-ref ref="DEFAULT-APPENDER" /> <!-- 调用记录方式 --> <appender-ref ref="console" /> </root> </log4j:configuration>
log4j.properties
因为是自己看的,注释里包含了一些额外的代码
### direct log messages to stdout and logFile###
log4j.rootCategory=INFO,stdout,logFile
# OpenSymphony Stuff
#log4j.logger.com.opensymphony=INFO
#log4j.logger.org.apache.struts2=INFO
#log4j.logger.org.apache.commons=INFO
#
## Spring Stuff
#log4j.logger.org.springframework=INFO
#log4j.logger.org.springframework.oxm=INFO
#
## Hibernate Stuff
#log4j.logger.org.hibernate=INFO
#log4j.logger.org.hibernate.type=INFO
#log4j.logger.org.hibernate.tool.hbm2ddl=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd hh\:mm\:ss}:%p] [%c:%m] %n
log4j.appender.logFile=org.apache.log4j.RollingFileAppender
log4j.appender.logFile.File=C\:\\demo.log
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=:%d{yyyy-MM-dd hh\:mm\:ss}:%p] [%c:%m] %n
log4j.appender.logFile.MaxFileSize = 5MB
log4j.appender.logFile.MaxBackupIndex =3
测试类
package com.yt.test.log4j.file; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; /** * * @author ChenSS 2017年8月7日下午7:40:15 * */ public class FileConfigure { static Logger logger = Logger.getLogger(FileConfigure.class); public static void initDomConfigure(){ DOMConfigurator.configure("src/log4j.xml"); } public static void initDomConfigure2() { DOMConfigurator.configure("src/log4j-plus.xml"); } public static void initPropertyConfigure() { PropertyConfigurator.configure("src/log4j.properties"); } public static void main(String args[]) { FileConfigure.initDomConfigure(); logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); } }
疯狂的妞妞 :每一天,做什么都好,不要什么都不做!