commons-logging是Jarkata封装了多种Java Log,如:Log4j、JDK Log、LogKit、NoOpLog、SimpleLog。
Eclipse下可以使用Log4E插件,快速插入Logger对象,设置Log4E的模版为commons-logging模版就可以了。并同时把commons-logging和log4j的jar包至于工程的classpath中。
在一个类MyClass的代码编辑页面,右键选择Log4E,选择declare logger,即在源代码基础上添加如下几段代码,如下:
import
org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** */ /**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(MyClass. class );
import org.apache.commons.logging.LogFactory;
/** */ /**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(MyClass. class );
需要在src目录下添加log4j.xml文件,如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration >
<!-- config console="STDOUT"/ -->
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{HH:mm:ss.SSS} %-5p [%t] - %m " />
</ layout >
</ appender >
<!-- config debug="DEBUG"/ -->
< appender name ="DEBUG_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/debug.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{HH:mm:ss.SSS} %c -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="DEBUG" />
</ filter >
</ appender >
<!-- config info="INFO"/ -->
< appender name ="INFO_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/denet.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %c -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="INFO" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
<!-- config error="ERROR"/ -->
< appender name ="ERROR_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/error.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="500KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %l -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="ERROR" />
</ filter >
</ appender >
<!-- config error="ERROR"/ -->
< appender name ="FATAL_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/fatal.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %l -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="FATAL" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!--
<appender name="TASK_RUNNING" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/task_running.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} [%t] - %m " />
</layout>
</appender>
<appender name="MONITOR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/monitor.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} %c - %m " />
</layout>
</appender>
<appender name="MONITOR_DAO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/monitor_dao.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} %c - %m " />
</layout>
</appender>
<logger name="monitor.general" additivity="true">
<level value="DEBUG" />
<appender-ref ref="MONITOR" />
</logger>
<logger name="monitor.dao" additivity="true">
<level value="DEBUG" />
<appender-ref ref="MONITOR_DAO" />
</logger>
<logger name="monitor.task" additivity="true">
<level value="DEBUG" />
<appender-ref ref="TASK_RUNNING" />
</logger>
-->
< root >
< priority value ="DEBUG" />
< appender-ref ref ="STDOUT" />
<!-- config ref,you can select what you want to show/ -->
<!--
<appender-ref ref="INFO_LOG" />
<appender-ref ref="ERROR_LOG" />
<appender-ref ref="FATAL_LOG" />
-->
</ root >
</ log4j:configuration >
<!--
Log4J Configuration Quick Reference:
====================================
Priority order is DEBUG < INFO < WARN < ERROR < FATAL
PatternLayout conversion characters:
%c Category of the logging event
%C Fully qualified class name of the caller
%d Date of the logging event (example: %d{HH:mm:ss,SSS} )
%F File name where the logging request was issued (caution: extremely slow)
%l Location information of the caller (caution: extremely slow)
%L Line number from where the logging request was issued (caution: extremely slow)
%m Application-supplied message
%M Method name from where the logging request was issued (caution: extremely slow)
%n Line separator
%p Priority of the logging event
%r Number of milliseconds since the start of the application
%t Name of the thread that generated the logging event
%x Nested diagnotic context associated with the thread
%% A single percent sign
Format modifiers examples:
%20c Left pad with spaces if category is less than 20 characters long
%-20c Right pad with spaces if category is less than 20 characters long
%.30c Truncate from the beginning if category is more than 30 chars long
%20.30c Left pad 20 chars + truncate from beginning if more than 30 chars
%-20.30c Right pad 20 chars + truncate from beginning if more than 30 chars
Examples: "%r [%t] %-5p %c %x - %m "
"%-6r [%15.15t] %-5p %30.30c %x - %m "
-->
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration >
<!-- config console="STDOUT"/ -->
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{HH:mm:ss.SSS} %-5p [%t] - %m " />
</ layout >
</ appender >
<!-- config debug="DEBUG"/ -->
< appender name ="DEBUG_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/debug.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{HH:mm:ss.SSS} %c -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="DEBUG" />
</ filter >
</ appender >
<!-- config info="INFO"/ -->
< appender name ="INFO_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/denet.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %c -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="INFO" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
<!-- config error="ERROR"/ -->
< appender name ="ERROR_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/error.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="500KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %l -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="ERROR" />
</ filter >
</ appender >
<!-- config error="ERROR"/ -->
< appender name ="FATAL_LOG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="log/fatal.log" />
< param name ="Append" value ="true" />
< param name ="MaxFileSize" value ="5000KB" />
< param name ="MaxBackupIndex" value ="2" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %l -- %m " />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="FATAL" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!--
<appender name="TASK_RUNNING" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/task_running.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} [%t] - %m " />
</layout>
</appender>
<appender name="MONITOR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/monitor.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} %c - %m " />
</layout>
</appender>
<appender name="MONITOR_DAO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/monitor_dao.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy:MM:dd HH:mm:ss.SSS} %c - %m " />
</layout>
</appender>
<logger name="monitor.general" additivity="true">
<level value="DEBUG" />
<appender-ref ref="MONITOR" />
</logger>
<logger name="monitor.dao" additivity="true">
<level value="DEBUG" />
<appender-ref ref="MONITOR_DAO" />
</logger>
<logger name="monitor.task" additivity="true">
<level value="DEBUG" />
<appender-ref ref="TASK_RUNNING" />
</logger>
-->
< root >
< priority value ="DEBUG" />
< appender-ref ref ="STDOUT" />
<!-- config ref,you can select what you want to show/ -->
<!--
<appender-ref ref="INFO_LOG" />
<appender-ref ref="ERROR_LOG" />
<appender-ref ref="FATAL_LOG" />
-->
</ root >
</ log4j:configuration >
<!--
Log4J Configuration Quick Reference:
====================================
Priority order is DEBUG < INFO < WARN < ERROR < FATAL
PatternLayout conversion characters:
%c Category of the logging event
%C Fully qualified class name of the caller
%d Date of the logging event (example: %d{HH:mm:ss,SSS} )
%F File name where the logging request was issued (caution: extremely slow)
%l Location information of the caller (caution: extremely slow)
%L Line number from where the logging request was issued (caution: extremely slow)
%m Application-supplied message
%M Method name from where the logging request was issued (caution: extremely slow)
%n Line separator
%p Priority of the logging event
%r Number of milliseconds since the start of the application
%t Name of the thread that generated the logging event
%x Nested diagnotic context associated with the thread
%% A single percent sign
Format modifiers examples:
%20c Left pad with spaces if category is less than 20 characters long
%-20c Right pad with spaces if category is less than 20 characters long
%.30c Truncate from the beginning if category is more than 30 chars long
%20.30c Left pad 20 chars + truncate from beginning if more than 30 chars
%-20.30c Right pad 20 chars + truncate from beginning if more than 30 chars
Examples: "%r [%t] %-5p %c %x - %m "
"%-6r [%15.15t] %-5p %30.30c %x - %m "
-->
大功告成!