The ultimate logging series: Logging using PHP functions

php functions blog banner

In part one of our PHP logging blog series, we discussed what logging is and covered the basics of creating logs in PHP applications using the PHP system logger. While the PHP system logger automatically records critical events like errors in code-execution, a more customized logging setup can be achieved using PHP functions.

For part two, let's look at the basics of creating custom error logs by calling PHP functions.

error_reporting()

The error_reporting() function sets the error_reporting directive during the runtime of the script. If the optional error_level is not set, error_reporting() will return to the current error reporting level.

Syntax

error_reporting(int level);

  • Level: This is an optional field. The parameter can either be an integer representing a bit field or named constants.

Error reporting imagesSource: php.net 

Return value

This returns to the old error_reporting level or to the current level if the level parameter is not given.

Usage

<?php

error_reporting(E_ALL & ~E_WARNING);

echo "Current error reporting level is " . error_reporting();

Output

error reporting use case

error_log()

The error_log() function sends an error message to the error handler defined in the error_log directive of the php.ini configuration file. Timestamps are added automatically to each log.

Syntax

error_log(string message, int type, string destination, string headers);

  • Message: This is a mandatory field. This parameter contains the error message that needs to be logged.

  • Type: This is an optional field. This parameter tells you where the error message should go.

 error logSource: php.net

  • Destination: This is an optional field. This parameter defines the destination of the error message. The destination parameter is dependent on the value of the type field.

  • Headers: This is an optional field for the extra headers. It's used when the type field is set to 1. This message type uses the same internal function as mail() does.

Return value

The return value depends on whether the parameters are accepted or not. The function will return True on success and False on failure. For example, if the type field is set to 0, this function always returns True, regardless of whether the error is logged or not, as type is an optional field.

Usage

In the example below, the error_log directive in the php.ini configuration file is set to a file name.

<?php

error_log("Reporting this error");

Output

error log output

syslog()

The syslog() function sends the log message to the system logger. The syslog() function can be used along with the openlog() and closelog() functions if required, however, this is not mandatory since these functions are automatically called by the syslog() function when needed.

Syntax

syslog(int priority, string message);

  • Priority: This is a mandatory field. This parameter indicates the priority level of the log message.

    Syslog functionsSource: php.net 
  • Message: This is a mandatory field. This parameter contains the message that needs to be logged.

Return value

The return type of this function is Boolean. This function returns True on success and False on failure.

Usage

<?php

syslog(LOG_WARNING, "Logging something here...");

Output

syslog output

openlog()

The openlog() function is used to open a connection to the system logger. This function will be automatically called by the syslog() function if necessary, making the openlog() function optional. If the openlog() function is not called explicitly, the first parameter of the openlog() function is set as false by default.

Syntax

openlog(string prefix, int flags, int facility);

  • Prefix: This is a mandatory field. The string passed to the prefix will be attached to each log message.

  • Flags: This is a mandatory field. Options to be used to generate the log messages should be passed here. More than one flag can be set here using the OR functionality.

open log optionsSource: php.net

  • Facility: This is a mandatory field. The value passed to this parameter is used to specify what type of program is logging the message.

open log facilities

Return value

The return type of this function is boolean. This function returns true on sucess and false on failure.

Usage

<?php

openlog("mysyslog", LOG_PID | LOG_PERROR, LOG_USER);

syslog(LOG_WARNING, "Logging something here...");

closelog();

Output

open logs output

As we previously mentioned, logging is crucial to gaining complete visibility of your application and understanding why an error occurred. In PHP, you can configure logs to suit your specifications and send all the collected data back to a monitoring tool to get a detailed report on your application's health and troubleshoot issues effectively.

Site24x7's log management tool, AppLogs, helps you manage all logs on a single dashboard, after which you can send the relevant information to our APM tool to help optimize the performance of your application.

Want to give our tool a try? Sign up for a free, 30-day trial here. 

Comments (0)