PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Turning The Tables
    ( Page 3 of 9 )

    While the most common destination for log data is a text file, it's quite possible that you might want to send your log messages elsewhere as well. A frequently-used alternative to a text file, especially when the number of writes isn't too high, is an SQL database, where log messages are appended as records to the end of a table.

    In order to illustrate this, consider the following example of an SQL table used to store log messages:

    CREATE TABLE `log` ( `date` TIMESTAMP NOT NULL, `type` TINYINT NOT NULL, `msg` VARCHAR(255) NOT NULL );
    And here's the rewritten logToDB() function, this time built around PHP's MySQL database functions and the table above:

    function logToDB($msg, $type) { // open connection to database $connection = mysql_connect("localhost", "joe", "pass") or die ("Unable to connect!"); mysql_select_db("logdb") or die ("Unable to select database!"); // formulate and execute query $query = "INSERT INTO log (date, type, msg) VALUES(NOW(), '$type', '$msg')"; mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close connection mysql_close($connection); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { logToDB("No fish available", 14); }
    If you don't estimate seeing a very large number of writes, using a database to store logs can offer significant advantages over a regular file, purely from the point of view of easier retrieval and sorting. Storing log messages in this manner makes it possible to easily retrieve ordered subsets of the log data, either by date or message type.

    Another option might be to send log messages to a specified email address via PHP's mail() function - for example, alerting a sysop whenever a script encounters errors. Here's an example:

    function logToMail($msg, $address) { // append date/time to message $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg; mail($address, "Log message", $str); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { logToMail("Could not connect to database", "webmaster@my.domain.name.com"); }


     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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 ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek