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.
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.
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);
}
?>