PHP
  Home arrow PHP arrow Page 6 - Logging With PHP
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 
Mobile Linux 
App Generation ROI 
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 / 30
    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 - Destination Unknown


    (Page 6 of 9 )

    The Log class also supports sending log messages to the console, the system logger, an SQL database or a user-specified email address. Take a look:

    <?php // include class include("Log.php"); // create Log object $l = &Log::singleton("console"); // 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, log messages are directed to the text console (if you're running this script via a Web server, the console is the browser window). Here's the output:

    Nov 26 22:05:25 [warning] Non-numeric variable encountered Nov 26 22:05:25 [error] No fish available Nov 26 22:05:27 [critical] Could not connect to database
    Messages can also be sent to an SQL database,

    <?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://joe:pass@localhost/test')); // 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); } ?>
    which can then be queried to retrieve subsets of the log messages, sorted by date or priority level.

    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:

    <?php // include class include("Log.php"); // create Log object $l = &Log::singleton("mail", "admin@host.com"); // 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); } ?>
    You can customize the mail message by adding an array containing a custom From: and Subject: line to the object constructor, as below:

    <?php // include class include("Log.php"); // create Log object $l = &Log::singleton("mail", "admin@host.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 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); } ?>

    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

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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