zoukankan      html  css  js  c++  java
  • jar包中使用log4j2不起作用

    某程序中有使用到log4j2,将该程序打包成jar,使用以下命令执行时,发现log4j不输出

    java -cp Tool.jar com.zhen.nameOnce.Log4jTest

    且报以下错误

    ERROR StatusLogger Unrecognized format specifier [d]
    ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [thread]
    ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [level]
    ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [logger]
    ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [msg]
    ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [n]
    ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
    ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
    ERROR StatusLogger Unrecognized format specifier [d]
    ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [thread]
    ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [level]
    ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [logger]
    ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [msg]
    ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
    ERROR StatusLogger Unrecognized format specifier [n]
    ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.

    原因:因为有添加maven-shade插件,需要做如下修改

    <plugins>
            <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                <!-- 不加这个filter会导致通过java -cp xxx.jar时无日志输出 -->
                  <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>**/Log4j2Plugins.dat</exclude>
                        </excludes>
                    </filter>
                  </filters>
                
                  <artifactSet>
                    <excludes>
                      <exclude>classworlds:classworlds</exclude>
                      <exclude>junit:junit</exclude>
                      <exclude>jmock:*</exclude>
                      <exclude>*:xml-apis</exclude>
                      <exclude>org.apache.maven:lib:tests</exclude>
                      <!-- <exclude>log4j:log4j:jar:</exclude> -->
                      <exclude>META-INF/*.SF</exclude>
                      <exclude>META-INF/*.DSA</exclude>
                      <exclude>META-INF/*.RSA</exclude>
                    </excludes>
                  </artifactSet>
                </configuration>
              </execution>
            </executions>
          </plugin>
          
        </plugins>

    添加filter。重新打包即可。

  • 相关阅读:
    Codeforces Round #627 (Div. 3) 总结
    [IOI1994] 时钟
    收集一些优秀的甲方安全开源项目
    python基础——对时间进行加减
    JSFinder:一个在js文件中提取URL和子域名的脚本
    python对齐输出
    python使用smtplib发送邮件
    任务2:扫描渗透测试(50分)[2019年信息安全管理与评估赛题答案-01]
    记一次Xmrig挖矿木马排查过程
    Bypass xss过滤的测试方法
  • 原文地址:https://www.cnblogs.com/EnzoDin/p/10756664.html
Copyright © 2011-2022 走看看