Unable to separate log file on Log4J2

纵然是瞬间 提交于 2021-02-11 17:22:19

问题


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)

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>

回答1:


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>

Source code

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");
    }

}

output : testFile1.log

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

output : testFile2.log

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



回答2:


Finally found the solution. I hope it will be useful .

  <Routing name="RoutingAppender">
        <Routes pattern="$${ctx:RequestId}">
            <Route>
                <RollingFile name="Rolling-${ctx:RequestId}" fileName="C:\XXX\a58086\XXXX\logs\${ctx:RequestId}.log"
                             filePattern="C:\XXXX\XXXX\Desktop\logs\$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
                    <PatternLayout pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"/>
                    <SizeBasedTriggeringPolicy size="512" />
                </RollingFile>
            </Route>

            <Route ref="Console" key="${ctx:RequestId}"/>
        </Routes>
    </Routing>


来源:https://stackoverflow.com/questions/61581068/unable-to-separate-log-file-on-log4j2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!