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 Java Logger Puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Logger Puzzle" Watch "Java Logger Puzzle" New topic

Java Logger Puzzle

Glenn Murray
Ranch Hand

Joined: Dec 07, 2001
Posts: 74

I am trying to write a utility class to get Loggers with a certain Handler Formatter. The output from the following code is

Can anyone explain why there are only messages from the last logger?


Glenn Murray
Author of Yo Soy Una Vaca De Hoy
michael warren
Ranch Hand

Joined: Oct 20, 2006
Posts: 50
Just tried this and get the same result as you, I'd be lieing if I said I knew why, but have experimented and think its something to do with using the same stream in each logger, possibly different threads accessing the same stream ?.

If the method to create the handler creates a handle using a different FileOutputStream (outputting to different files) for each logger then it works as expected, and if it uses the same file then only the 1st logger outputs. I tried creating a handler that flushes the stream after each "publish" but that didn't have any effect.

I'd also be interested if anyone can shed any light on this.
michael warren
Ranch Hand

Joined: Oct 20, 2006
Posts: 50
Interestingly the following handler works as expected

The documentation for StreamHandler says "This is primarily intended as a base class or support class to be used in implementing other logging Handlers" implying that you're not expected to use it directly, think I need to read more on the Logging package in general to understand why it doesn't work as expected.
I agree. Here's the link:
subject: Java Logger Puzzle
It's not a secret anymore!