xmllog4jconfig.xml 如下:
对比properties方式和xml方式,可以看出他们程序中,除了定义读取配置文件的方式不同外,其它的根本就没
有区别了,这也就是log4j的魅力所在了,不需要改变程序,只需要通过改变定义的配置文件,我们就可以控制日志的输
出方式,是否输出,输出格式。我们可以通过定义不同的xml文件来控制输出方式.以下是各种输出方式与布局的例子。
4.2.3 比较简单的一个。
4.2.4 文件的输出 pattern的布局
4.2.5 控制台的输出 和简单的布局
4.2.6 定义三种输出方式,按照不同的级别,选择不同的输出方式.debug,info,级别通过System.out来打印。
WARN,FATAL通过System.err来输出。ERROR,FATAL发送到邮箱。
五、参考资料
1.http://www.vipan.com/htdocs/log4jhelp.html
Dont Use System.out.println! Use Log4j - Vipan Singla
2.http://jakarta.apache.org/log4j/docs/manual.html
Short introduction to log4j - Ceki Gülcü - March 2002
3.http://www-128.ibm.com/developerworks/cn/java/jw-log4j/
log4j提供了对日志记录的控制
4.http://logging.apache.org/log4j/docs/documentation.html
log4j提供的文档。
转帖自:http://jakarta.apache.org/log4j/ xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="appender" class ="org.apache.log4j.FileAppender" >
< param name ="File" value ="Indentify-Log.txt" />
< param name ="Append" value ="false" />
< 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 >
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="appender" class ="org.apache.log4j.FileAppender" >
< param name ="File" value ="Indentify-Log.txt" />
< param name ="Append" value ="false" />
< 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 >
对比properties方式和xml方式,可以看出他们程序中,除了定义读取配置文件的方式不同外,其它的根本就没
有区别了,这也就是log4j的魅力所在了,不需要改变程序,只需要通过改变定义的配置文件,我们就可以控制日志的输
出方式,是否输出,输出格式。我们可以通过定义不同的xml文件来控制输出方式.以下是各种输出方式与布局的例子。
4.2.3 比较简单的一个。
<?
xml version="1.0" encoding="gb2312"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
<!--
配置输出的日志目录为在控制台。
格式采用为PatternLoyout,并且样式为:%d{ISO8601} %r [%c :: %p %m]%n
-->
< appender name ="CA" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %r [%c :: %p %m]%n" />
</ layout >
</ appender >
< root >
< priority value ="debug" />
< appender-ref ref ="CA" />
</ root >
</ log4j:configuration >
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
<!--
配置输出的日志目录为在控制台。
格式采用为PatternLoyout,并且样式为:%d{ISO8601} %r [%c :: %p %m]%n
-->
< appender name ="CA" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{ISO8601} %r [%c :: %p %m]%n" />
</ layout >
</ appender >
< root >
< priority value ="debug" />
< appender-ref ref ="CA" />
</ root >
</ log4j:configuration >
4.2.4 文件的输出 pattern的布局
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="appender" class ="org.apache.log4j.FileAppender" >
< param name ="File" value ="Indentify-Log.txt" />
< param name ="Append" value ="false" />
< 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 >
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="appender" class ="org.apache.log4j.FileAppender" >
< param name ="File" value ="Indentify-Log.txt" />
< param name ="Append" value ="false" />
< 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 >
4.2.5 控制台的输出 和简单的布局
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="ConsoleAppender" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.SimpleLayout" />
</ appender >
< root >
< priority value ="debug" />
< appender-ref ref ="ConsoleAppender" />
</ root >
</ log4j:configuration >
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration xmlns:log4j ="http://jakarta.apache.org/log4j/" >
< appender name ="ConsoleAppender" class ="org.apache.log4j.ConsoleAppender" >
< layout class ="org.apache.log4j.SimpleLayout" />
</ appender >
< root >
< priority value ="debug" />
< appender-ref ref ="ConsoleAppender" />
</ root >
</ log4j:configuration >
4.2.6 定义三种输出方式,按照不同的级别,选择不同的输出方式.debug,info,级别通过System.out来打印。
WARN,FATAL通过System.err来输出。ERROR,FATAL发送到邮箱。
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration >
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
< param name ="Target" value ="System.out" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}] %-5p %c %m %n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
< appender name ="STDERR" class ="org.apache.log4j.ConsoleAppender" >
< param name ="Target" value ="System.err" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}] %-5p %c %m %n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="WARN" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!-- -->
<!-- Declare the SMTPAppender -->
<!-- -->
< appender name ="EMAIL" class ="org.apache.log4j.net.SMTPAppender" >
< param name ="BufferSize" value ="512" />
< param name ="SMTPHost" value ="mail.suiyuanwu.com" />
< param name ="From" value ="log4j@server5" />
< param name ="To" value ="web@suiyuanwu.com" />
< param name ="Subject" value ="[SMTPAppender] Application message" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!-- -->
<!-- setup log4js root logger -->
<!-- -->
< root >
< level value ="all" />
< appender-ref ref ="STDOUT" />
< appender-ref ref ="STDERR" />
< appender-ref ref ="EMAIL" />
</ root >
</ log4j:configuration >
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
< log4j:configuration >
< appender name ="STDOUT" class ="org.apache.log4j.ConsoleAppender" >
< param name ="Target" value ="System.out" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}] %-5p %c %m %n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
< appender name ="STDERR" class ="org.apache.log4j.ConsoleAppender" >
< param name ="Target" value ="System.err" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}] %-5p %c %m %n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="WARN" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!-- -->
<!-- Declare the SMTPAppender -->
<!-- -->
< appender name ="EMAIL" class ="org.apache.log4j.net.SMTPAppender" >
< param name ="BufferSize" value ="512" />
< param name ="SMTPHost" value ="mail.suiyuanwu.com" />
< param name ="From" value ="log4j@server5" />
< param name ="To" value ="web@suiyuanwu.com" />
< param name ="Subject" value ="[SMTPAppender] Application message" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!-- -->
<!-- setup log4js root logger -->
<!-- -->
< root >
< level value ="all" />
< appender-ref ref ="STDOUT" />
< appender-ref ref ="STDERR" />
< appender-ref ref ="EMAIL" />
</ root >
</ log4j:configuration >
五、参考资料
1.http://www.vipan.com/htdocs/log4jhelp.html
Dont Use System.out.println! Use Log4j - Vipan Singla
2.http://jakarta.apache.org/log4j/docs/manual.html
Short introduction to log4j - Ceki Gülcü - March 2002
3.http://www-128.ibm.com/developerworks/cn/java/jw-log4j/
log4j提供了对日志记录的控制
4.http://logging.apache.org/log4j/docs/documentation.html
log4j提供的文档。
转帖自:http://jakarta.apache.org/log4j/ xml