zoukankan      html  css  js  c++  java
  • Log4j 2 简介及示例

    1、获得Logger实例

    import org.apache.logging.log4j.Logger;

    Logger logger=LogManager.getLogger( “logger的名字” );

    Logger logger=LogManager.getLogger(HelloWorld.class);

          (1)The LogManager will locate the appropriate LoggerContext and then obtain the Logger from it.

         (2)The logger simply has a name and is associated with a LoggerConfig.

     Logger x = Logger.getLogger("wombat");
     Logger y = Logger.getLogger("wombat");

       (3)通过相同的名字获得对同一个logger对象的引用。

    2、LoggerConfig的层级

         The root LoggerConfig resides at the top of the LoggerConfig hierarchy. it always exists and it is part of every hierarchy. A Logger that is directly linked to the root LoggerConfig. 

         The LoggerConfig named "com.foo" is a parent of the LoggerConfig named"com.foo.Bar".

         层级关系: root  > com.foo (包名)> com.foo.Bar (类名)

    3、Log levels

    每个Logger引用一个合适的LoggerConfig,LoggerConfig也引用了它的父类对象。

    Level of LogEvent的等级(从底到高):TRACE, DEBUG, INFO, WARN, ERROR ,FATAL

    例如:

    Logger Name Assigned LoggerConfig Level
    root root Debug
    x x Error
    x.y x
    Error
    x.y.z x
    Error

    1、the loggers root and X each have a Configured LoggerConfig with the same name. 

    2、The loggers X.Y and X.Y.Z do not have configured LoggerConfigs and so get their Level from the LoggerConfig assigned to them,X, since it is the LoggerConfig whose name has the longest match to the start of the Logger's name.

    4、Appender

    (1) An output destination is called an Appender. 

    (2) Currently, appenders exist for the console, files, remote socket servers, Apache Flume, JMS, and remote UNIX Syslog daemons. More than one Appender can be attached to a Logger.

    (3) Each enabled logging request for a given logger will be forwarded to all the appenders in that Logger's LoggerConfig as well as the Appenders of the LoggerConfig's parents.

    The table below shows an example:

    Logger
    Name
    Added
    Appenders
    Additivity
    Flag
    Output Targets
    root A1 not applicable A1
    x A-x1, A-x2 true A1, A-x1, A-x2
    x.y none true A1, A-x1, A-x2
    x.y.z A-xyz1 true A1, A-x1, A-x2, A-xyz1
    security A-sec false A-sec
    security.access none true A-sec

    5、示例程序

    (1)jar包:log4j-core-2.0-beta4.jar  log4j-api-2.0-beta4.jar

    (2)配置文件:log4j2.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <configuration status="OFF"> 
        <properties> 
            <property name="filename">target/test.log</property> 
        </properties>
    
        <appenders> 
            <Console name="Console" target="SYSTEM_OUT"> 
                <PatternLayout 
                    pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5level - [%logger{1} - %M] %msg%n" /> 
            </Console> 
            <File name="File" fileName="${filename}"> 
                <PatternLayout 
                    pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5level - [%logger{1} - %M] %msg%n" /> 
            </File> 
        </appenders>
    
        <loggers> 
            <root level="trace"> 
                <appender-ref ref="Console" /> 
            </root> 
            <logger name="edu.shao.log4j.sample" level="debug" additivity="true"> 
                <appender-ref ref="File" /> 
            </logger> 
        </loggers>
    
    </configuration>

     (3)程序

    import org.apache.logging.log4j.LogManager; 
    import org.apache.logging.log4j.Logger;
    
    public class HelloWorld { 
        private static Logger logger=LogManager.getLogger(HelloWorld.class); 
        
        public static boolean judge(){ 
            logger.entry(); 
            logger.debug("In doIt()."); 
            logger.info("In doIt().."); 
            logger.warn("In doIt()..."); 
            return logger.exit(true); 
        }
    
        public static void main(String[] args) { 
            logger.trace("begin..."); 
            if (judge()) { 
                logger.error("Do it again!"); 
                logger.fatal("Do it again!!"); 
            } 
            logger.trace("end!"); 
        } 
    } 

    (4)文件和控制台的输出

    2013-03-28 16:48:33,047 DEBUG - [HelloWorld - judge] In doIt().
    2013-03-28 16:48:33,049 INFO  - [HelloWorld - judge] In doIt()..
    2013-03-28 16:48:33,049 WARN  - [HelloWorld - judge] In doIt()...
    2013-03-28 16:48:33,049 ERROR - [HelloWorld - main] Do it again!
    2013-03-28 16:48:33,050 FATAL - [HelloWorld - main] Do it again!!

  • 相关阅读:
    1.8.10- 表单域
    sublime常用快键键
    HTML常用标签
    1.8.9- 下拉菜单
    给定一个文件名,和字符串,统计字符中在文件中出现的次数
    初始easyUI
    关于maven的CoreException: Could not get the value for parameter compilerId for plugin 。。的错误
    1 创建一个存储过程,以及对存储过程的调用 MySQL
    Java 将word转为pdf jacob方式
    数据库查询的数据导出到xls表,集合数据导出到xls表
  • 原文地址:https://www.cnblogs.com/windlaughing/p/2987157.html
Copyright © 2011-2022 走看看