Backend Development 6 min read

PHP Logging Functions and Best Practices

This article explains PHP's built‑in logging functions, demonstrates how to use error_log() with various parameters, shows how to extend logging via set_error_handler(), and introduces the Monolog library for advanced logging in backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP Logging Functions and Best Practices

When developing PHP applications, logging is essential for debugging, troubleshooting, and tracking user behavior. This article introduces PHP's built‑in logging functions, their usage, parameters, and configuration.

Logging Functions Overview

PHP provides several built‑in functions for logging:

error_log() : records error messages.

syslog() : writes messages to the system log.

openlog() : opens a connection to the system logger.

closelog() : closes the logger connection.

syslog‑ng : advanced system logger with configuration tools and multiple transport protocols.

In practice, error_log() is the most commonly used function.

Using error_log()

Basic Syntax

<code>bool error_log ( string $message [, int $message_type [, string $destination [, string $extra_headers ]]] )</code>

Parameters:

$message : the message to write.

$message_type : can be 0 (default, write to PHP error log), 1 (send to a file or email), or 2 (write to the system log).

$destination : used when $message_type is 1 (target file or email) or 2 (system log type); ignored for type 0.

$extra_headers : used when $message_type is 1 to specify email headers.

Example of writing to a file:

<code>$logfile = '/path/to/logfile.log';
error_log($message, 3, $logfile);
</code>

Extending Logging with set_error_handler()

The set_error_handler() function allows custom error handling.

Syntax

<code>mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )</code>

$error_handler : user‑defined function or closure that processes the error.

$error_types : optional bitmask of error types to handle (default E_ALL | E_STRICT).

Example:

<code>$logfile = '/path/to/logfile.log';

function my_error_handler($errno, $errstr, $errfile, $errline) {
    $message = "$errno: $errstr in $errfile on line $errline";
    error_log($message, 3, $logfile);
}

set_error_handler('my_error_handler');
</code>

Using Monolog for Advanced Logging

Monolog is a popular third‑party PHP logging library. Install it via Composer:

<code>composer require monolog/monolog</code>

Basic usage:

<code>use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('mylog');
$log->pushHandler(new StreamHandler($logfile, Logger::WARNING));

$log->error('This is an error message.');
</code>

The article concludes that logging is a fundamental feature for any application, and developers can choose between built‑in functions ( error_log() , syslog() , openlog() , closelog() ) or third‑party libraries like Monolog, and can further customize behavior with set_error_handler() to meet specific needs.

Backendloggingerror_logmonologset_error_handler
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.