zoukankan      html  css  js  c++  java
  • log4j

    Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

    Log4j is a popular logging package written in Java. One of its distinctive features is the notion of inheritance in loggers. Using a logger hierarchy it is possible to control which log statements are output at arbitrary granularity. This helps reduce the volume of logged output and minimize the cost of logging.

    One of the advantages of the log4j API is its manageability. Once the log statements have been inserted into the code, they can be controlled with configuration files. They can be selectively enabled or disabled, and sent to different and multiple output targets in user-chosen formats. The log4j package is designed so that log statements can remain in shipped code without incurring a heavy performance cost.

    Log4j是一个用Java编写的流行的日志包。它的一个显著特点是继承loggers概念。用 logger层次可以以任意的粒度控制日志语句的输出。这有助于减少日志输出量,并将日志记录的成本将至最低。

    log4j API的优点之一就是它的可管理性。一旦日志语句被插入到代码中,可以用配置文件控制它们。他们可以有选择地启用或禁用,并以用户自定义的格式输出到不同的多个目标。log4j包的设计使日志语句可以保持在(shipped code),而不会产生沉重的性能成本。


    Log4j有三个主要组件:Logger,Appender和Layout。这三种类型的组件一起工作使开发者可以按消息的类别和等级来记录消息,并且在运行时控制这些消息怎么格式化和在哪里输出。

    Logger:处理日志的核心组件。可设置日志的优先级。

    Appender:日志输出目的地。

    Layout:以什么格式输出日志。


    Log4j定义了8个级别的log,优先级从高到低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL,在项目中一般用ERROR、WARN、INFO、DEBUG4个级别。当设定了默一级别时,只有大于等于此级别的log才会被打印出来。

    Level

    OFF

    The OFF has the highest possible rank and is intended to turn off logging.

    拥有最高的级别,用于关闭日志。

    FATAL

    The FATAL level designates very severe error events that will presumably lead the application to abort.

    指出非常严重的错误事件,这个错误事件可能会导致应用程序终止。

    ERROR

    The ERROR level designates error events that might still allow the application to continue running.

    指出发生的错误事件,但这种错误事件并不影响应用程序继续运行。

    WARN

    The WARN level designates potentially harmful situations.

    指明了潜在的有害的情况。

    INFO

    The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

    指出一些消息,这些消息在粗粒度级别上突出应用程序的运行过程。

    DEBUG

    The DEBUG Level designates fine-grained informational events that are most useful to debug an application.

    指出细粒度的信息事件,对调试应用程序非常有用。

    TRACE

    The TRACE Level designates finer-grained informational events than the DEBUG.

    跟踪级别指定的细粒度信息的事件比调试。

    指出比DEBUG粒度更细的信息事件。

    ALL

    The ALL has the lowest possible rank and is intended to turn on all logging.

    拥有最低的级别,用于打开日志。

    Appender

    org.apache.log4j.Appender

    Implement this interface for your own strategies for outputting log statements.

    这是个接口。

    org.apache.log4j.WriterAppender

    WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.

    将日志信息以流格式发送到任意指定的地方。

    org.apache.log4j.ConsoleAppender

    ConsoleAppender appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.

    WriterAppender的子类,日志输出到控制台。默认的target 是 "System.out.",也可指定"System.err"。

    org.apache.log4j.FileAppender

    FileAppender appends log events to a file.

    WriterAppender的子类,日志输出到文件。

    org.apache.log4j.DailyRollingFileAppender

    DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. 

    FileAppender的子类,以用户指定的频率定时产生日志文件。

    org.apache.log4j.RollingFileAppender

    RollingFileAppender extends FileAppender to backup the log files when they reach a certain size. 

    FileAppender的子类,当文件到达指定大小时,产生一个新的文件。

    Layout

    org.apache.log4j.Layout

    Extend this abstract class to create your own log layout format.

    扩展此抽象类以创建自己的日志布局格式。

    org.apache.log4j.helpers.DateLayout

    This abstract layout takes care of all the date related options and formatting work.

    这个抽象布局负责所有与日期相关的选项和格式化工作。

    org.apache.log4j.HTMLLayout

    This layout outputs events in a HTML table. Appenders using this layout should have their encoding set to UTF-8 or UTF-16, otherwise events containing non ASCII characters could result in corrupted log files.

    输出事件在一个HTML表中

    org.apache.log4j.SimpleLayout

    consists of the level of the log statement, followed by " - " and then the log message itself. 

    包含日志信息的级别和信息串,中间横杠连接。(例如:ERROR - this is error)

    org.apache.log4j.PatternLayout

    A flexible layout configurable with pattern string. 

    可以灵活地指定布局模式。

    org.apache.log4j.TTCCLayout

    TTCC layout format consists of time, thread, category and nested diagnostic context information, hence the name.Each of the four fields can be individually enabled or disabled. The time format depends on the DateFormat used.

    DateLayout的子类,TTCC布局包含时间、线程、类别和嵌套诊断上下文信息。四个字段中的每一个都可以单独启用或禁用.。时间格式取决于使用的日期格式。

    org.apache.log4j.XMLLayout

    The output of the XMLLayout consists of a series of log4j:event elements as defined in the log4j.dtd. It does not output a complete well-formed XML file. The output is designed to be included as an external entity in a separate file to form a correct XML file.


    日志信息格式中符号含义: 

    符号 含义 例子
    %d 输出日志时间点的日期或时间,默认格式为ISO8601,可自定义%d{yyyy-MM-dd} 2000-12.12
    %p 输出日志信息优先级 FATAL
    %c 输出日志信息所在类的全名  test.TestClass
    %F  输出日志消息产生时所在的文件名称  TestClass.java
    %L 输出代码中的行号  11
    %l 输出日志事件的发生位置 test.TestClass.test(TestClass.java:11)
    %m 输出代码中指定的消息,产生的日志具体信息 this is fatal
    %t 输出产生该日志事件的线程名  main
    %r 输出自应用启动到输出该log信息耗费的毫秒数  1
    %x 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。   
    %n  输出一个回车换行符  
    %% 输出一个"%"字符   

    可以在%与模式字符之间加上修饰符来控制其最小宽度、最大宽度、和对齐方式。如输出日志信息优先级为ERROR和WARN: 

    1、[%p]:[ERROR][WARN]

    2、[%5p]:右对齐,最小宽度是5,小于5时从左补空格;[ERROR][ WARN]

    3、[%-5p]:左对齐,最小宽度是5,小于5时从右补空格;[ERROR][WARN ]

    4、[%.4p]:右对齐,最大宽度是3,大于4时从左将多余字符去掉;[RROR][WARN]

    5、[%5.4p]:右对齐,当小于5时从左补空格,当大于4时从左将多余字符去掉;[RROR][ WARN]


    时间格式:

    DatePattern Rollover schedule
    yyyy-MM 每月
    yyyy-ww 每周
    yyyy-MM-dd 每天 
    'yyyy-MM-dd-a 每天两次 
    yyyy-MM-dd-HH 每小时 
    yyyy-MM-dd-HH-mm 每分钟 

     Log4J配置文件的基本格式如下:

    #配置根Logger
    log4j.rootLogger = [ level ] , appenderName1 , appenderName2 ,   …
    
    #配置日志信息输出目的地Appender
    log4j.appender.appenderName =   fully.qualified.name.of.appender.class 
    log4j.appender.appenderName.option1 =   value1 
    … 
    log4j.appender.appenderName.optionN =   valueN 
    
    #配置日志信息的格式(布局)
    log4j.appender.appenderName.layout =   fully.qualified.name.of.layout.class 
    log4j.appender.appenderName.layout.option1 =   value1 
    … 
    log4j.appender.appenderName.layout.optionN = valueN

    简单例子:

    log4j.rootLogger = warn,stdout
    #输出到控制台
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.SimpleLayout

    参考文档:

    log4j配置详解(非常详细)

    log4j配置信息详解

    官方文档

  • 相关阅读:
    暑期集训第五天(6-26)题解及总结
    暑期集训第四天(6-25)题解及总结
    暑期集训第三天(6-24)题解及总结
    暑期集训第二天(6-23)题解及总结
    暑期集训第一天(6-22)题解及总结(于6-23补)
    51nod1534 棋子游戏
    CF938D Buy a Ticket
    P4588 [TJOI2018]数学计算
    yumdownloader:下载保存Yum包而不安装
    RHEL 7.4 下yum安装配置nginx与php
  • 原文地址:https://www.cnblogs.com/SQP51312/p/6232402.html
Copyright © 2011-2022 走看看