Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Logging With PHP
Log script activity in your Web application with PHP's logging functions.

| Destination Unknown |

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                          |
+----------------+-------+----------+----------------------------------+
| 20061126074936 |       |        4 | Non-numeric variable encountered |
| 20061126074936 |       |        4 | Non-numeric variable encountered |
| 20061126074937 |       |        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); }

?>



How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback