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 
  • 相关阅读:
    UIWebView 本地缓存
    基于x86 Android ndk应用移植指南(android ndk 生成libs/x86)
    安装android的IntelHaxm.exe时出错的问题
    使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决
    犯傻似得的开发
    关于在win8下开发c或者c++时,某些特殊情况
    android 图片加载过多崩溃
    辞职的三十八个理由
    android程序的代码混淆打包
    几条常见的数据库建表id增长sql语句
  • 原文地址:https://www.cnblogs.com/live-for-learning/p/11080048.html
Copyright © 2011-2022 走看看