Error Logging in PHP

How to log php error in to system logger or a file?
or
I want a mail to be send on the occurance of a error?
or
How to send errors to a 3rd party logging system?

Explanation

Php's error_log() function will do this work of logging, mailing and redirecting error messages.
Consider that a database connection fails and so a error has to generated.
a) Send a message by mail: error_log("Your Message",1,"email id")
Log type "1" for error_log() function will be used for mailing.
Code Used: e.g:
//If mysql connection fails error_log will be called
if (!mysql_connect('localhost', 'mysql_user', 'mysql_password'))
{
error_log("connection fails", 1, "support@hioxindia.com");
// Now when ever the connection fails a mail will be send to
// "support@hioxindia.com".
}

b) Send a message to system logger: error_log("Your Message", 0)
Log type "0" for error_log function will be used for system logger.
Code Used: e.g:
//If mysql connection fails error_log will be called
if (!mysql_connect('localhost', 'mysql_user', 'mysql_password'))
{
error_log("My error message", 0);
// as 0 is used, the system logger will be used
}


c) Send a message to a ip and port: error_log("Your Message", 2, "ip:port")
Log type "2" for error_log function will be used for logging error messages in to a ip address.
Code Used: e.g:
//If mysql connection fails error_log will be called
if (!mysql_connect('localhost', 'mysql_user', 'mysql_password'))
{
error_log("My error message", 2, "127.0.0.1:4500");
// as 2 is used, the log message will be send to the specified port of the ip.
}


b) Adding error message in to a file: error_log("Your Message", 4, "filename")
Log type "4" for error_log will appended to the message to file specified.
Code Used: e.g:
//If mysql connection fails error_log will be called
if (!mysql_connect('localhost', 'mysql_user', 'mysql_password'))
{
error_log("My error message", 4, "/home/hscripts/error_log.txt");
}


PHP Topics


Ask Questions

Ask Question