Log - A simple Object Orientated Logger

- NAME
- SYNOPSIS
- DESCRIPTION
- REQUIRED MODULES
- METHODS

- new
- _init & Private methods
- msg
- exp
- close
- DESTROY
- PIDstamp
- dateTimeStamp
- debugValue
- expText
- getExpText
- clearExpText
- expCnt
- getFileName

- PROPERTIES
- KNOWN ISSUES
- AUTHOR
- LICENSE
- CVS ID

# NAME

Log - A simple Object Orientated Logger

# SYNOPSIS

     use Log;

     # Pretty format, all the parameters
     my $log = Log->new({
       debug           => 4,                   # Set the debug level
       logFileName     => 'myLogFile.log',     # define the log filename
       logFileMode     => '>',                 # '>>' Append or '>' overwrite
       dateTimeStamp   => 1,                   # Timestamp log data entries
       stderrRedirect  => 1,                   # Redirect STDERR to the log file
       defaultFile     => 1,                   # Use the log file as the default filehandle
       logFileDateTime => 1,                   # Timestamp the log filename
       appName         => 'myApplicationName', # The name of the application
       PIDstamp        => 1,                   # Stamp the log data with the Process ID
       storeExpText    => 1,                   # Store internally all exp text
     });

     # Minimal instance, logfile name based on application name
     my $log = Log->new();

     # Typical usage, set the debug level and log filename (say from a config file)
     my $log = Log->new({ debug => $debugLevel, logFileName => $logFileName, });

     # Print message to the log file if the debug is >= 2
     $log->msg(2, "Add this to the log file if debug >= 2 \n");

     # Print an exception (error) message to the log file
     $log->exp("Something went wrong\n");

     # Close the log file (optional at exit)
     $log->close();

     # Change the debug level, capturing the old value
     $oldDebugValue     = $log->debugValue($newDebugValue);

     $currentDebugValue = $log->debugValue();

     # Get all the exceptions text (so you can do something with all the errors, eg email them)
     $allExceptions     = $log->getExpText();

     $numberErrors      = $log->expCnt();        # How many times has $log->exp been called

# DESCRIPTION

Log is a class providing methods to log data to a file. There are a number of parameters that can be passed to allow configuration of the logger.

# REQUIRED MODULES

Carp (confess is used), FindBin and Symbol;

# METHODS

There are no class methods, the object methods are described below. Private class method start with the underscore character '_' and should be treated as Private.

## new

Called to create a Log object. The following optional named parameters can be passed to the constructor via an anonymous hash:

debug
  Used to set the debug level. The default level is 9. The debug level is used by other methods to determine if data is logged or ignored. See `msg` and `exp` methods.

logFileName
  Defines the path and name of the log file that is written too. If not defined then the value of appName with '.log' appended is used. If appName is not defined in the constructor then BinFind is used to determine the name of the application.

logFileMode
  Used to determine if the log file is overwritten or appended too. Default is append. Valid value are '>' for overwrite and '>>' for append.

dateTimeStamp
  If true (default is false), then each entry written to the log file using the `msg` and `exp` methods has the current date and time prepended to the data.

stderrRedirect
  If true (default is true), then redirect STDERR to the log file.

defaultFile
  If true (default is false), then select the log file as the default output file.

logFileDateTime
  If true (default is false), then include the date and time into the name of the log file just before the '.log'. The format of the date and time used is _YYYYMMDD-HHMMSS

appName
  If logFileName is not defined then the appName is used as the basis of the log file. If appName is not defined then the FindBin module is use to determine the name of the application and is stored within the appName hash variable.

PIDstamp
  If true (default is false), then the Process ID is prepended to the data written to the log file by the `msg` and `exp` methods. This is handy when there are more than one processes writting to the same log file.

storeExpText
  If true (default is false), then any data written with the `exp` method is also stored internally for later retrival with the `getExpText` method. The stored data can also be cleared with the `clearExpText` method. This can be useful if there may be multiple exceptions which you then want to report on (other than in the log file) as one text string.

## _init & Private methods

Private method to initialise the object on construction. Called by `new()`. All Private methods start with _ and should be treated as PRIVATE. No other private methods are documented (since they are private).

## msg

The `msg` method is used to log a message to the log file. The first POSITIONAL argument to `msg` is the ``debug level'' at which the message should be added to the log file if the instance ``debug value'' is greater than or equal to the ``debug level''.

The second and optional subsiquent arguments are treated as text to print to the log file.

eg. $log->msg(2, ``Printed to log file if 'debug' is greater than or equal to 2 \n'');

Note that newline characters are not automatically appended by this method.

## exp

`exp` is used to report exceptions. There is no ``debug level'' parameter, just one or more text strings which are printed to the log file. The text printed has ``**'' prepended to each line (this occurs before prepended timestamp or PID values).

Note that newline characters are not automatically appended by this method.

## close

Closes the file handle associated with the log file.

## DESTROY

`DESTROY` is defined and closes the file handle associated with the log file.

## PIDstamp

The `PIDstamp` method can be used to set or get the value of the PIDstamp instance variable. If called without parameters, the current value of the PIDstamp instance variable is returned. If called with a parameter, the parameter is used to set the PIDstamp instance variable and the previous value is returned.

Refer to the `new` method for further information.

## dateTimeStamp

The `dateTimeStamp` method can be used to set or get the value of the dateTimeStamp instance variable. If called without parameters, the current value of the dateTimeStamp instance variable is returned. If called with a parameter, the parameter is used to set the dateTimeStamp instance variable and the previous value is returned.

Refer to the `new` method for further information.

## debugValue

The `debugValue` method can be used to set or get the value of the debugValue instance variable. If called without parameters, the current value of the debugValue instance variable is returned. If called with a parameter, the parameter is used to set the debugValue instance variable and the previous value is returned.

Refer to the `new` method for further information.

## expText

The `expText` method can be used to set or get the value of the storeexptext instance variable. If called without parameters, the current value of the storeexptext instance variable is returned. If called with a parameter, the parameter is used to set the storeexptext instance variable and the previous value is returned.

Refer to the `new` method for further information.

## getExpText

The `expText` method is used to retreive the stored value of the instance ``Exception Text''.

## clearExpText

The `clearExpText` method is used to clear the stored value of the instance ``Exception Text''.

## expCnt

The `expCnt` method is used to retreive the number of times that the exp method has been called for this object.

## getFileName

The `getFileName` method is used to retreive the actual log file name used for this object.

# PROPERTIES

see the `new` method.

# KNOWN ISSUES

none

# AUTHOR

Greg George, IT Technology Solutions P/L, Australia Mobile: +61-404-892-159, Email: gng@cpan.org

# LICENSE

Copyright (c) 1999- Greg George. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

# CVS ID

$Id: Log.pl,v 1.1 2004/07/18 18:20:11 gxg6 Exp $