Forward Computing and Control Pty. Ltd.
Logging Package V1.3.0

2004/11/22

au.com.forward.logging
Class LogStdStreams

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.PrintStream
              extended by au.com.forward.logging.LogStdStreams
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class LogStdStreams
extends java.io.PrintStream

Redirects System.Err and optionally System.Out to an output file This catches any uncaught exception traces. A heading can be written to the top of the file. Autoflush is set so ensure last error saved to disk Each application invocation appends to the log file. Use this class is you are using Log4J or some other non-Java logging package.
If you are using standard Java logging then consider using Logging.initializeLogging(String, String ,boolean, int) instead To initialize this class add the following code to your main application class

 
 import au.com.forward.logging.*;

 static {
    // put all log files in user's home dir  
    String userHome = System.getProperty("user.home",".")
                      +System.getProperty("file.separator","/");
    // use current dir if cannot find user home 
    // set up logging for errors etc
    // throws RuntimeException if cannot open log file
    LogStdStreams.initializeErrorLogging(userHome+"applicationLog.log", 
                            "Log File for Application "+ new Date(),
                            true, true); 
                            // redirect System.out as well as err and append to existing log
 }                                        

Then in the body of the code any statements like

 
 System.err.println(" Error message ..");
 System.out.println(" Output ... ");
will be sent to the log file and not sent to the terminal window.

Version:
1 5th Oct 2001
Author:
Dr. M.P. Ford

Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Method Summary
static java.lang.String getLogFileName()
          Get the name of the logfile
static java.io.PrintStream getLogStream()
          Get the PrintStream being used for this logfile
static void initializeErrorLogging(java.lang.String fileName)
          The static initialization method
Also redirects System.out, rewrites output file
static void initializeErrorLogging(java.lang.String fileName, java.lang.String initialStr)
          The static initialization method which writes heading
Also redirects System.out, rewrites output file
static void initializeErrorLogging(java.lang.String fileName, java.lang.String initialStr, boolean logStdOut)
          The static initialization method which writes heading
Rewrites output file
static void initializeErrorLogging(java.lang.String fileName, java.lang.String initialStr, boolean logStdOut, boolean append)
          The static initialization method
static void initializeErrorLogging(java.lang.String fileName, java.lang.String initialStr, boolean logStdOut, int maxSizeInKbytes)
          The static initialization method to roll logfile.
static boolean isLoggingInitialized()
          Has the logging already been initialized?
static boolean isSystemOutRedirected()
          Is the System.Out being redirected to this logfile
static void main(java.lang.String[] args)
          A test main()
 
Methods inherited from class java.io.PrintStream
append, append, append, checkError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getLogStream

public static java.io.PrintStream getLogStream()
Get the PrintStream being used for this logfile

Returns:
the PrintStream System.err is being redirected to
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has not been called yet

isLoggingInitialized

public static boolean isLoggingInitialized()
Has the logging already been initialized?

Returns:
true if logging already initialized else false

isSystemOutRedirected

public static boolean isSystemOutRedirected()
Is the System.Out being redirected to this logfile

Returns:
true if System.Out is being redirected
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has not been called yet

getLogFileName

public static java.lang.String getLogFileName()
Get the name of the logfile

Returns:
true if System.Out is being redirected
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has not been called yet

initializeErrorLogging

public static void initializeErrorLogging(java.lang.String fileName)
The static initialization method
Also redirects System.out, rewrites output file

Parameters:
fileName - the name of the log file
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has already been called yet

initializeErrorLogging

public static void initializeErrorLogging(java.lang.String fileName,
                                          java.lang.String initialStr)
The static initialization method which writes heading
Also redirects System.out, rewrites output file

Parameters:
fileName - the name of the log file
initialStr - the heading string to write to the log file when opened
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has already been called

initializeErrorLogging

public static void initializeErrorLogging(java.lang.String fileName,
                                          java.lang.String initialStr,
                                          boolean logStdOut)
The static initialization method which writes heading
Rewrites output file

Parameters:
fileName - the name of the log file
initialStr - the heading string to write to the log file when opened
logStdOut - true if System.Out to redirected to log file also
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has already been called

initializeErrorLogging

public static void initializeErrorLogging(java.lang.String fileName,
                                          java.lang.String initialStr,
                                          boolean logStdOut,
                                          boolean append)
The static initialization method

Parameters:
fileName - the name of the log file
initialStr - the heading string to write to the log file when opened
logStdOut - true if System.Out to redirected to log file also
append - true if to append to existing log, false to rewrite new log.
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has already been called or cannot create log file dir or open log file

initializeErrorLogging

public static void initializeErrorLogging(java.lang.String fileName,
                                          java.lang.String initialStr,
                                          boolean logStdOut,
                                          int maxSizeInKbytes)
The static initialization method to roll logfile.
Will roll log file to .old when it exceeds the specified Kbytes

Parameters:
fileName - the name of the log file
initialStr - the heading string to write to the log file when opened
logStdOut - true if System.Out to redirected to log file also
maxSizeInKbytes - roll current file to .old if it exceeds this size on startup. Any existing .old log file is deleted.
Throws:
java.lang.RuntimeException - if initializeErrorLogging() has already been called or cannot create log file dir or open log file

main

public static void main(java.lang.String[] args)
A test main()

Parameters:
args - Description of Parameter

Forward Computing and Control Pty. Ltd.
Logging Package V1.3.0

2004/11/22

Copyright ©2003, Forward Computing and Control Pty. Ltd
ACN 003 669 994   NSW Australia,   All Rights Reserved.
email:fcc@forward.com.au