java 如何使用 log4j 在我的项目目录中写入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5973880/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:47:20  来源:igfitidea点击:

how to use log4j to write a file inside my project directory?

javatomcatservletslog4j

提问by AabinGunz

I have a log4j properties file which is creating a file inside my tomcat>bin folder but instead can it write the log file to my project's root dir? webapps>test>___?

我有一个 log4j 属性文件,它在我的 tomcat>bin 文件夹中创建一个文件,但它可以将日志文件写入我的项目的根目录吗?webapps>测试> ___?

Here is my log4j properties file contents.

这是我的 log4j 属性文件内容。

#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender

# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%t %-5p %c{3} - %m%n

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=/test/a.log
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n

# now map our console appender as a root logger, means all log messages will go to this appender
#for console printing
#log4j.rootLogger = DEBUG, consoleAppender   

#for file printing
log4j.rootLogger = DEBUG, rollingFile   

采纳答案by Harry Joy

Try replacing this:

尝试替换这个:

log4j.appender.rollingFile.File=/test/a.log

with this:

有了这个:

log4j.appender.rollingFile.File=../webapps/test/a.log


Note (by Stephen C) - the "../" means this solution depends on whether or not the Tomcat launch mechanism you use makes $CATALINA_HOME the current directory before the JVM that hosts Tomcat. Some do, and some don't.

注意(Stephen C)-“../”表示此解决方案取决于您使用的 Tomcat 启动机制是否使 $CATALINA_HOME 成为托管 Tomcat 的 JVM 之前的当前目录。有些会,有些不会。

回答by Stephen C

The log4j configurations understand "${catalina.home}", so ...

log4j 配置理解"${catalina.home}",所以......

 log4j.appender.rollingFile.File=${catalina.home}/webapps/test/a.log

However, I don't think it is a good idea to put logs into the webappstree because they are liable to be blown away if your webapp is redeployed.

但是,我认为将日志放入webapps树中并不是一个好主意,因为如果您的 web 应用程序被重新部署,它们很可能会被吹走。

Put them in ${catalina.home}/logsinstead.

把它们${catalina.home}/logs放进去。

Or better still, put them in the distro-specific conventionalplace to put application logfiles; e.g. "/var/spool/..." or "/var/log/...".

或者更好的是,将它们放在发行版特定的常规位置以放置应用程序日志文件;例如“/var/spool/...”或“/var/log/...”。

Putting logfiles in standard places means there are less places for someone else (e.g. the guy who is the backup sysadmin when you are on holiday) to investigate if the file system fills up with old logfiles.

将日志文件放在标准位置意味着其他人(例如,在您度假时作为备份系统管理员的人)调查文件系统是否已被旧日志文件填满的位置更少。