Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j referring to external properties file in class path

Tags:

log4j

I have a log4j.properties file

    log4j.appender.BigBrotherLog=org.apache.log4j.RollingFileAppender
    log4j.appender.BigBrotherLog.File=${userprofile.broker.bigbrother.log4j.file.path}
    log4j.appender.BigBrotherLog.MaxFileSize=100MB
    log4j.appender.BigBrotherLog.MaxBackupIndex=10
    log4j.appender.BigBrotherLog.layout=org.apache.log4j.PatternLayout
    log4j.appender.BigBrotherLog.layout.ConversionPattern=%d{yy/MM/dd} %d{HH:mm:ss} ALARM CRITICAL SITA ESB (SOAESB) [%-t] (%F:%L) %-5p %-c{1} %x- %m%n
    log4j.appender.BigBrotherLog.Threshold=FATAL

where I'm passing ${userprofile.broker.bigbrother.log4j.file.path} from a external properties file. But I want to have put this external properties file in the class path. How can I make it work? Thanks.

like image 797
Ravi Avatar asked Aug 15 '11 18:08

Ravi


People also ask

Where does log4j look for properties file?

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.

Does log4j need a properties file?

It's optional if the file name is log4j. properties and it's in the project classpath. We have to configure it before using the logger. Here is a simple program showing how to configure and use log4j logging.

How do I put log4j jar in my class path?

Add log4j jar file to classpath of the project. In the Java Build Path dialogue, go to Libraries tab and Click on Add External JARs button and add the log4j-xxx. jar file by browsing your local drive where log4j jar file is extracted and Click OK. Now the log4j jar file is available to be used in your application.


1 Answers

When you invoke the java command, pass in the system property -Dlog4j.configuration=file:[path-to-your-external-file]. The important part is the file:, otherwise log4j will only attempt to load from the class path and system resources.

You can also use -Dlog4j.debug=true to see where log4j has attempted to source it's configuration file from.

like image 191
WLPhoenix Avatar answered Sep 28 '22 14:09

WLPhoenix