我正在尝试为本地运行的tomcat服务器配置log4j2。我无法将日志传递到与tomcat安装相关的位置。
如果我指定一个绝对路径,它的工作原理。如果我使用相对路径,它会输出到System.getProperty("user.dir")
的位置。
但是,当我尝试使用${catalina.home}
或${catalina.base}
时,它无效。如果我在启动时输出这两个系统属性,则会定义它们并指向正确的位置。
另外,
-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})
答案 0 :(得分:107)
我解决了自己的问题。对于系统属性,您需要使用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
答案 1 :(得分:0)
经过几个小时尝试用Tomcat解决我自己的log4j2文件记录问题后,我发现了另一个可能的原因。希望这可以节省其他人的时间!
我的Tomcat目录位于/Library/Tomcat
。最后,我尝试重新安装Tomcat,这次将工作目录放在/usr/local
中,并带有符号链接到/Library/Tomcat
。这与Moemars建议包括sys
前缀一起解决了我的日志记录问题。