1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple Logging with PHP

Discussion in 'PHP' started by pradeep, Jul 20, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Manier times we require to create logs of various activities/errors in our web applications, so here we'll look into how to achieve that.
    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:
    <?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:
       <?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:
    <?php
       
    function logToFile($filename$msg)
       { 
       
    // open file
       
    $fd fopen($filename"a");
       
    // write string
       
    fwrite($fd$msg "\n");
       
    // close file
       
    fclose($fd);
       }
       
    $v "Logging with PHP";
       if (!
    is_numeric($v)) { logToFile("my.log""Non-numeric variable
     was found"
    ); }
       
    $a = array("chocolate""strawberry""peach");
       if (!
    in_array('fish'$a)) { logToFile("my.log""No fish found"); }
       
    $conn = @mysql_connect("go4expert""user""password");
       if (!
    $conn)
       {
       
    logToFile("my.log""Could not connect to database on go4expert");
       die(
    "Could not connect to database on go4expert");
       }
       
    ?>
    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:
    <?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:

    Code:
       [2002/11/25 06:02:42] Non-numeric variable was found 
       [2002/11/25 06:02:42] No fish found
       [2002/11/25 06:02:43] Could not connect to database on go4expert
     

Share This Page