PHP error handling error log

In some companies, there is a special log collection system. The log collection system will silently help you collect errors, warnings, and prompts behind the scenes.

There are also some companies that do not have a dedicated log collection system and collect the running logs from the server through files.

Among them: PHP errors and warnings must be received.

Then the question comes - if users are not allowed to see it, and the error reporting level is set, how to collect errors into the log system?

Here are the relevant configuration items that need to be used in php.ini. These two configuration items are:

ParameterConfiguration itemDescription
log_errorson/offWhether to enable logging
log_errors_max_lenInteger type, default 1024 Maximum record length of single-line errors
error_logsyslog or specified pathWhere the error log is recorded

Note:

1. The log_errors and log_errors_max_len in the table are very easy to understand.

2. The error_log specifies the path on which the error will be stored. The syslog in the configuration items may be a bit difficult to understand. syslog refers to system recording. Windows system is in the log collector of the computer. Linux defaults to: /etc/syslog.conf

[Extension] Learn about the knowledge points. If the Linux system is started or log collection is modified. May be stored on third-party dedicated log collection servers.

In addition, PHP also specially prepared a custom error log function for us:

bool error_log (string $error message[, int $error message type = 0 [, string $storage Target]] )

This function can send error information to the error log of the web server, or to a file.

Commonly used error message types:

Error message typesDescription
0Send to the default error_log specified location
1Send to the specified email location
3Send to the specified file location


Example:

<?php

//无法连接到数据库服务器,直接记录到php.ini 中的error_log指定位置
error_log("无法连接到数据库服务器服务器");

//可以发送邮件,但是php.ini必须配置过邮件系统
error_log('可以用邮件报告错误,让运维人员半夜起床干活',1 ,'pig@php.cn');

//记录在指定的位置
error_log("我是一个错误哟", 3, "d:/test/my-errors.log");

?>

Note:## Sending emails in #error_log may be unfamiliar to beginners, so you don’t need to master some knowledge.


Continuing Learning
||
<?php //无法连接到数据库服务器,直接记录到php.ini 中的error_log指定位置 error_log("无法连接到数据库服务器服务器"); //可以发送邮件,但是php.ini必须配置过邮件系统 error_log('可以用邮件报告错误,让运维人员半夜起床干活',1 ,'liwenkai@phpxy.com'); //记录在指定的位置 error_log("我是一个错误哟", 3, "d:/test/my-errors.log"); ?>
submitReset Code