Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > PHP

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

Advanced Logging in PHP with PEAR


On 20th July, 2006
Wink Advanced Logging in PHP with PEAR

Show Printable Version Email this Page Subscription Add to Favorites Copy Advanced Logging in PHP with PEAR link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

PHP offers one 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:

Code: PHP
<?php
  // include class
  require("Log.php");
  // create Log object
  $l = &Log::singleton("file", "my.log");
  // test it
  $v = "Logging with PHP using PEAR";
  if (!is_numeric($v)) { $l->log("Non-numeric variable was found",PEAR_LOG_WARNING); }
  $a = array("chocolate", "strawberry", "peach");
  if (!in_array('fish', $a)) { $l->log("No fish found", PEAR_LOG_ERR);}
  $conn = @mysql_connect("go4expert", "user", "password");
  if (!$conn) { $l->log("Could not connect to database on go4expert", PEAR_LOG_CRIT);
  }
  ?>
The first step here is to include() the Log class:

Code: PHP
// 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.

Code: PHP
// 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:

Code: PHP
$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:

Code: PHP
$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:

Code:
Nov 26 06:47:15  [warning] Non-numeric variable was found
  Nov 26 06:47:15  [error] No fish found
  Nov 26 06:47:17  [critical] Could not connect to database on go4expert
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:

Code: PHP
<?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 was found",
  PEAR_LOG_WARNING); }
  $a = array("chocolate", "strawberry", "peach");
  if (!in_array('fish', $a)) { $l->log("No fish found", PEAR_LOG_ERR);
  }
  $conn = @mysql_connect("go4expert", "user", "password");
  if (!$conn) { $l->log("Could not connect to database on go4expert", PEAR_LOG_CRIT);
  }
  ?>

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

Messages can also be sent to an SQL database,

Code: PHP
<?php
 // include class
 include("Log.php");
 // create Log object
 // second argument is table name
 // fourth argument is PHP::DB compatible DSN for database access $l =
 &Log::singleton("sql", "log_table", "", array('dsn' =>
 'mysql://user:passwordword@go4expert/errors'));
 // test it
 $v = "Mary had a little lamb";
 if (!is_numeric($v)) { $l->log("Non-numeric variable was found",
 PEAR_LOG_WARNING); }
 $a = array("chocolate", "strawberry", "peach");
 if (!in_array('fish', $a)) { $l->log("No fish found", PEAR_LOG_ERR);
 }
 $conn = @mysql_connect("go4expert", "user", "passwordword");
 if (!$conn) { $l->log("Could not connect to database on go4expert", PEAR_LOG_CRIT);
 }
 ?>

which can then be queried to retrieve subsets of the log messages, sorted by date or priority level.

Code: SQL
mysql> SELECT * FROM log_table WHERE priority >= 4;
 +----------------+-------+----------+----------------------------------+
 | logtime        | ident | priority | message                          |
 +----------------+-------+----------+----------------------------------+
 | 20021126074936 |       |        4 | Non-numeric variable encountered |
 | 20021126074936 |       |        4 | Non-numeric variable encountered |
 | 20021126074937 |       |        4 | Non-numeric variable encountered |
 +----------------+-------+----------+----------------------------------+
 3 rows IN SET (0.05 sec)

Finally, log messages can also be directed to a specified email address, as in the following example:

Code: PHP
<?php
 // include class
 include("Log.php");
 // create Log object
 $l = &Log::singleton("mail", "admin@go4expert.com");
 // test it
 $v = "Mary had a little lamb";
 if (!is_numeric($v)) { $l->log("Non-numeric variable was found",
 PEAR_LOG_WARNING); }
 $a = array("chocolate", "strawberry", "peach");
 if (!in_array('fish', $a)) { $l->log("No fish found", PEAR_LOG_ERR);
 }
 $conn = @mysql_connect("go4expert", "user", "passwordword");
 if (!$conn) { $l->log("Could not connect to database on go4expert", PEAR_LOG_CRIT);
 }
 ?>

You can customize the mail message by adding an array containing a custom From: and Subject: line to the object constructor, as below:

Code: PHP
<?php
 // include class
 include("Log.php");
 // create Log object
 $l = &Log::singleton("mail", "admin@go4expert.com", NULL, array('from' => 'L.
 Ogger'
, 'subject' => 'Log message'));
 // test it
 $v = "Mary had a little lamb";
 if (!is_numeric($v)) { $l->log("Non-numeric variable was found",
 PEAR_LOG_WARNING); }
 $a = array("chocolate", "strawberry", "peach");
 if (!in_array('fish', $a)) { $l->log("No fish found", PEAR_LOG_ERR);
 }
 $conn = @mysql_connect("go4expert", "user", "password");
 if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT);
 }
 ?>
<< Simple Logging with PHP
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
PHP On-The-Fly! Kings PHP 5 05-01-2008 11:28 AM
Track your visitors, using PHP Kings PHP 8 12-11-2006 01:53 AM
Simple Logging with PHP pradeep PHP 0 07-20-2006 07:26 PM
Installing & Using PEAR pradeep PHP 0 07-20-2006 12:42 PM
Begining with PHP shabbir PHP 0 07-15-2004 03:22 PM

 

All times are GMT +5.5. The time now is 05:29 AM.