英文:
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
<?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
得分: 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
<?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
得分: 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 .
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论