log4j配置(1)

一、log4j的组成:Loggers, Appenders and Layouts
Log4j has three main components: loggers,appenders andlayouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

log4j主要由三部分组成:loggers,appenders,layouts

    (1)Loggers

      Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule

      logger的命名,区分大小写,并且遵循层级命名规则。例如,logger A 名为"com.foo",logger B 名为"com.foo.Bar",那么A 是B的父亲

     The root logger resides at the top of the logger hierarchy.Invoking the class staticLogger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class staticLogger.getLogger method.

    rootLogger位于最顶层,并且可通过静态方法Logger.getRootLogger获得。

    Loggers may be assigned levels. The set of possible levels, that isTRACE,DEBUG,INFO,WARN,ERROR andFATAL are defined in theorg.apache.log4j.Level class.If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level

    loggers的记录级别:TRACE,DEBUG,INFO,WARN, ERROR and FATAL 。记录级别具有继承性,例如logger A 未定义记录级别,则会找其父类记录级别

     (2)Appenders and Layouts

      Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called anappender. Currently, appenders exist for theconsole,files, GUI components,remote socket servers,JMS,NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously.

     log4j允许将日志输出 到过个 destination。例如:console,files。。。。



     The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy.

     日志输出目的地地 具有 累加性,即日志会输出到logger对象指定的所有输出destination和该logger对象祖先指定的输出destination   


log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender -------这句话的意思是定义CONSOLE为org.apache.log4j.ConsoleAppender(log4j的控制台输出),Log4j提供的appender有以下几种:

org.apache.log4j.ConsoleAppender(控制台)

org.apache.log4j.FileAppender(文件)

org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)

org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)

org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)




     More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating alayout with an appender.

     可通过layout指定日志输出的样式



log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout -------定义log4j使用的输出布局(输出格式)。

log4j提供以下4种布局样式:

org.apache.log4j.HTMLLayout(以HTML表格形式布局)

org.apache.log4j.PatternLayout(可以灵活地指定布局模式,就是可以自定义输出样式),

org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),

org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)



log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n -------这个就是针对PatternLayou你自定义的输出格式,重点讲解一下打印参数, Log4J采用的是类似C语言中的printf函数的打印格式格式化日志信息的

%m 输出代码中指定的消息

  %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL

  %r 输出自应用启动到输出该log信息耗费的毫秒数

  %c 输出所属的类目,通常就是所在类的全名

  %t 输出产生该日志事件的线程名

  %n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”,也就是一跳消息占用一行

  %d 输出日志时间点的日期或时间,紧跟一对花括号进行自定义格式

       %c 输出所属的类目,通常就是所在类的全名

       %l 精确到行

       %x 输出对齐




     As a side note, let me mention that in log4j child loggers link only to their existing ancestors. In particular, the logger namedcom.foo.Bar is linked directly to theroot logger, thereby circumventing the unusedcom or com.foo loggers. This significantly increases performance and reduces log4j's memory footprint.

     child loggers 使用共有的祖先,这样可以明显提高性能

   

     log4j配置实例1:


[html] view plaincopy
log4j.rootLogger=debug, stdout, R 
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
 
# Pattern to output the caller's file name and line number. 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 
 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=example.log 
 
log4j.appender.R.MaxFileSize=100KB 
# Keep one backup file 
log4j.appender.R.MaxBackupIndex=1 
 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
     log4j配置实例2


[html] view plaincopy
log4j.rootLogger=DEBUG, CONSOLE, FILE 
## for console 
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n 
## for file 
log4j.appender.FILE=org.apache.log4j.RollingFileAppender 
log4j.appender.FILE.File=D:/logs/log4j.log 
log4j.appender.FILE.MaxFileSize=1MB 
log4j.appender.FILE.Append = true 
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH\:mm\:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n 

猜你喜欢

转载自currentj.iteye.com/blog/2269016