Log4j2模式布局+转换模式下的负数

时间:2016-11-28 16:24:56

标签: java log4j2

我正在使用 log4j2 2.5 并尝试更改模式布局以缩短包输出。

我的包命名模式类似于com.mycompany.myproject.myapp。使用%C%c格式选项,我尝试删除包路径(com.mycompany.myproject)的前三个组件,并保持原样。

根据log4j 2.x manual,使用否定号时:

  

如果为负,则布局会删除最左边的相应数字   记录器名称组件。

所以我的PatternLayout包含%C{-3}

<PatternLayout pattern="%d %-5p [%t] [%C{-3}] (%F:%M:%L) - %m%n"/>

不幸的是,我获得的输出充满了.-,而不是删除了包结构的前3个部分。输出-.-.-.-.MyClass

更新:添加log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] [%c{-3}] (%F:%M:%L) - %m%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
  </Loggers>
</Configuration>

更新2 :添加了虚拟主类,pom.xml和输出

Main.java

package com.example.test.log4jtester;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {

    private static final Logger LOGGER = LogManager.getLogger(Main.class.getName());


    public static void main(String[] args){                              

        LOGGER.info("java.vendor: {} ",System.getProperties().getProperty("java.vendor"));
        LOGGER.info("java.version: {} ",System.getProperties().getProperty("java.version"));
        LOGGER.info("os.arch: {} ",System.getProperties().getProperty("os.arch"));
        LOGGER.info("os.name: {} ",System.getProperties().getProperty("os.name"));
        LOGGER.info("os.version: {} ",System.getProperties().getProperty("os.version"));

    }
}

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.test</groupId>
    <artifactId>Log4jTester</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

输出

--- exec-maven-plugin:1.2.1:exec (default-cli) @ Log4jTester ---
2016-11-30 13:17:21,606 INFO  [main] [-.-.-.-.Main] (Main.java:main:32) - java.vendor: Oracle Corporation 
2016-11-30 13:17:21,607 INFO  [main] [-.-.-.-.Main] (Main.java:main:33) - java.version: 1.8.0_60 
2016-11-30 13:17:21,607 INFO  [main] [-.-.-.-.Main] (Main.java:main:34) - os.arch: amd64 
2016-11-30 13:17:21,607 INFO  [main] [-.-.-.-.Main] (Main.java:main:35) - os.name: Windows 10 
2016-11-30 13:17:21,607 INFO  [main] [-.-.-.-.Main] (Main.java:main:36) - os.version: 10.0

0 个答案:

没有答案