Introduction: Why Do You Need Logging in PHP?
Logging is not just about recording errors to a file. It is your main tool for monitoring, debugging, and analyzing the operation of a web application. Without quality logging, you will be guessing why the site crashed or why a user cannot place an order. For beginner PHP developers, setting up proper logging is the first step towards professional development.
Imagine your application is a black box. Logs are the only way to look inside and understand what is happening at any given moment. In this article, we will cover best practices that will help you avoid common beginner mistakes and build a reliable monitoring system.
1. Basics: Built-in PHP Functions for Logging
PHP provides several built-in functions for working with logs. The simplest and most popular is error_log(). It allows you to send a message to the server's system log or to a specified file.
Basic example of using error_log()
// Simplest way to write an error to the system logerror_log("This is an error message", 0);
// Writing to a specific fileerror_log("Database connection error", 3, "/var/log/php_errors.log");
// Writing with a message type specifiederror_log("Warning: slow request", 4);However, using error_log() directly in production is not a good idea. First, you mix different types of messages. Second, it is difficult to manage the format and log rotation. For serious projects, it is better to use specialized libraries.
Configuring php.ini for improved logging
Before writing code, make sure your php.ini is configured correctly:
; Enable error logginglog_errors = On; Path to the log fileerror_log = /var/log/php_errors.log; Maximum log size (in bytes)log_errors_max_len = 1024; Disable displaying errors on screen (for production)display_errors = Off; Enable logging of all error levelserror_reporting = E_ALL2. Logging Levels: From DEBUG to CRITICAL
One of the main mistakes beginners make is recording all messages in one pile. Professional logging uses severity levels. This helps to quickly filter logs and not miss critical errors among informational messages.
Standard PSR-3 Levels
The PHP community has adopted the PSR-3 standard, which defines 8 logging levels:
- DEBUG — detailed debug information (used only in development)
- INFO — informational messages (user registered, order created)
- NOTICE — normal but significant events (use of deprecated functions)
- WARNING — warnings (e.g., slow request, low disk space)
- ERROR — errors requiring attention (failed to connect to database)
- CRITICAL — critical errors (main system component unavailable)
- ALERT — requires immediate intervention (hack, system failure)
- EMERGENCY — system is inoperable
Example of implementing levels with Monolog
The best library for logging in PHP is Monolog. It fully complies with PSR-3 and is easy to configure:
use Monolog\\Logger;use Monolog\\Handler\\StreamHandler;use Monolog\\Handler\\RotatingFileHandler;use Monolog\\Level;
// Create a logger with a channel name$log = new Logger('app');
// Add a handler for all levels$log->pushHandler(new StreamHandler('/var/log/app.log', Level::Debug));
// Separate file for critical errors$log->pushHandler( new RotatingFileHandler('/var/log/critical.log', 30, Level::Critical));
// And