Home arrow PHP arrow Page 6 - Logging With PHP

Destination Unknown - 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
The Log class also supports sending log messages to the console, the system logger, an SQL database or a user-specified email address. Take a look:

<?php // include class include("Log.php"); // create Log object $l = &Log::singleton("console"); // test it $v = "Mary had a little lamb"; if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); } ?>
In this case, log messages are directed to the text console (if you're running this script via a Web server, the console is the browser window). Here's the output:

Nov 26 22:05:25 [warning] Non-numeric variable encountered Nov 26 22:05:25 [error] No fish available Nov 26 22:05:27 [critical] Could not connect to database
Messages can also be sent to an SQL database,

<?php // include class include("Log.php"); // create Log object // second argument is table name // fourth argument is PHP::DB compatible DSN for database access $l = &Log::singleton("sql", "log_table", "", array('dsn' => 'mysql://joe:pass@localhost/test')); // test it $v = "Mary had a little lamb"; if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); } ?>
which can then be queried to retrieve subsets of the log messages, sorted by date or priority level.

mysql> SELECT * FROM log_table WHERE priority >= 4; +----------------+-------+----------+----------------------------------+ | logtime | ident | priority | message | +----------------+-------+----------+----------------------------------+ | 20021126074936 | | 4 | Non-numeric variable encountered | | 20021126074936 | | 4 | Non-numeric variable encountered | | 20021126074937 | | 4 | Non-numeric variable encountered | +----------------+-------+----------+----------------------------------+ 3 rows in set (0.05 sec)
Finally, log messages can also be directed to a specified email address, as in the following example:

<?php // include class include("Log.php"); // create Log object $l = &Log::singleton("mail", "admin@host.com"); // test it $v = "Mary had a little lamb"; if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); } ?>
You can customize the mail message by adding an array containing a custom From: and Subject: line to the object constructor, as below:

<?php // include class include("Log.php"); // create Log object $l = &Log::singleton("mail", "admin@host.com", NULL, array('from' => 'L. Ogger', 'subject' => 'Log message')); // test it $v = "Mary had a little lamb"; if (!is_numeric($v)) { $l->log("Non-numeric variable encountered", PEAR_LOG_WARNING); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { $l->log("No fish available", PEAR_LOG_ERR); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { $l->log("Could not connect to database", PEAR_LOG_CRIT); } ?>


 
 
>>> 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: