Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date and message on same line using logging API

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this article to see how easy it to write your own Formatter:
http://www.javaranch.com/newsletter/May2002/newslettermay2002.jsp#logging
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, that's pretty odd! Your work around does work though. Thanks for the help!
Barry
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic