• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date and message on same line using logging API

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, that's pretty odd! Your work around does work though. Thanks for the help!
Barry
 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic