Home arrow PHP arrow Page 3 - Logging With PHP

Turning The Tables - PHP

Want to log script activity in your Web application, but haveno clue where to begin? Take our handy tour of PHP's logging functions,and find out how simple it really is.

TABLE OF CONTENTS:
  1. Logging With PHP
  2. The Bare Necessities
  3. Turning The Tables
  4. Turning Up The Heat
  5. Biting Into A PEAR
  6. Destination Unknown
  7. Artificial Intelligence
  8. Big Brother Is Watching
  9. Closing Time
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 53
November 27, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: