Logging With PHP - Destination Unknown
(Page 6 of 9 )
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);
}
?>
Next: Artificial Intelligence >>
More PHP Articles
More By icarus, (c) Melonfire