In this third part of a five-part series on error and exception handling, you'll learn how to log errors and what role efficient exception handling plays in this this. This article is exceprted from chapter eight of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
Error Logging
If you’ve decided to log your errors to a separate text file, the Web server process owner must have adequate permissions to write to this file. In addition, be sure to place this file outside of the document root to lessen the likelihood that an attacker could happen across it and potentially uncover some information that is useful for surreptitiously entering your server. When you write to the syslog, the error messages look like this:
Dec 5 10:56:37 example.com httpd: PHP Warning: fopen(/home/www/htdocs/subscribers.txt): failed to open stream: Permission denied in /home/www/htdocs/book/8/displayerrors.php on line 3
When you write to a separate text file, the error messages look like this:
[05-Dec-2005 10:53:47] PHP Warning: fopen(/home/www/htdocs/subscribers.txt): failed to open stream: Permission denied in /home/www/htdocs/book/8/displayerrors.php on line 3
As to which one to use, that is a decision that you should make on a per-environment basis. If your Web site is running on a shared server, then using a separate text file or database table is probably your only solution. If you control the server, then using the syslog may be ideal, because you’d be able to take advantage of a syslog-parsing utility to review and analyze the logs. Take care to examine both routes and choose the strategy that best fits the configuration of your server environment.
PHP enables you to send custom messages as well as general error output to the system syslog. Four functions facilitate this feature. These functions are introduced in this section, followed by a concluding example.
define_syslog_variables()
void define_syslog_variables(void)
Thedefine_syslog_variables()function initializes the constants necessary for using theopenlog(),closelog(), andsyslog()functions. You need to execute this function before using any of the following logging functions.
openlog()
int openlog(string ident, int option, int facility)
Theopenlog()function opens a connection to the platform’s system logger and sets the stage for the insertion of one or more messages into the system log by designating several parameters that will be used within the log context:
ident: A message identifier added to the beginning of each entry. Typically this value is set to the name of the program. Therefore, you might want to identify PHP-related messages as “PHP” or “PHP5”.
option: Determines which logging options are used when generating the message. A list of available options is offered in Table 8-2. If more than one option is required, separate each option with a vertical bar. For example, you could specify three of the options like so:LOG_ODELAY | LOG_PERROR | LOG_PID.
facility: Helps determine what category of program is logging the message. There are several categories, includingLOG_KERN,LOG_USER,LOG_MAIL,LOG_DAEMON,LOG_AUTH,LOG_LPR, andLOG_LOCALN, whereNis a value ranging between 0 and 7. Note that the designated facility determines the message destination. For example, designatingLOG_CRONresults in the submission of subsequent messages to thecronlog, whereas designatingLOG_USERresults in the transmission of messages to themessagesfile. Unless PHP is being used as a command-line interpreter, you’ll likely want to set this toLOG_USER. It’s common to useLOG_CRONwhen executing PHP scripts from acrontab. See the syslog documentation for more information about this matter.
Table 8-2. Logging Options
Option
Description
LOG_CONS
If error occurs when writing to the syslog, send output to the system console.
LOG_NDELAY
Immediately open the connection to the syslog.
LOG_ODELAY
Do not open the connection until the first message has been submitted for logging. This is the default.
LOG_PERROR
Output the logged message to both the syslog and standard error.
LOG_PID
Accompany each message with the process ID (PID).
closelog()
int closelog(void)
Thecloselog()function closes the connection opened byopenlog().
syslog()
int syslog(int priority, string message)
Thesyslog()function is responsible for sending a custom message to the syslog. The first parameter,priority, specifies the syslog priority level, presented in order of severity here:
LOG_EMERG: A serious system problem, likely signaling a crash
LOG_ALERT: A condition that must be immediately resolved to avert jeopardizing system integrity
LOG_CRIT: A critical error, which could render a service unusable but does not necessarily place the system in danger
LOG_ERR: A general error
LOG_WARNING: A general warning
LOG_NOTICE: A normal but notable condition
LOG_INFO: General informational message
LOG_DEBUG: Information that is typically only relevant when debugging an application
The second parameter,message, specifies the text of the message that you’d like to log. If you’d like to log the error message as provided by the PHP engine, you can include the string%min the message. This string will be replaced by the error message string (strerror) as offered by the engine at execution time.
Now that you’ve been acquainted with the relevant functions, here’s an example: