Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How do I specify the location of log4j.log?

807589Sep 18 2008 — edited Sep 18 2008
I want to create the log4j.log file in "C:\TEMP\" for Windows and "/tmp/" for linux. Is there anyway that I can do to that?
Here is what I currently have for my log4j.properties:
# SET ROOT CATEGORY PRIORITY TO DEBUG AND ONLY APPENDER TO A1, C1, R
log4j.rootCategory=DEBUG, C1, R

log4j.appender.C1=org.apache.log4j.ConsoleAppender
log4j.appender.C1.layout=org.apache.log4j.PatternLayout
log4j.appender.C1.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
#Create the file in Windows
#log4j.appender.R.File=C://TEMP//log4j.log
#Create the file in Linux
log4j.appender.R.File=/tmp/log4j.log
log4j.appender.R.MaxFileSize=15000KB
# KEEP FIVE BACKUP FILES
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
#log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
Thank You,
Chris

Comments

807589
Here is another configuration file that uses multiple appenders.
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
In addition, as the root logger has been allocated a second appender, output will also be directed to the example.log file. This file will be rolled over when it reaches 100KB. When roll-over occurs, the old version of example.log is automatically moved to example.log.1.
807589
Where does the log go in Linux if I do this:
log4j.appender.R.File=log4j.log
807589
revchrism wrote:
Where does the log go in Linux if I do this:
log4j.appender.R.File=log4j.log
to the mentioned location i.e. log4j.log
807589
Where is the file going to be located? /, /home, /tmp, the users home directory
625957
Where is the file going to be located? /, /home, /tmp, the users home directory
Why not try it? I vote for none of the above.

You can use java system properties in your log4 properties file: [http://forums.sun.com/thread.jspa?messageID=4445852]
807589
I have some users on Windows and others on Linux, currently the Windows users have no problem with the log4j.log (it's being created in C:\TEMP\). However the Linux users are not able to create the log4j.log because there is no file called C:\TEMP\ on their systems. What I need is a way to have the program see which OS the user has and change the output folder for the log4j.log. Is there anyway of doing that? Can I setup multiple file locations?
625957
From what I understand you have these options:

1) Create 2 builds of your app for different operating systems with different log4j configs
2) Create the logs in a directory relative to your application.
3) Use a java system property in log4j properties file and log to java.io.tempdir for example.

You seem really keen on logging to c:\temp or /tmp, why?
1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 16 2008
Added on Sep 18 2008
7 comments
6,603 views