Log4J2基本配置

 

[1]. Log4J2入门:

 

<1>. 导入Jar包:
log4j-api-2.0-beta9.jar
log4j-core-2.0-beta9.jar

 

<2>. 编写代码:

//注意导包:log4j.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HelloWorld {
    private static Logger logger = LogManager.getLogger(HelloWorld.class.getName());
   
    public static void main(String[] args) {
        logger.info("Hello World !");
        logger.error("Hello World !");    //默认优先级为error(即仅打印ERROR级别的日志信息).
    }
}

 

[2]. 配置文件:

 

<1>. 默认的配置文件如下.(src下文件名:log4j2.xml)
 
 
     
         
        
     
 

# Java:

public class Tip {
    private static Logger logger = LogManager.getLogger(Tip.class.getName());
   
    public boolean showTip() {
        logger.entry();    //    日志(Log)进入一个方法.
        logger.error("Somethind wrong !");    //ERROR级别的日志.(默认仅打印ERROR级别)
       
        //可以被替换做...
        return logger.exit(false);   
    }
}

public class TestTip {
    private static Logger logger = LogManager.getLogger(TestTip.class.getName());
    public static void main(String[] args) {
        //显式地跟踪程序的执行流程.
        logger.trace("Enter app ...");   
        Tip tip = new Tip();
        if(!tip.showTip()) {
            logger.error("Didn't do it !");
        }
        logger.trace("Exit app ...");
       
    }
}

 

<3>.

 
 
     
       

你可能感兴趣的:(Log4j2相关)