abi1964 abi1964 - 4 months ago 25
Java Question

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

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>___?

Here is my log4j properties file contents.

#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

Answer

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.

Comments