• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Logfiles

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
How to convert System.err and print statements to error.log file
Regards.
Vijaya.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to use one (or more) of the stream classes to create a stream that outputs to a file. I think there is a method to set System.err to refer to the stream you create, but you will have to check the API to find out for sure.
Layne
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three basic approaches I can think of:
(1) Use OS-level output redirection, e.g.
java MyClass 1> output.log 2> error.log
or
java MyClass 1> all_output.log 2>&1
I believe the same syntax works in Unix and Windows here. This is easy, and requires no changes inside your code.
(2) Use System.setOut() and System.setErr() within your program to redirect the output to a stream writing to a file, as Layne suggests. Also pretty easy.
(3) Replace the various println() statements with more advanced logging techniques, using classes such as those in java.util.logging or the Log4J package. This is more work than the other two, but may be more rewarding as it will allow you much more flexibility to make other adjustments later. E.g. you may initially decide that loggers will simply write messages to the screen. Later you can easily revise this to write all messages to one detailed log file, and also write only errors and important non-error messages to a different file, and also create a pop-up window to inform the user of errors. (For example.) These later changes can be made by making changes just to the code that creates and configures the loggers (or maybe just to configuration files which are read by your code). Most of the code in the other classes that do the logging will not need to be changed at all.
I like the third option for a big project, where the power and flexibility I get later makes it worthwhile to spend a little extra energy at teh beginning to set up logging. For smaller projects, the first two options may be appropriate.
 
vijaya soft
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!,
ThanQ!!!Jim.
Regards.
Vijaya.
 
I can't take it! You are too smart for me! Here is the tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic