Log4j配置实例(log4j.xml)
一个完整的xml配置文件(propertities文件功能不全,不能使用过滤器,并且个人认为xml格式比较清晰,易读)。
附带测试程序。
- import java.net.URL;
- import org.apache.log4j.xml.DOMConfigurator;
- /**
- * 单实例 读取log4j配置文件,初始化log4j
- *
- */
- public class Log4jXMLConfig
- {
- private static Log4jXMLConfig instance;
- public static synchronized Log4jXMLConfig initia()
- {
- if (instance == null)
- {
- return new Log4jXMLConfig();
- }
- return instance;
- }
- private Log4jXMLConfig()
- {
- final URL url = Log4jXMLConfig.class.getResource("log4j.xml");
- DOMConfigurator.configure(url);
- }
- }
import java.net.URL;import org.apache.log4j.xml.DOMConfigurator;/** * 单实例 读取log4j配置文件,初始化log4j * */public class Log4jXMLConfig{ private static Log4jXMLConfig instance; public static synchronized Log4jXMLConfig initia() { if (instance == null) { return new Log4jXMLConfig(); } return instance; } private Log4jXMLConfig() { final URL url = Log4jXMLConfig.class.getResource("log4j.xml"); DOMConfigurator.configure(url); }}
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration>
- <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
- </layout>
- <!--限制输出级别-->
- <filter class="org.apache.log4j.varia.LevelRangeFilter">
- <param name="LevelMax" value="ERROR"/>
- <param name="LevelMin" value="TRACE"/>
- </filter>
- </appender>
- <appender name="FILE" class="org.apache.log4j.FileAppender">
- <param name="File" value="C:/log4j1.log"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
- </layout>
- </appender>
- <appender name="DATABASE" class="org.apache.log4j.jdbc.JDBCAppender">
- <param name="URL" value="jdbc:oracle:thin:@192.168.0.59:1521:oanet"/>
- <param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
- <param name="user" value="hdczoa"/>
- <param name="password" value="system"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="INSERT INTO hdczoa.LOG4J(stamp,thread, info_level,class,message) VALUES ('%d', '%t', '%p', '%c', %m)" />
- </layout>
- </appender>
- <!-- 发邮件(只有ERROR时才会发送!) -->
- <appender name="MAIL"
- class="org.apache.log4j.net.SMTPAppender">
- <param name="threshold" value="debug" />
- <!-- 日志的错误级别
- <param name="threshold" value="fatal"/>
- -->
- <!-- 缓存文件大小,日志达到512K时发送Email -->
- <param name="BufferSize" value="512" /><!-- 单位K -->
- <param name="From" value="test@163.com" />
- <param name="SMTPHost" value="smtp.163.com" />
- <param name="Subject" value="juyee-log4jMessage" />
- <param name="To" value="test@163.com" />
- <param name="SMTPUsername" value="test" />
- <param name="SMTPPassword" value="test" />
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="%-d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%c] %m%n" />
- </layout>
- </appender>
- <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
- <param name="BufferSize" value="256" />
- <appender-ref ref="DATABASE" />
- </appender>
- <!--通过<logger></logger>的定义可以将各个包中的类日志输出到不同的日志文件中-->
- <logger name="com.litt2.log4j" additivity="false">
- <level value="WARN" />
- <appender-ref ref="CONSOLE" />
- </logger>
- <!--通过<category></category>的定义可以将各个包中的类日志输出到不同的日志文件中-->
- <category name="com.litt3">
- <level value="DEBUG" />
- <appender-ref ref="CONSOLE" />
- <appender-ref ref="MAIL" />
- </category>
- <root>
- <priority value="debug" />
- <appender-ref ref="CONSOLE" />
- <appender-ref ref="FILE" />
- </root>
- </log4j:configuration>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" /> </layout> <!--限制输出级别--> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMax" value="ERROR"/> <param name="LevelMin" value="TRACE"/> </filter> </appender> <appender name="FILE" class="org.apache.log4j.FileAppender"> <param name="File" value="C:/log4j1.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" /> </layout> </appender> <appender name="DATABASE" class="org.apache.log4j.jdbc.JDBCAppender"> <param name="URL" value="jdbc:oracle:thin:@192.168.0.59:1521:oanet"/> <param name="driver" value="oracle.jdbc.driver.OracleDriver"/> <param name="user" value="hdczoa"/> <param name="password" value="system"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="INSERT INTO hdczoa.LOG4J(stamp,thread, info_level,class,message) VALUES ('%d', '%t', '%p', '%c', %m)" /> </layout> </appender> <!-- 发邮件(只有ERROR时才会发送!) --> <appender name="MAIL" class="org.apache.log4j.net.SMTPAppender"> <param name="threshold" value="debug" /> <!-- 日志的错误级别 <param name="threshold" value="fatal"/> --> <!-- 缓存文件大小,日志达到512K时发送Email --> <param name="BufferSize" value="512" /><!-- 单位K --> <param name="From" value="test@163.com" /> <param name="SMTPHost" value="smtp.163.com" /> <param name="Subject" value="juyee-log4jMessage" /> <param name="To" value="test@163.com" /> <param name="SMTPUsername" value="test" /> <param name="SMTPPassword" value="test" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%c] %m%n" /> </layout> </appender> <appender name="ASYNC" class="org.apache.log4j.AsyncAppender"> <param name="BufferSize" value="256" /> <appender-ref ref="DATABASE" /> </appender> <!--通过<logger></logger>的定义可以将各个包中的类日志输出到不同的日志文件中--> <logger name="com.litt2.log4j" additivity="false"> <level value="WARN" /> <appender-ref ref="CONSOLE" /> </logger> <!--通过<category></category>的定义可以将各个包中的类日志输出到不同的日志文件中--> <category name="com.litt3"> <level value="DEBUG" /> <appender-ref ref="CONSOLE" /> <appender-ref ref="MAIL" /> </category> <root> <priority value="debug" /> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root></log4j:configuration>
另外一个配置文件:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <!-- ========================== 自定义输出格式说明================================ -->
- <!-- %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL -->
- <!-- #%r 输出自应用启动到输出该log信息耗费的毫秒数 -->
- <!-- #%c 输出所属的类目,通常就是所在类的全名 -->
- <!-- #%t 输出产生该日志事件的线程名 -->
- <!-- #%n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n” -->
- <!-- #%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似:2002年10月18日 22:10:28,921 -->
- <!-- #%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10) -->
- <!-- ========================================================================== -->
- <!-- ========================== 输出方式说明================================ -->
- <!-- Log4j提供的appender有以下几种: -->
- <!-- org.apache.log4j.ConsoleAppender(控制台), -->
- <!-- org.apache.log4j.FileAppender(文件), -->
- <!-- org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件), -->
- <!-- org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件), -->
- <!-- org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方) -->
- <!-- ========================================================================== -->
- <!-- 输出到日志文件 -->
- <appender name="filelog_appender"
- class="org.apache.log4j.RollingFileAppender">
- <!-- 设置File参数:日志输出文件名 -->
- <param name="File" value="log/testlog4jxml_all.log" />
- <!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
- <param name="Append" value="true" />
- <!-- 设置文件大小 -->
- <param name="MaxFileSize" value="1MB" />
- <!-- 设置文件备份 -->
- <param name="MaxBackupIndex" value="10000" />
- <!-- 设置输出文件项目和格式 -->
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p (%c:%L)- %m%n" />
- </layout>
- </appender>
- <!-- 输出到日志文件 每天一个日志 -->
- <appender name="filelog_daily" class="org.apache.log4j.DailyRollingFileAppender">
- <param name="File" value="log/daily.log" />
- <param name="DatePattern" value="'daily.'yyyy-MM-dd'.log'" />
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] (%c:%L) - %m%n" />
- </layout>
- </appender>
- <!-- 输出到控制台中 -->
- <appender name="console" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n" />
- <!-- "%-5p: [%t] [%c{3}.%M(%L)] | %m%n" -->
- </layout>
- </appender>
- <appender name="EMAIL_QQ" class="org.apache.log4j.net.SMTPAppender">
- <param name="Threshold" value="INFO"/>
- <param name="BufferSize" value="128" />
- <param name="SMTPHost" value="smtp.qq.com" />
- <param name="SMTPUsername" value="cjjvictory" />
- <param name="SMTPPassword" value="***" />
- <param name="From" value="cjjvictory@qq.com" />
- <param name="To" value="cjjvictory@gmail.com" />
- <param name="Subject" value="测试邮件发送" />
- <param name="LocationInfo" value="true" />
- <param name="SMTPDebug" value="true" />
- <layout class="org.cjj.log4j.extend.PatternLayout_zh">
- <param name="ConversionPattern" value="[%d{ISO8601}] %-5p %c %m%n"/>
- </layout>
- </appender>
- <!--- 异步测试,当日志达到缓存区大小时候执行所包的appender -->
- <appender name="ASYNC_test" class="org.apache.log4j.AsyncAppender">
- <param name="BufferSize" value="10"/>
- <appender-ref ref="EMAIL_QQ"/>
- </appender>
- <!-- 设置包限制输出的通道 -->
- <category name="org.cjj" additivity="false">
- <!-- 日志输出级别,起码可以有5个级别,可以扩展自己的级别,邮件发送必须是ERROR级别不好用,所以最后自己扩展一个邮件发送级别 -->
- <level value="ERROR" />
- <appender-ref ref="filelog_daily" />
- <appender-ref ref="daily_appender" />
- <appender-ref ref="console" />
- <appender-ref ref="ASYNC_test" />
- </category>
- </log4j:configuration>
Web配置log4j,需求增加以下内容到WEB-INF/web.xml
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>smilecargo.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>