gpt4 book ai didi

java - 动态 Log4j2 日志文件路径

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:41 25 4
gpt4 key购买 nike

我想在Log4j2.xml动态设置一个目录。

Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>    
<Configuration status="WARN">
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

<RollingFile name="MyFile" fileName="${sys:logDirectory}/app.log"
filePattern="${sys:logDirectory}/app-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="2000"/>
</RollingFile>

</Appenders>

<Loggers>
<Root level="DEBUG">
<appender-ref ref="Console" level="INFO"/>
<appender-ref ref="MyFile" level="ERROR"/>
</Root>
</Loggers>
</Configuration>

运行:

public static void main(final String[] args) 
{
System.setProperty("log4j.logDirectory", "C:\\debug.log");
org.apache.logging.log4j.core.LoggerContext ctx =
(org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
ctx.reconfigure();
log.error("TEST");
}

输出:

2017-03-15 18:20:27,131 main ERROR
Unable to create file ${sys:logDirectory}/app.log java.io.IOException:
The syntax for the filename, directory name or drive name is wrong.
--- EDIT: the original error message in german was: ---
(Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch.)
...

最佳答案

我尝试过,但我发现如果我这样做,它对我有用;System.setProperty("logDirectory", "C:\debug.log");

所以尝试删除log4j。在你的配置语句中。System.setProperty("log4j.logDirectory", "C:\debug.log");

关于java - 动态 Log4j2 日志文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com