简体   繁体   English

Log4j2记录到文件和IDE中,但不是从CLI运行时

[英]Log4j2 logs to file and in IDE but not when run from CLI

I usually try to figure out things on my own and it works most of the time, but I am stuck with this one so please help. 我通常会尝试自己弄清楚它并且大部分时间都能正常工作,但我仍然坚持这个,所以请帮忙。 Which seemed like an easy thing ended up being a huge headache. 这似乎是一件容易的事情,最终令人头疼。 So the problem is that there no line of log being outputted to Console when running mvn test from the CLI. 所以问题是当从CLI运行mvn测试时,没有任何日志输出到Console。 Things work fine when I run my tests from IDE and also writing logs to file works in both scenarios. 当我从IDE运行我的测试并且在两种情况下都将日志写入文件时,工作正常。 Tried different things, but always with the same result Below is my log4j config file: 尝试了不同的东西,但总是有相同的结果下面是我的log4j配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="MainConfiguration">
<Properties>
    <Property name="logFileDirectory">target/logs</Property>
</Properties>

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} [%t - %tid] %-5level %logger{36} - %msg%n"/>
    </Console>

    <RollingFile name="File" fileName="${logFileDirectory}/test.log"
                 filePattern="${logFileDirectory}/archive/logs/test_%d{yyyy-MM-dd_HH}.log">
        <PatternLayout>
            <pattern>%d{dd/MM/yyyy HH:mm:ss} %-5p [%t - %tid] [%c{1}] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="10 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="20"/>
    </RollingFile>
</Appenders>

<Loggers>
    <Root level="debug">
        <AppenderRef ref="File"/>
        <AppenderRef ref="Console"/>
    </Root>
</Loggers>

Update: The lo4j2.xml is located in src/main/resources. 更新:lo4j2.xml位于src / main / resources中。 Tried to add a log4j2-test.xml in src/test/resources, but the result did not change. 试图在src / test / resources中添加log4j2-test.xml,但结果没有改变。 Also, all of my code is test related so it's in src/test/java. 此外,我的所有代码都与测试相关,因此它位于src / test / java中。 Just in case, here are the two dependencies in my pom: 以防万一,这是我的pom中的两个依赖项:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.1</version>
    </dependency>

Thanks! 谢谢!

Ok, I feel dumb, but anyways... After going over and over the pom and config files, realized that the maven surefire plugin was configured to redirect test output to file. 好吧,我感到愚蠢,但无论如何......经过pom和配置文件后,意识到maven surefire插件已配置为将测试输出重定向到文件。 My only consolation for missing such an obvious thing is that I did not write this myself. 我错过这么明显的事情的唯一安慰是我自己没有写这个。 But still, so much wasted time... Here's what was wrong: 但是,仍然浪费了很多时间......这就是错误:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <parallel>classes</parallel>
                <threadCount>10</threadCount>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
            </configuration>
        </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM