PHP
  Home arrow PHP arrow Page 5 - Logging With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Logging With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2002-11-27

    Table of Contents:
  • Logging With PHP
  • The Bare Necessities
  • Turning The Tables
  • Turning Up The Heat
  • Biting Into A PEAR
  • Destination Unknown
  • Artificial Intelligence
  • Big Brother Is Watching
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Logging With PHP - Biting Into A PEAR


    (Page 5 of 9 )

    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:

    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.

    More PHP Articles
    More By icarus, (c) Melonfire


       · I like the simple example that loggs to a file and with a small modification, we can...
       · This is a great stuff...But how can we make the log file thus created write...
       · This should do it:[code]function log_action($msg) { $today =...
       · This should do it:[code]function log_action($msg) { $today =...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT