maven-log4j2配置

1. maven项目添加依赖

  <dependencies>
   <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.12.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.12.0</version>
  </dependency>
  </dependencies>
2.  添加log4j2配置文件,目录如下图
3. 配置文件名log4j2.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>
 
4. 测试配置和打印结果5. 5. 5.打开房间的555级5
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 

转载于:https://www.cnblogs.com/live-for-learning/p/11080048.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值