gpt4 book ai didi

tomcat - log4j2 为 FileAppender 指定 tomcat 主目录的相对路径

转载 作者:行者123 更新时间:2023-11-28 21:43:42 25 4
gpt4 key购买 nike

我正在尝试为本地运行的 tomcat 服务器配置 log4j2。我无法将日志传输到与 tomcat 安装相关的位置。

如果我指定一个绝对路径,它就可以工作。如果我使用相对路径,它会输出到 System.getProperty("user.dir") 的位置。

但是当我尝试使用 ${catalina.home}${catalina.base} 时,它不起作用。如果我在启动时输出这两个系统属性,它们将被定义并指向正确的位置。

此外,

  • Tomcat 7.0.26
  • servlet 2.5
  • log4j2.0 测试版 8
  • 我们使用的是 slf4j,后面是 log4j2。
  • 我通过环境变量 -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector 将所有记录器设置为异步。

我的感觉是Tomcat在处理log4j2.xml的时候可能没有完全初始化?任何想法将不胜感激!

这是我的log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug" name="LoggingConfig">
<appenders>
<FastFile name="ALog" fileName="${catalina.home}/logs/test.log" immediateFlush="true" append="true">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
</FastFile>
</appenders>

<loggers>
<logger name="a.namespace.dir" level="info" additivity="false">
<appender-ref ref="ALog"/>
</logger>

<root level="info">
<appender-ref ref="ALog"/>
</root>
</loggers>

</configuration>

来自 catalina.out

2013-07-31 11:22:00,313 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.FastFileAppender for element FastFile with params(fileName="${catalina.home}/logs/test.log", append="true", name="ALog", immediateFlush="true", suppressExceptions="null", PatternLayout(%d %p %c{1.} [%t] %m%n), null, advertise="null", advertiseURI="null", Configuration(LoggingConfig))

2013-07-31 11:22:00,315 DEBUG Starting FastFileManager ${catalina.home}/logs/test.log

2013-07-31 11:22:00,316 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element appenders with params(appenders={ALog})

2013-07-31 11:22:00,317 DEBUG Generated plugins in 0.000016000 seconds

2013-07-31 11:22:00,318 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="ALog", level="null", null)

2013-07-31 11:22:00,320 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element logger with params(additivity="false", level="info", name="a.namespace.dir", includeLocation="null", appender-ref={org.apache.logging.log4j.core.config.AppenderRef@6e8ef177}, properties={}, Configuration(LoggingConfig), null)

2013-07-31 11:22:00,321 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="ALog", level="null", null)

2013-07-31 11:22:00,322 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element root with params(additivity="null", level="info", includeLocation="null", appender-ref={org.apache.logging.log4j.core.config.AppenderRef@6a8ef455}, properties={}, Configuration(LoggingConfig), null)

2013-07-31 11:22:00,323 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPlugin for element loggers with params(loggers={a.namespace.dir, root})

最佳答案

我解决了我自己的问题。对于系统属性,您需要在变量前加上 sys:

<FastFile name="ALog" fileName="${sys:catalina.home}/logs/test.log" immediateFlush="true" append="true">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
</FastFile>

重新阅读这部分文档后发现: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

关于tomcat - log4j2 为 FileAppender 指定 tomcat 主目录的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17978126/

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