Logging in PHP Applications: Best Practices for Beginners
\n\nLogging is the process of recording information about an application's operation into special files (logs). For beginner PHP developers, knowing how to properly configure logging is as important as knowing the language syntax. Logs help:
\n- \n
- Catch errors that are not visible on the screen (for example, in a production environment). \n
- Track user behavior and system operation. \n
- Conduct security audits. \n
- Speed up debugging of complex bugs. \n
In this article, we will cover best practices for logging in PHP: from the built-in error_log() function to using professional libraries like Monolog. You will learn how to avoid cluttering your code, choose the right logging levels, and avoid common beginner mistakes.
1. Basics: Built-in Logging in PHP
\nThe simplest way to write something to a log is to use the error_log() function. It can send messages to the server's system log, a specified file, or an email.
Basic Logging Example
\n<?php\n// Writing to the system log (syslog)\nerror_log('This is an error message');\n\n// Writing to a specific file\nerror_log('Database connection error', 3, '/var/log/my_app_errors.log');\n?>\n\nPros: No need to install libraries, works out of the box.
\nCons:
\n- \n
- No flexibility (cannot easily change the output format). \n
- Difficult to filter by severity level. \n
- Inconvenient when working with multiple channels (file, database, Slack). \n
For simple scripts and learning, this is sufficient, but for serious projects, more advanced solutions should be used.
\n\n2. Logging Levels: When and What to Write
\nTo make logs useful rather than "noise," it is important to classify messages by severity level. The PSR-3 standard (which we will cover below) offers 8 levels:
\n- \n
- DEBUG — Debugging information (variable values, execution time). Used only during development. \n
- INFO — Informational messages (user registered, request processed). \n
- NOTICE — Normal but significant events (use of a deprecated API). \n
- WARNING — Warnings (something went wrong, but the application continues working, e.g., invalid input format). \n
- ERROR — Errors requiring intervention (failed to save a file). \n
- CRITICAL — Critical errors (a core system component is unavailable). \n
- ALERT — Requires immediate action (hack, database failure). \n
- EMERGENCY — System is inoperable. \n
Example of Using Levels (without libraries)
\n<?php\nfunction logMessage($level, $message) {\n $logFile = __DIR__ . '/app.log';\n $timestamp = date('Y-m-d H:i:s');\n $formatted = "[{$timestamp}] [{$level}] {$message}" . PHP_EOL;\n file_put_contents($logFile, $formatted, FILE_APPEND);\n}\n\nlogMessage('INFO', 'User logged in');\nlogMessage('ERROR', 'Failed to connect to the database');\n?>\n\nTip: Never write passwords, tokens, or full SQL queries with user data into logs — this is a security risk.
\n\n3. PSR-3 and Monolog: The Professional Approach
\nPSR-3 is a PHP-FIG recommendation that describes a common interface for logging libraries. The most popular implementation of PSR-3 is Monolog. It is used in Laravel, Symfony, and many other frameworks.
\n\nInstalling Monolog via Composer
\ncomposer require monolog/monolog\n\nSimple Example with Monolog
\n<?php\nrequire 'v