#native_company# #native_desc#
#native_cta#

PEAR Primer Page 5

By Joe Stump
on December 26, 2002

The PEAR Log Class
I like logging certain errors and information straight to syslog, like failed logins to my site’s admin section.
It’s also a less intrusive way to debug your code. The PEAR Log class lets you log to syslog, your own log files,
SQL databases, and more. It also has some more sophisticated features that let you “attach” classes to logs and
then “notify” those classes when things are logged. I have yet to tackle those features, but they sound
interesting. I’m going to build on the PEAR DB example to incorporate the Log class.

<?php

  require_once('Base.php');

  $log Log::factory('syslog','My App');

  $db =& DB::connect(BASE_DEFAULT_DSN,true);

  if(!
DB::isError($db))

  {

    
$db->setFetchMode(DB_FETCHMODE_ASSOC);

    

    
$sql "SELECT *

            FROM table

            WHERE foo='bar'"
;

    $result $db->query($sql);

    if(!
DB::isError($result) && $result->numRows())

    {

      while(
$row $result->fetchRow())

      {

        echo 
'&lt;li&gt;'.$row['foo']."n";

      }

    }

    else

    {

      
$this->log($sql);

    }

  }

  else

  {

    
$log->log($db->getMessage());

  }

?>



Now tail your syslog and run the above example. You should find that there are some errors. It includes all sorts
of date information and the identifier (the second argument in the constructor) you assigned your log. You can
alternately use your own log files by using the type ‘file’ instead of ‘syslog’.

1
|
2
|
3
|
4
|
5
|
6
|
7