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 
  • 相关阅读:
    PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
    概念:RPG游戏中两个兵种互相攻击的逻辑
    php怎么获取checkbox复选框的内容?
    20150724之问题
    Uploadify 之使用
    oneThink后台添加插件步骤详解
    针对各种浏览器,前端解决方案(持续更新...)
    解决IE8中select下拉列表文字上下不居中的问题
    针对IE6 7 8当独写样式
    document对象详解
  • 原文地址:https://www.cnblogs.com/live-for-learning/p/11080048.html
Copyright © 2011-2022 走看看