PHP
  Home arrow PHP arrow Page 4 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 28
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Logging With PHP - Turning Up The Heat


    (Page 4 of 9 )

    If the thought of rolling your own code doesn't appeal to you, you can also use PHP's built-in error_log() function, which logs data both to a file and elsewhere. The error_log() function needs a minimum of two arguments: the error message to be logged, and an integer indicating where the message should be sent. There are three possible integer values in PHP 4.x:

    0 - send the message to the system logger ("syslogd" on *NIX, and the Event Log on Windows NT);

    1 - send the message to the specified email address;

    3 - send the message to the specified file;

    Here's a trivial example which demonstrates how this works:


    <?php // set a variable $temp = 101.6; // test it and log an error // this will only work if // you have "log_errors" and "error_log" set in your php.ini file if ($temp > 98.6) { error_log("Body temperature above normal.", 0); } ?>
    Now, if you look at the system log file after running the script, you'll see something like this:

    [28-Feb-2002 15:50:49] Body temperature above normal.
    You can also write the error message to a file,

    <?php // set a variable $temp = 101.6; // test it and log an error if ($temp > 98.6) { error_log("Body temperature above normal.", 3, "a.out"); } ?>
    or send it out as email.

    <?php // set a variable $temp = 101.6; // test it and log an error if ($temp > 98.6) { error_log("Body temperature above normal.", 1, "administrator@this.body.com"); } ?>
    It's possible to combine this error logging facility with a custom error handler to ensure that all script errors get logged to a file. Here's an example which demonstrates this:

    <?php // custom handler function eh($type, $msg, $file, $line) { // log all errors error_log("$msg (error type $type)", 0); // if fatal error, die() if ($type == E_USER_ERROR) { die($msg); } } // report all errors error_reporting(E_ALL); // define custom handler set_error_handler("eh"); // let's now write some bad code // this will trigger a warning, since the file doesn't exist include("common.php"); ?>
    And here's the output that gets logged to the system log file:

    [28-Feb-2002 16:15:06] Failed opening 'common.php' for inclusion (include_path='.;') (error type 2)

    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

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway