• 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

Need to print date in stdout logfile of tomcat

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JavaDevelopers,

we have a web application, now we want to print date at stdout log file, actually we are printing the output using "System.out.println()" in so many java files,
so we want date & time in stdout/stderr logfiles at where we are getting the output(with out changing java files). please help me.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can't really change what the code does without changing any code.

You should be using something like Log4J for logging, and then you could easily change the output format in the configuration rather than changing any code.
 
santhosh bhupathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bear Bibeault thank you for replay,
But we need to pass our class to gettlogger() method and we need import log4j package in the old java files is it right,
or if there any possibility can you provide me simple example please.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you want to use code from Log4J then you're going to have to change your code to use the Log4J classes and therefore you'd have to import them.

I would recommend against trying to update your application based on a "simple example" which you got from somewhere on the web. Even if you found it here. There's far too much code out there which has been written using that technique. Instead I would suggest you spend a little time going through the Log4J tutorial, so you understand what it is and what the recommended way to use it is. You can find that by using "Log4J tutorial" as your search engine keywords.

Of course if you still have problems after that then we're still here to help. Good luck!
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a forum dedicated specifically to Tomcat, so that's really a better place to ask Tomcat-related questions.

stdout and stderr aren't really "logfiles". They are the JVM's standard writers after the time-honored architecture initially defined bu Unix and later adopted by DOS, Windows and Linux (among other OS's). As such, they contain literally what was contained in the argument of the System.out or System.err print/println statement, no more and no less. Well, except that println appends an OS-specific end-of-line character sequence to the output, anyway,

True loggers such as log4j channel the log print requests, categorizing them and routing them to log outputs, allowing stuff like timestamps to be automatically added along the way. You can select or suppress specific topics as needed. This is why we discourage using stdout in web applications in favor of logging. Also because the J2EE and JEE specs don't actually support the concept of stdout/stderr, so for a given webserver, there's no telling where the stdout and stderr output might show up. If it shows up at all.

The Tomcat server uses its own logger in addition to the stdout console file (which is usually named catalina.out). ONLY Tomcat internal messages go to that logger, however. Each webapp is responsible for its own logging setup independent of the logging environment of the Tomcat server.

It's actually fairly easy to automate conversion to log4j. Just create a log4j config file in the WEB-INF/classes directory to define what should go where, add something like the following statement to each class to be logged:


along with the import for org.apache.log4j.Logger. Then do a bulk edit/replace of "System.out.println(" with "log.info(". You can replace System.err.println with "log.error(". In cases where "print()" is used instead of "println()", then it's best to fix on a case-by-case basis, but the MessageFormat class can assist in creating a loggable message string.

Incidentally, I set up a hotkey in my IDE that automatically inserts the import and logger definition for a class, since I do it so often. Saves a lot of typing!
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
santhosh bhupathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

we have a web application, now we want to print date at stdout log file, actually we are printing the output using "System.out.println()" in so many java files,
so we want date & time in stdout/stderr logfiles at where we are getting the output(with out changing java class files). please help me.
 
santhosh bhupathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But we don't have a permission to change java class files and 150+ java files having output statements and the webapplication is sevlet based application.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh bhupathi wrote:But we don't have a permission to change java class files



Bear Bibeault wrote:Well, you can't really change what the code does without changing any code.



How do they expect you to change what the code does without changing the code?
 
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well modifying the code is a must in your case.

But you should also remember this for future.
System.out.println() in code is a DEFECT in CODE REVIEW.
You should never use them. Instead use Logger.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it actually take all that long to find/replace sys.out and sys.err calls with appropriate logger calls?
Compared with trying to hack in a solution.
reply
    Bookmark Topic Watch Topic
  • New Topic