com.marringtons.util
Class Log

java.lang.Object
  extended bycom.marringtons.util.Log

public class Log
extends Object

Adept provides a simplified logging system where the Sun or log4j classes are overkill. The default log writes to the console - though this can be redirected to wherever is convenient. This is a very simple logging system. If you want permanent logging messages and better logging control, use the Sun or Log4J systems.

 Log.message("write a simple message to the log");
 Log.error("time stamp, message and stack dump to log", new Exception(
 		"sample exception"));
 Log.error(new Exception("sample exception"));
 Log.stamp("time stamp and message to log");
 
 Log.setStream(getOutputStream());
 Log.setStream(System.err); // PrintStream
 Log.setStream("myFile.log");
 
 Log.dumpStack = false; // suppresses stack dumps (usually for testing where errors expected).
 
 Log.verbose = false; // default is property verbose.logging
 Log.verbose("message won't log");
 Log.verbose = true;
 Log.verbose("message will log");
 if (Log.verbose) // save processing arguments that are never used ...
 		Log.verbose("message " + createMessage() + " that is expensive to build");
 Log.setVerbosity(true); // so persists as a property between runs.
 
 
While creating junit tests for other classes it is often important to review log output. There is a method called forJunit() for that purpose. Every time it is called it resets an in-memory logging buffer after extracting a string of all log messages since the last call.
 
  Log.forJunit();
  ...
  String logMessages = Log.forJunit();
  
 

Author:
Paul Marrington
See Also:
Application

Field Summary
static boolean dumpStack
          Used to reduce display on expected errors - only used by unit tests.
static boolean verbose
          Set to true to see more information on program operation - for production level fault finding.
 
Constructor Summary
Log()
           
 
Method Summary
static void debug(Object message)
          Method debug - write a timestamped message to the application log file.
static String error(Object message)
          Used to log errors.
static String error(Object message, Throwable throwable)
          Used to log errors.
static String error(Throwable throwable)
          Used to log errors.
static String forJunit()
          Set log for Junit testing by recording log output to a string and returning it on demand.
static void message(Object message)
          write a message to the application log file.
static void resetStream()
          Restore the stream to the console.
static void setStream(OutputStream stream)
          Logging goes to the console by default.
static void setStream(PrintStream stream)
          Logging goes to the console by default.
static void setStream(String name)
          Logging goes to the console by default.
static void setVerbosity(boolean verbosity)
          In Verbose mode get more logging messages - for production debugging.
static String stamp()
          Send timestamp to log (yyy7mmdd-hh:mm.sss).
static String stamp(Object message)
          Add a timestamped message line to the log.
static void verbose(String string)
          Only log if verbose mode is on.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dumpStack

public static boolean dumpStack
Used to reduce display on expected errors - only used by unit tests.


verbose

public static boolean verbose
Set to true to see more information on program operation - for production level fault finding. Changes will not persist. Use setVerbosity() to persist change between program invocations. Also used to stop parameter processing on log messages.
 if (Log.verbose)
   Log.verbose( "message "+createMessage()+" that is expensive to build");
 

See Also:
setVerbosity(boolean)
Constructor Detail

Log

public Log()
Method Detail

message

public static void message(Object message)
write a message to the application log file.

Parameters:
message - to write

error

public static String error(Object message,
                           Throwable throwable)
Used to log errors.

Parameters:
message - to display - IMPORTANT to provide context information
throwable - display exception info and stack dump
Returns:
a reference to find the error in the log - to give the user.

error

public static String error(Throwable throwable)
Used to log errors.

Parameters:
throwable - Exception that caused the error
Returns:
a reference to find the error in the log - to give the user.

error

public static String error(Object message)
Used to log errors.

Parameters:
message - to display - IMPORTANT to provide context information (what where and why).
Returns:
a reference to find the error in the log - to give the user.

stamp

public static String stamp()
Send timestamp to log (yyy7mmdd-hh:mm.sss).

Returns:
a reference to find the error in the log - to give the user.

stamp

public static String stamp(Object message)
Add a timestamped message line to the log.

Parameters:
message - to write to log.
Returns:
a reference to find the error in the log - to give the user.

resetStream

public static void resetStream()
Restore the stream to the console.
 Log.forJunit();
 ...
 String logResult = Log.forJunit();
 Log.resetStream();
 


setStream

public static void setStream(OutputStream stream)
Logging goes to the console by default. Use this method to redirect it to another stream.

Parameters:
stream - to write log messages to.

setStream

public static void setStream(PrintStream stream)
Logging goes to the console by default. Use this method to redirect it to another PrintStream. Commonly used to redirect back to standard error:
 Log.setStream( System.err);
 

Parameters:
stream - to write log messages to.

setStream

public static void setStream(String name)
Logging goes to the console by default. Use this method to redirect it to a file.

Parameters:
name - of file to write log messages to.

debug

public static void debug(Object message)
Method debug - write a timestamped message to the application log file. Helper function - same as stamp - that can be found and removed from production code.

Parameters:
message - to write

setVerbosity

public static void setVerbosity(boolean verbosity)
                         throws FileNotFoundException
In Verbose mode get more logging messages - for production debugging. This method sets it now and persistently until the call is made again. If you only want to change the verbosity for this run, modify the variable directly.

Parameters:
verbosity - true to display verbose messages.
Throws:
FileNotFoundException - because we record the configuration change
See Also:
verbose

verbose

public static void verbose(String string)
Only log if verbose mode is on.

Parameters:
string - message to write if verbose logging is on.

forJunit

public static String forJunit()
Set log for Junit testing by recording log output to a string and returning it on demand. It is cleared every time it is returned.

Returns:
String containing the log since the last time forJunit was called.


Copyright © 2005 Paul Marrington http://library.marringtons.com