This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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: 14961

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 8 API documentation
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: 14961

"west west", please check your private messages. You can see them by clicking My Profile.
I agree. Here's the link:
subject: log4j use best way
It's not a secret anymore!