我正在尝试为本地运行的 tomcat 服务器配置 log4j2。我无法将日志传送到与 tomcat 安装相关的位置。
如果我指定一个绝对路径,它就可以工作。如果我使用相对路径,它会输出到System.getProperty("user.dir")
.
但是,当我尝试使用其中一个${catalina.home}
或${catalina.base}
它不起作用时。如果我在启动时输出这两个系统属性,它们会被定义并指向正确的位置。
还,
- 雄猫 7.0.26
- 小服务程序 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})