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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 34
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT