The ultimate logging series: Using the PHP system logger
Logging is essential to application development. Logs provide exhaustive, robust information that is useful for tracking all the changes made to an application's code. PHP logs help you track the performance of the method calls within your application, the occurrence of a particular event, and the errors in your application. With proper PHP logging techniques, you can track and optimize an application's performance.
In part one of this two-part series, let us look at logging, why we need it in application monitoring, and how to create logs using the PHP system logger.
What is logging, and why is it a necessary addition to monitoring?
Logging is the process of systematically collecting and preserving application error data in centralized storage. It helps identify application performance issues so you can rectify them and ensure optimal performance and availability. Logging goes hand in hand with monitoring as a complementary process that improves application observability.
Using logging and monitoring together helps you gather a broad range of data to measure application performance and troubleshoot when errors are recorded. In other words, while application performance monitoring helps answer the how of an application's performance, logging answers why a certain issue happened to provide maximum clarity.
Log creation in PHP
Logs are created in two ways in PHP. Logs can be created automatically by using the PHP system logger if there is an error in the code during execution. You can also use PHP functions to create custom logs. In this blog, let us look at how to create and customize logs using the PHP system logger.
Using the PHP system logger
display_errors
Log messages can be displayed in the browser using the directive display_errors in the php.ini configuration file. It is advisable to have display_errors set to Off to ensure maximum security. However, it can be set to On in the development environment to make it easier for the developers to debug the application.
Usage
To display errors during development:
display_errors = On
To disable display_errors:
display_errors = Off
Sample script
<?php
trigger_error("User-generated warning message.",E_USER_WARNING);
trigger_error("User-generated notice message.",E_USER_NOTICE);
Use case 1
If the display_errors directive is set to show log messages in the browser:
display_errors = On
display_startup_errors
Occasionally, errors may occur during PHP's startup sequence. These startup errors will not be displayed, even when the display_errors directive is on. Startup errors can be viewed by enabling the display_startup_errors feature. However, it is ideal to enable this feature only in the development environment and disable it at all other times to avoid security mishaps.
Usage
To display startup errors:
display_startup_errors = On
To disable the startup errors display:
display_startup_errors = Off
error_reporting
Use the error_reporting directive to log specific errors in PHP. You can set the error reporting level by passing the appropriate parameter (an integer representing a bit field or named constants) to the error_reporting directive. This directive uses a set of predefined constants and bitwise operators to express what types of events to include and exclude from logs. For example, you would use the E_ALL directive to log all errors.
Source: php.net
Usage
To report all errors:
error_reporting = E_ALL
To report all errors except E_NOTICE:
error_reporting = E_ALL & ~E_NOTICE
Sample script
<?php
trigger_error("User-generated warning message.",E_USER_WARNING);
trigger_error("User-generated notice message.",E_USER_NOTICE);
Use case 1
If the error_reporting directive is set to report all errors:
error_reporting = E_ALL
Output
Use case 2
If you want to set the error_reporting directive to report all errors except user-generated notices, you can use the following command:
error_reporting = E_ALL & ~E_USER_NOTICE
Output
error_log
By passing a file name to the error_log directive in the php.ini configuration file, you can write logs to that specific file. If the error_log directive is set to syslog, it generates a log message that the system logger will distribute. If error_log is unset, logs are created using the server API. For example, if the platform uses Apache as the server, logs are written to Apache’s error log.
Usage
To write logs to a file:
error_log = /var/log/error.log
To unset the error_log directive, add a semicolon (;) in front of the error_log directive or remove the directive:
;error_log = /var/log/error.log
Sample script
<?php
trigger_error("User-generated warning message.",E_USER_WARNING);
trigger_error("User-generated notice message.",E_USER_NOTICE);
Use case 1
To set the error_log directive to a file name (/var/log/sample.log), use the following command:
error_log = /var/log/sample.log
Output
Use case 2
If the error_log directive is unset and the platform uses Apache as the server:
;error_log = /var/log/error.log
Output
PHP logging allows you to capture useful information that is required to effectively monitor and troubleshoot your application. As a best practice, logging should be conducted regularly in application monitoring. You can gain a detailed view of everything in your application that needs troubleshooting and use this information to work on these issues.
AppLogs is a log management service by Site24x7 that helps you manage logs on a single dashboard. Also, with Site24x7's APM solution, you can capture vital data from your PHP application and effectively optimize its performance. Want to find out how to monitor your application effectively? Sign up for a free, 30-day trial here.
Update: To learn about creating and customizing logs using PHP functions, read part two of our blog series by clicking here.