Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Logging With PHP
Log script activity in your Web application with PHP's logging functions.

| Biting Into A PEAR |

As if all that wasn't enough, PHP offers one more alternative - a special Log class that comes courtesy of PEAR, the PHP Extension and Application Repository (http://pear.php.net). In case you didn't know, PEAR is an online repository of free PHP software, including classes and modules for everything from data archiving to XML parsing. This Log class is maintained by Jon Parise, and the latest version can be downloaded from http://pear.php.net/package-info.php?package=Log

Like the error_log() function, the PEAR Log class allows logging to a variety of different destinations - system logger, text file, email address, database and even MCAL. Here's an example demonstrating basic usage:


<?php

// include class
include("Log.php");

// create Log object
$l = &Log::singleton("file", "my.log");

// test it
$v = "Mary had a little lamb";
if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); }

$a = array("chocolate", "strawberry", "peach");
if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); }

$conn = @mysql_connect("localhost", "joe", "pass");
if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); }

?>


The first step here is to include() the Log class:


// include class
include("Log.php");


Once that's done, a new Log object can be created. This object constructor requires, as its first parameter, a string indicating the kind of log to open - current valid values are 'console', 'syslog', 'sql', 'file', and 'mcal'. Depending on the log type, additional data may be provided - the name of the file to use in case of a file logger, for example.


// create Log object
$l = &Log::singleton("file", "my.log");


The Log class internally defines eight priority levels for logging - here's the list, culled directly from the source code of the class:


$priorities = array(
            PEAR_LOG_EMERG  => 'emergency',
            PEAR_LOG_ALERT  => 'alert',
            PEAR_LOG_CRIT    => 'critical',
            PEAR_LOG_ERR    => 'error',
            PEAR_LOG_WARNING => 'warning',
            PEAR_LOG_NOTICE  => 'notice',
            PEAR_LOG_INFO    => 'info',
            PEAR_LOG_DEBUG  => 'debug'
        );


Each log message that you send to the logger can be flagged with a specific priority, and the logger can be set up to log only those messages matching or exceeding a specific priority level (by default, all messages are logged). This can be clearly seen from the test code below, in which each message to the logger includes a priority level:


$v = "Mary had a little lamb";
if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); }

$a = array("chocolate", "strawberry", "peach");
if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); }

$conn = @mysql_connect("localhost", "joe", "pass");
if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); }


Here's what the log file looks like:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Nov 26 06:47:15  [warning] Non-numeric variable encountered

Nov 26 06:47:15  [error] No fish available

Nov 26 06:47:17  [critical] Could not connect to database
').'
'

If you'd like to tell the logger to only log messages above a certain priority level - for example, critical and above - this priority level should be specified as the fifth argument to the object constructor. Consider the following revision of the previous example:


<?php

// include class
include("Log.php");

// create Log object
$l = &Log::singleton("file", "my.log", NULL, array(), PEAR_LOG_ERR);

// test it
$v = "Mary had a little lamb";
if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); }

$a = array("chocolate", "strawberry", "peach");
if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); }

$conn = @mysql_connect("localhost", "joe", "pass");
if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); }

?>


In this case, only messages flagged as PEAR_LOG_ERR and above will be written to the specified log file.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback