无法使用log4j2创建日志文件

时间:2018-02-28 09:21:17

标签: java log4j2

我对log4j2比较新。我正在尝试使用log4j2创建一个日志文件,但它没有被创建。任何帮助表示赞赏。提前谢谢。

log4j.xml配置文件(如下):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
    <Properties>
        <Property name="basePath">../resources/logs</Property>
    </Properties>
    <Appenders>
        <!-- File Appender -->
        <File name="FILE" fileName="${basePath}/logfile.log" append="true">
            <PatternLayout
                pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
        </File>
        <!-- Console Appender -->
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout
                pattern="[%-5p][%d{dd-MMM-yyyy HH:mm:ss a}] %C{0}.%M:(%L) - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="log" level="all" />
        <Root level="all">
            <AppenderRef ref="FILE" level="all" />
            <AppenderRef ref="STDOUT" level="all" />
        </Root>
    </Loggers>
</Configuration>

项目文件夹结构(下方):

enter image description here

我的Java代码(下):

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

public class Demo {

    public static Logger log;

    public static void main(String[] args) {
        System.err.println("Start");

        System.setProperty("log4j.configurationFile", "./resources/config/logger/log4j2.xml");
        log = LogManager.getLogger(Demo.class.getName());

        System.err.println("Initialisied Logger");
        log.trace("TRACE");
        log.debug("DEBUG");
        log.info("INFO");
        log.warn("WARN");
        log.error("ERROR");
        log.fatal("FATAL");

        System.err.println("END");
    }

}

Maven依赖关系(下方):

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

控制台外出(下方):

Start
Initialisied Logger
[TRACE][28-Feb-2018 14:45:11 PM] Demo.main:(17) - TRACE
[DEBUG][28-Feb-2018 14:45:11 PM] Demo.main:(18) - DEBUG
[INFO ][28-Feb-2018 14:45:11 PM] Demo.main:(19) - INFO
[WARN ][28-Feb-2018 14:45:11 PM] Demo.main:(20) - WARN
[ERROR][28-Feb-2018 14:45:11 PM] Demo.main:(21) - ERROR
[FATAL][28-Feb-2018 14:45:11 PM] Demo.main:(22) - FATAL
END

2 个答案:

答案 0 :(得分:0)

有类似的问题。只是向您展示一个正常工作的代码会更容易,我将在下面向您提供;)

<强>的pom.xml

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

    <resources>

        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}</targetPath>
            <includes>
                <include>log4j2.xml</include>
            </includes>
        </resource>

    </resources>

<强> Demo.java

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

public class Demo {

    public static Logger log;

    public static void main(String[] args) {
        System.err.println("Start");

private static final Logger log = LogManager.getLogger(Demo.class);

        System.err.println("Initialisied Logger");
        log.trace("TRACE");
        log.debug("DEBUG");
        log.info("INFO");
        log.warn("WARN");
        log.error("ERROR");
        log.fatal("FATAL");

        System.err.println("END");
    }

}

最好将xml日志文件复制到src/main/resources中。 log4j 2 - configuration issue

(我只是随机提出一个关于log4j2的问题,因为有数千个,这会帮助你解决,但我相信我发布的代码也会对你有帮助。)

<强>更新 我忘了粘贴正常工作的log4j2.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />


        <RollingFile name="RollingFile" filename="/var/log/mylog.log"
            filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">
            <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>
</Configuration>

更新2: 而且,既然每个人都喜欢截图,那么你就拥有了这个文件夹结构。 Netbeans,Eclipse ...名称可能会更改,但src/main/resourcespom.xml在所有IDE中都是相同的。 Screenshot-log4j2

答案 1 :(得分:0)

通过将两个点替换为一个点来解决它,即../resources./resources我知道这是一个愚蠢的错误。 : - )

<Properties>
        <Property name="basePath">../resources/logs</Property>
</Properties>

感谢@vikingsteve在评论中指出它。