Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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   
like image 494
AabinGunz Avatar asked May 12 '11 05:05

AabinGunz


People also ask

Where does log4j write to?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.

Does log4j create log file?

Each log file is rolled out every day, and the file without date in its name is the current log file. Suppose today is 2012-11-04, and at midnight, log4j will back up the app. logfile into app. log.

Can we placed log4j properties file anywhere in the project?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.


2 Answers

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.

like image 117
Harry Joy Avatar answered Oct 18 '22 18:10

Harry Joy


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

 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 webapps tree because they are liable to be blown away if your webapp is redeployed.

Put them in ${catalina.home}/logs instead.

Or better still, put them in the distro-specific conventional place to put application logfiles; e.g. "/var/spool/..." or "/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.

like image 35
Stephen C Avatar answered Oct 18 '22 19:10

Stephen C