NAME

Logger - A PHP class to facilitate multiple-file log entries


SYNOPSIS

    include("class.Logger.php3");
    $logger = new Logger("/path/to/log/file/root_directory");

    $logger->initialize(
                     array(
                            ERRLOG => 'error_log',
                            DEBUGLOG => 'debug_log'
                          )
                       );

    $logger->log(ERRLOG,"This is an error_log entry");
    $logger->log(DEBUGLOG,"This is logged to the debug_log");

    $logger->close_logs();
    exit;


DESCRIPTION

The Logger class is used to maintain a persistant log file as efficiently as possible. Using Logger, your programs can append log entries to as many different files as you need, using only 1 fopen() call and 1 fclose() call per log file.

Logger's primary use is for debugging personal programs when you can't or don't want to log via error_log(). That, and this class is a bit more efficient than error_log() , IMHO. error_log() has to make many system calls or apache method calls to facilitate it's log entries - Logger() doesn't.


METHODS


new Logger("/path/to/log/root_directory");

The new Logger() method will create a new Logger object, using the specified root directory as the location in which to store it's log files. If the path is omitted from the new() method, you must call the set_root() method, followed by the initialize() method. As you can see, it's much easier to set the root directory from new(). If you're on a WIN32 box, do not specify the root in your new() method. See also WIN32.


set_root("/path/to/log/directory");

The set_root() method specifies where you want your log files. The trailing slash on the directory is optional. The set_root() method is smart enough to figure it out. This method is considered internal, and only needs to be called if you do not specify the root path when you call the new() method. There are reasons you may want to do this, especially if this is a WIN32 box. See also WIN32


initialize( array( HANDLE, filename ) );

The initialize() method works on one argument; an array of HANDLE => filename pairs. The filename will be associated with the HANDLE you give it. This is a required step. If you're on a WIN32 box, see the WIN32 section.


log(HANDLE, "Log Entry Text");

The log() method is the meat of the class. It accepts the HANDLE associated with a log file, and a log entry. Log entries are ALREADY Timestamped and include the PID of the process. Log entries are already terminated with a carriage return - you do not need to include one with your log entry.

Log entries are formatted as follows;

    Mon dd hh:mm:ss [PID #] This is a log entry

Example:

    log() call:   $logger->log(ERRLOG,"This is a log entry");

    results   :   Jan 24 15:01:40 [27526] This is a log entry


close_logs()

The close_logs() method is a required step!. Before your program exits, it should call this method to close all currently open log files. The class opens a log file when the first log() method is called on it. The log file remains open until the close_logs() method is called, which means that if your program terminates without calling close_logs(), strange things may happen. (Although PHP's clean up should catch it, I wouldn't risk it) The close_logs() method will close all currently open logs. Calling the log() method after a close_logs() method shouldn't harm anything, but it will re-open the logs. (Usefull if you need to dynamically alter the set_root() location)


VARIABLES


DEBUG (boolean, default == true)

The DEBUG variable, if true (default) will log Logger class errors to the error_log function. The calls to log() do NOT get sent to error_log()! Only errors internal to the class get sent to error_log(). During development of your program, leave this set to true so you can spot file permission errors and what-not. Once your program goes live, set DEBUG to false.


WIN32 (boolean, default == false)

The global WIN32 var, if set to true, alters the way initialize() and set_root() work. If you're on a WIN32 server, here is the correct way to create a Logger object;

    include("class.Logger.php3");
    $logger = new Logger();

    $logger->WIN32 = true;
    $logger->set_root("\path\to\log\root\directory");
    $logger->initialize( array ( HANDLE, filename.log ) );


FORMAT (default == "%b %d %H:%M:%S")

This is Logger's internal TimeStamp format. Changing this variable alters the format of the timestamp. See also strftime() in the PHP documentation.


ROOT (default == "")

This variable is set with the path to the log file root directory, and is set by the set_root() method. The variable can be altered dynamically by your own code if need be. ROOT requires the trailing slash on your directory name. The set_root() method is smart enough to add it if you leave it off but if you alter the variable yourself, be sure to include it.


PID (default == getmypid() )

If you need the PID of your process and want to save yourself a function call, just grab this variable. It is set with the current PID when the new() method is called.


FILELIST ( default == array() )

The FILELIST var holds the array submitted by a successful initialize() method. You can alter it on the fly by simply modifying it's contents.


EXAMPLES

See the code examples sprinkled throughout this document. This class is fairly easy to figure out.


ERRORS

If DEBUG is true, file errors and what-not are logged via an error_log() call by the Logger class. Most httpd implementations place these errors in the daemons error_log file. (ie, /var/log/httpd/error_log ). If you don't know where this file is on your server, odds are you won't be able to read it anyway.


DOCUMENTATION

Your're looking at it.


INSTALLATION

Drop the class.Logger.php3 file in the same directory as your source, or, place it in the include_path directory specified in your php3.ini file.


BUGS

None that I care to admit, although I do have this nasty rash... :)

Submit bug reports or commentary to CDI, cdi@thewebmasters.net


VERSION

Revision 1.0 1999/01/28 CDI, cdi@thewebmasters.net


AUTHOR

Copyright (c) 1999 CDI, cdi@thewebmasters.net, All Rights Reserved.


LICENSE

This program is free software; you can redistribute it and/or modify it under the GNU General Artistic License, with the following stipulations;

Changes or modifications must retain these Copyright statements. Changes or modifications must be submitted to the AUTHOR, cdi@thewebmasters.net.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Artistic License for more details. This software is distributed AS-IS.


AVAILABILITY

The latest version of this class should be available from the following locations;

http://www.thewebmasters.net/php/

http://php.codebase.org/


SEE ALSO

Ummm let's see: error_log() in the PHP3 documentation.


FILES

none


HISTORY

Version 1.0: Initial Public Release