本文介绍了无法在Log4J2上分离日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我作为RequestId给出的每个线程将日志分开.但是,有一个我不知道的问题.也许,我弄错了.

I would like to separate logs with each thread which I gave as RequestId.However, there is a problem I do not know. Maybe, I made mistake/s.

 Unable to create file C:\XXXX\XXXXXX\XXXX\logs\${ctx:RequestId}.log java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.canonicalize0(Native Method)
at java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:428)
at java.io.File.getCanonicalPath(File.java:618)
at java.io.File.getCanonicalFile(File.java:643)
at org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:142)

这是我的log4j.xml

Here is my log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration  status="debug" >
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="" />
        </Console>
        <Routing name="Routing">
            <Routes pattern="$${ctx:RequestId}">
                <Route>
                    <RollingFile  name="Rolling-${ctx:RequestId}" fileName="C:\XXXXX\XXXXXX\Desktop\logs\${ctx:RequestId}.log"
                                  filePattern="C:\XXXXX\XXXXX\Desktop\logs\${ctx:RequestId}.%i.log.gz">
                        <PatternLayout>
                            <pattern></pattern>
                        </PatternLayout>
                        <Policies>
                            <OnStartupTriggeringPolicy />
                        </Policies>
                    </RollingFile>
                </Route>
                <Route key="${ctx:RequestId}" ref="Console" />
            </Routes>
        </Routing>
    </Appenders>
    <Loggers>
        <Logger level="INFO" name="TRY_ME">
            <AppenderRef ref="Routing"/>
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

推荐答案

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Routing name="RoutingAppender">
            <Routes pattern="${ctx:logFileName}">
                <Route>
                    <RollingFile name="Rolling-${ctx:logFileName}"
                                 fileName="./logs/${ctx:logFileName}.log"
                                 filePattern="./logs/${ctx:logFileName}.%i.log.gz">
                        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5level] %logger{36} - %msg%n"/>
                        <SizeBasedTriggeringPolicy size="512" />
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="all">
            <AppenderRef ref="RoutingAppender" />
        </Root>
    </Loggers>
</Configuration>

源代码

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

public class Log4j2Test{
    static Logger logger = LogManager.getLogger(Log4j2Test.class);

    public static void main(String[] args) {
        ThreadContext.put("logFileName", "testFile1" );
        logger.info("log printed! - testFile1");

        ThreadContext.put("logFileName", "testFile2" );
        logger.info("log printed! - testFile2");

        ThreadContext.remove("logFileName");
    }

}

输出:testFile1.log

output : testFile1.log

16:27:51.868 [INFO ] com.test.Log4j2Test- log printed! - testFile1

输出:testFile2.log

output : testFile2.log

16:27:51.897 [INFO ] com.test.Log4j2Test- log printed! - testFile2

这篇关于无法在Log4J2上分离日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:38