java 如何配置我的 log4j(使用 Glassfish)以登录到日志目录,而不是在配置中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1405029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure my log4j ( using Glassfish ) to log in the logs directory, not in config?
提问by
I have the following line in my log4j.properties file:
我的 log4j.properties 文件中有以下行:
log4j.appender.logfile.File=MyApplication.log
log4j.appender.logfile.File=MyApplication.log
My log file appears in MyDomain/config directory, but I would like it to land in the MyDomain/logs directory. How can I achieve that? I am not allowed to modify the startserv script.
我的日志文件出现在 MyDomain/config 目录中,但我希望它位于 MyDomain/logs 目录中。我怎样才能做到这一点?我不允许修改 startserv 脚本。
Thanks in advance for your help!
在此先感谢您的帮助!
回答by Herme
In fact, it is log4j who resolves the variable ${catalina.home}, Glassfish declares ${catalina.home} as ${com.sun.aas.instanceRoot} that points to path/to/MyDomain/
实际上,解析变量 ${catalina.home} 的是 log4j,Glassfish 将 ${catalina.home} 声明为指向 path/to/MyDomain/ 的 ${com.sun.aas.instanceRoot}
You can declare any variable in GF environment and put it on log4j.properties, log4j will parse them when log4j is configured.
你可以在 GF 环境中声明任何变量并把它放在 log4j.properties 上,log4j 会在配置 log4j 时解析它们。
That is really useful to set server based logging configuration parameters, using same log4.properties for test and deployment
这对于设置基于服务器的日志配置参数非常有用,使用相同的 log4.properties 进行测试和部署
回答by John Yeary
You can set the appender to use the ${com.sun.aas.instanceRoot}, but unlike some other comments it should be:
您可以将 appender 设置为使用 ${com.sun.aas.instanceRoot},但与其他一些注释不同的是,它应该是:
log4j.appender.logfile.File=${com.sun.aas.instanceRoot}/logs/MyApplication.log
This is the correct location for logs in the ../domains/domain{x}/logs directory.
这是 ../domains/domain{x}/logs 目录中日志的正确位置。
回答by matt b
The following is possible in Tomcat, perhaps Glassfish sets a similar environment variable pointing to it's filesystem location:
在 Tomcat 中可能出现以下情况,也许 Glassfish 设置了一个类似的环境变量指向它的文件系统位置:
log4j.appender.logfile.File=${catalina.home}/logs/MyApplication.log
${catalina.home}is an environment/system property set by Tomcat pointing to it's install directory. Log4j is capable of expanding these, at least in the PropertyConfigurer.
${catalina.home}是 Tomcat 设置的环境/系统属性,指向它的安装目录。Log4j 能够扩展这些,至少在PropertyConfigurer.
回答by stivlo
Since in GlassFish the file lands in config and logs is its sibling, instead of
由于在 GlassFish 中,文件位于 config 中,而日志是它的同级文件,而不是
log4j.appender.logfile.File=MyApplication.log
use
利用
log4j.appender.logfile.File=../logs/MyApplication.log
Tested on GlassFish 3.1.1 and it works.
在 GlassFish 3.1.1 上测试过并且可以正常工作。
回答by Vinay Sajip
You need to specify an absolute path, not a relative one (assuming Unix paths):
您需要指定绝对路径,而不是相对路径(假设为 Unix 路径):
log4j.appender.logfile.File=/path/to/MyDomain/logs/MyApplication.log

