maven-log4j2配置

1. maven项目添加依赖

 
  
    org.apache.logging.log4j
    log4j-api
    2.12.0
 

 
    org.apache.logging.log4j
    log4j-core
    2.12.0
 

 
2.  添加log4j2配置文件,目录如下图
maven-log4j2配置_第1张图片
3. 配置文件名log4j2.xml,内容如下


 
   
     
   

 

 
   
     
   

 

 
4. 测试配置和打印结果5. 5. 5.打开房间的555级5 maven-log4j2配置_第2张图片
5. 带参数的打印(参考官方文档写法即可,亲测有效)

Substituting Parameters

Frequently the purpose of logging is to provide information about what is happening in the system, which requires including information about the objects being manipulated. In Log4j 1.x this could be accomplished by doing:

  1. if (logger.isDebugEnabled()) {
  2. logger.debug("Logging in user " + user.getName() + " with birthday " + user.getBirthdayCalendar()); //这种写法类似于system.out
  3. }

Doing this repeatedly has the effect of making the code feel like it is more about logging than the actual task at hand. In addition, it results in the logging level being checked twice; once on the call to isDebugEnabled and once on the debug method. A better alternative would be:

logger.debug("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar()); //官方介绍推荐使用这种写法

With the code above the logging level will only be checked once and the String construction will only occur when debug logging is enabled.

6.参考官方文档连接:
https://logging.apache.org/log4j/2.x/manual/configuration.html
https://logging.apache.org/log4j/2.x/manual/api.html 

你可能感兴趣的:(maven-log4j2配置)