zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    ssl双向认证
    keycloak管理用户权限
    Apollo单向SSL认证(2)
    Apollo单向SSL认证(1)
    apollo1.7.1初探(二)使用apollo订阅主题,发布主题消息
    apollo1.7.1初探(一)安装apollo、创建并启动broker
    Mosquito集群模式
    什么是MQTT协议?
    物影子操作
    kafka和mqtt的区别是什么?
  • 原文地址:https://www.cnblogs.com/live-for-learning/p/11080048.html
Copyright © 2011-2022 走看看