PHP
  Home arrow PHP arrow Page 2 - 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 - The Bare Necessities


    (Page 2 of 9 )

    Logging data to a file in PHP can be as simple or as complex as you want to make it. Break it down, though, and it all comes down to these three simple lines of code:

    <?php // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $str . "\n"); // close file fclose($fd); ?>
    Fundamentally, logging data to a file consists of three steps:

    1. Open the target file (or create it if it doesn't already exist);

    2. Append your data to the end of the file;

    3. Close the file.

    You can encapsulate this as a function,

    <?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $msg . "\n"); // close file fclose($fd); } ?>
    and then use it liberally within your code as and when required.

    <?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $msg . "\n"); // close file fclose($fd); } $v = "Mary had a little lamb"; if (!is_numeric($v)) { logToFile("my.log", "Non-numeric variable encountered"); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { logToFile("my.log", "No fish available"); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { logToFile("my.log", "Could not connect to database"); die("Could not connect to database"); } ?>
    You can make the function a little more professional by having it automatically append the date and time of the log message to the log file as well:

    <?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // append date/time to message $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg; // write string fwrite($fd, $str . "\n"); // close file fclose($fd); } ?>
    Here's an example of the output:

    [2002/11/25 06:02:42] Non-numeric variable encountered [2002/11/25 06:02:42] No fish available [2002/11/25 06:02:43] Could not connect to database

    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 3 hosted by Hostway