无法在Log4J2上分离日志文件。

huangapple go评论86阅读模式
英文:

Unable to separate log file on Log4J2

问题

以下是翻译好的内容:

我想要根据我提供的RequestId来分离日志,每个线程一个日志文件。
然而,有一个我不知道的问题。也许,我犯了错误。

无法创建文件 C:\XXXX\XXXXXX\XXXX\logs${ctx:RequestId}.log,java.io.IOException: 文件名、目录名或卷标语法不正确
在 java.io.WinNTFileSystem.canonicalize0(Native Method)
在 java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:428)
在 java.io.File.getCanonicalPath(File.java:618)
在 java.io.File.getCanonicalFile(File.java:643)
在 org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:142)

这是我的 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>
英文:

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

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

答案1

得分: 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>

源代码:

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

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

输出:testFile2.log

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

log4j2.xml

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

    &lt;Loggers&gt;
        &lt;Root level=&quot;all&quot;&gt;
            &lt;AppenderRef ref=&quot;RoutingAppender&quot; /&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;

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(&quot;logFileName&quot;, &quot;testFile1&quot; );
		logger.info(&quot;log printed! - testFile1&quot;);

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

		ThreadContext.remove(&quot;logFileName&quot;);
	}

}

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

得分: 0

最终找到了解决方案。希望它会有用。

<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>
英文:

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

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

            &lt;Route ref=&quot;Console&quot; key=&quot;${ctx:RequestId}&quot;/&gt;
        &lt;/Routes&gt;
    &lt;/Routing&gt;

huangapple
  • 本文由 发表于 2020年5月4日 04:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61581068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定