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;
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.
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()
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()
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()
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()
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)
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.
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 ) );
strftime()
in the PHP
documentation.
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.
new()
method is called.
initialize()
method. You can alter it on the fly by simply
modifying it's contents.
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.
Submit bug reports or commentary to CDI, cdi@thewebmasters.net
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.
http://www.thewebmasters.net/php/
error_log()
in the PHP3 documentation.