File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes log4j use best way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "log4j use best way " Watch "log4j use best way " New topic

log4j use best way

shankha bhattacharya

Joined: Apr 20, 2007
Posts: 12
How can I use log4j in the best way in my program?

I configure all logging properties in the properties file.


#For File Append of the logs
log4j.rootCategory=DEBUG, rfware
log4j.appender.rfware.layout.ConversionPattern=%d [%t] %-5p %c - %m%n


But I want to load this properties during the start up of my application for logging in my java all java classes.

can you provide some code guide line how can I achieve it ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

If you put that into a file named and make sure that the properties file is in a directory that's in the classpath when you run your application, then Log4J will automatically find it and you don't have to do anything special to initialise Log4J in your application.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
shankha bhattacharya

Joined: Apr 20, 2007
Posts: 12
If I am not use the name then how can I use it to my application?
marc weber

Joined: Aug 31, 2004
Posts: 11343

Promoting to the intermediate forum.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Doesn't this belong in "Other opensource"?

Anyway, take a look at the PropertyConfigurator. If you call configure(file) or configureAndWatch(file) before the first log statement, it will use the configuration in the given file.
jianfeng ding

Joined: Sep 06, 2007
Posts: 2
how about use: PropertyConfigurator.configure(".\\src\\") first init the log4j?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

"west west", please check your private messages. You can see them by clicking My Profile.
It is sorta covered in the JavaRanch Style Guide.
subject: log4j use best way
Similar Threads
How to use different logging in different packages with log4j?
How to separate Console and / or File appenders in Log4J?
New to log4j
Log4j and WebSphere - the standard WebSphere SystemOutput.Is this how it should work?
Servlet Logging