File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes date and message on same line using logging API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "date and message on same line using logging API" Watch "date and message on same line using logging API" New topic
Author

date and message on same line using logging API

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi,
Using the standard library for logging, i.e. java.util.logging, I was wondering if there is any way to display the date and the message on the same line. Currently if you do something like logger.info("My Message"); The logger will display a timestamp followed by the class name and method where the trace is coming from and then the next line will be "My Message". I would like to display all of this information on the same line or even cut the timestamp out altogether. If anyone knows how, I would love to hear. Many thanks!

Barry
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
See this article to see how easy it to write your own Formatter:
http://www.javaranch.com/newsletter/May2002/newslettermay2002.jsp#logging


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Thanks Thomas for the great article. It was very useful! I am a bit puzzled by the behaviour of my Formatter though. I extended Formatter class and overrode the format() method and defined my format like I wanted. Then I used the ConsoleHandler and set the format to my Format class. The puzzling thing is at runtime, the top line is formatted exactly as I specified, but then I get 2 more lines that is like it was before with no formatting. Why does this happen? Here is a sample output:

The top line is what I want. How do I get rid of the next 2 lines? Thanks so much for your help!
Barry
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This is a common mistake. The problem is that your Logger object has a parent Logger object that defaults to print to the console. it's a pain in the neck and I'm not sure why it was implemented this way but there is a way to handle it.

This will clear out all the Handlers in the parent object.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

wow, that's pretty odd! Your work around does work though. Thanks for the help!
Barry
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
I have just been reading through old posts to figure out how to set up java sdk 1.4 logging the way I like.
I think in this situation you could just as easily go:

and it would solve the double logging to console issue, instead of looping through the parents to turn them off.
I couldn't reconstruct a situation like this though - the way I have configured logging with a logging.properties file, my logger doesn't actually have its own handler, it inherits it from its parent.
My two cents.
Adam


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Yes, but if you need the handler (and in my case I did) then setUseParentHandlers(false) I'm assuming will disable ALL handlers which would be no good.
Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date and message on same line using logging API