File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Logging to one file from multiple java files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logging to one file from multiple java files" Watch "Logging to one file from multiple java files" New topic

Logging to one file from multiple java files

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Hi Guys,

I want to set up a logger that writes to one file from many different java files.
I just want to user the Logger class for now as I think thats all I need for my program.

How would I change the above code so that I can use myLogger in a different java file but still in the same package?
Also I want to be able to log messages from within static and non-static methods which is where I�m having the trouble at the moment.
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

I believe your problem can be solved by using Singleton pattern. You have to wrap your myLogger object in a Singleton class. Hence, you will use your logger wrapper in any part of any class in your application, and will output the log message into one file.

For example:

You can use your wrapper in any part of any class in your application like this:

[ August 20, 2008: Message edited by: Hendy Setyo Mulyo ]

Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
You should move the config of the destination log file, level, formatter etc out to for easier maintenance. You can specify where it is using JVM arg

You can create another instance of Logger in another class and just use that, you dont need to use the same instance. For example:

Then, depending on what level you have specifed out in the properties file (default is INFO unless changed), some, none or all of the above log messages will appear.


<a href="" target="_blank" rel="nofollow">Use Code Tags!!</a>
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Logging to one file from multiple java files
It's not a secret anymore!