聂永的博客

记录工作/学习的点点滴滴。

Log4j.xml 学习笔记

一。log4j的基础知识

 1.log4j有三种主要组件:logger、appender and layout
  2.Log4j提供的appender有以下几种:
  org.apache.log4j.ConsoleAppender(控制台)
  org.apache.log4j.FileAppender(文件)
  org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
  org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
  org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
  3.Log4j提供的layout有以下几种:
  org.apache.log4j.HTMLLayout(以HTML表格形式布局)
  org.apache.log4j.PatternLayout(可以灵活地指定布局模式)
  org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串)
  org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
  4.Log4j提供的几种输出格式:
  %M:Used to output the method name where the logging request was issued.
  %m:Used to output the application supplied message associated with the logging event.
  %l:Used to output location information of the caller which generated the logging event
  %L:Used to output the line number from where the logging request was issued.
  %p:Used to output the priority of the logging event.
  %n:Outputs the platform dependent line separator character or characters.
  %r:Used to output the number of milliseconds elapsed since the start of the application until the creation of the logging event.
  %F:Used to output the file name where the logging request was issued.
  %d:Used to output the date of the logging event.
  %c:Used to output the category of the logging event
  %C:Used to output the fully qualified class name of the caller issuing the logging request
  5.如果是对于效率要求比较高的话,要在log.debug()之前加上log.isDebugEnabled()进行判断,这样能够大大减少执行时间
  6.对于各个appenders,共有的属性是layout(一般设置为org.apache.log4j.PatternLayout),Threshold(Log的级别)
  (1)ConsoleAppender:Target(System.out和System.err)
  (2)FileAppender:File(定义输出的文件名),Append(定义是否为追加)
  (3)DailyRollingFileAppender(除FileAppender属性外):MaxFileSize(最大文件大小),MaxBackupIndex()

以上来自http://www.wangchao.net.cn/bbsdetail_64587.html

二。详细配置log4j.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration threshold="debug"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="console.CONSOLE"
  class="org.apache.log4j.ConsoleAppender">
  <param name="threshold" value="debug" />
  <layout class="org.apache.log4j.PatternLayout">
  <!--
   <param name="ConversionPattern" value="%d{HH:mm:ss}[%C.%M(%L)-%p] %m%n"/>
  -->
  <param name="ConversionPattern" value="%d{HH:mm:ss}[%C(%L)-%p] %m%n"/>
  </layout>
</appender>

<appender name="file.text.SYSFILE"
  class="org.apache.log4j.RollingFileAppender">
  <param name="threshold" value="error" />
  <param name="file" value="/juyee.log" />
  <param name="maxFileSize" value="2MB" />
  <param name="maxBackupIndex" value="5" />
  <param name="append" value="true" />
  <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="file.text.DATE_FILE"
  class="org.apache.log4j.DailyRollingFileAppender">
  <param name="threshold" value="debug" />
  <param name="file" value="/juyee.log" />
  <param name="append" value="true" />
  <param name="datePattern" value="'.'yyyy-MM-dd" />
  <!-- each hour
   <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
  -->
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d %-5p [%c] %m%n" />
  </layout>
</appender>
<appender name="file.text.APPFILE"
  class="org.apache.log4j.RollingFileAppender">
  <param name="threshold" value="error" />
  <param name="file" value="${webapp.root}/WEB-INF/logs/juyee.log" />
  <param name="maxFileSize" value="1024KB" />
  <param name="maxBackupIndex" value="5" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d %-5p %c - %m [%t] (%F:%L)%n" />
  </layout>
</appender>
<appender name="file.html.HTML"
  class="org.apache.log4j.RollingFileAppender">
  <param name="threshold" value="error" />
  <param name="file" value="/juyee-log.html" />
  <param name="maxFileSize" value="1024KB" />
  <param name="maxBackupIndex" value="5" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.HTMLLayout" />
</appender>
<appender name="file.xml.XML"
  class="org.apache.log4j.RollingFileAppender">
  <param name="threshold" value="error" />
  <param name="file" value="/juyee-log.xml" />
  <param name="maxFileSize" value="1024KB" />
  <param name="maxBackupIndex" value="5" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.xml.XMLLayout" />
</appender>

  <appender name="mail.MAIL" class="org.apache.log4j.net.SMTPAppender">
  <param name="threshold" value="debug"/>
            <!--
        <param name="threshold" value="fatal"/>
        -->
  <param name="BufferSize" value="10"/>
  <param name="From" value="yangguanjun@justonetech.com"/>
  <param name="SMTPHost" value="www.justonetech.com"/>
  <param name="Subject" value="juyee-log4jMessage"/>
  <param name="To" value="yangguanjun@justonetech.com"/>
  <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="remote.CHAINSAW"
  class="org.apache.log4j.net.SocketAppender">
  <param name="threshold" value="fatal" />
  <param name="remoteHost" value="localhost" />
  <param name="port" value="4445" />
  <param name="locationInfo" value="true" />
</appender>

<category name="com.juyee" additivity="true">
  <priority value="info" />
  <appender-ref ref="console.CONSOLE" />
  <!--
  <appender-ref ref="file.text.DATE_FILE" />
  -->
</category>

<category name="com.co" additivity="true">
  <priority value="debug" />
  <appender-ref ref="console.CONSOLE" />
  <appender-ref ref="file.text.DATE_FILE" />
</category>

<category name="org" additivity="true">
  <priority value="WARN" />
  <appender-ref ref="console.CONSOLE" />
  <!--
  <appender-ref ref="file.text.DATE_FILE" />
  -->
</category>

<root>
  <!--
   <level value="trace" />
   <level value="debug" />
   <level value="info" />
   <level value="warn" />
   <level value="error" />
   <level value="fatal" />
   <appender-ref ref="console.CONSOLE" />
   <appender-ref   ref="file.text.DATE_FILE"/>
  -->
</root>
</log4j:configuration>

Refer : http://dolphin-ygj.javaeye.com/blog/312504

三。加载log4j.xml

Refer : http://smilecertian.blog.sohu.com/52335422.html

posted on 2009-01-20 09:12 nieyong 阅读(6400) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

公告

所有文章皆为原创,若转载请标明出处,谢谢~

新浪微博,欢迎关注:

导航

<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(58)

随笔分类(130)

随笔档案(151)

个人收藏

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜