Summary of using different methods to log error reports
In the previous article, I brought you "Take you to understand the error types and error levels of PHP", which introduced the error types and error levels in PHP in detail. In this article we Let’s take a look at the configuration and use of error logs in PHP. I hope everyone has to help!
In our previous article, we introduced the exception handling, error types and error levels of PHP errors. Next, let’s introduce the configuration of error logs in PHP. and how to use it. For PHP developers, once a project is put into use, the display_errors
option in the configuration file php.ini should be turned off immediately to avoid revealing paths, database connections, data tables and other information due to these errors.
In any project that is put into use, errors will inevitably occur. Some error reports are useful to developers. At this time, we can log the error reports through separate text files. . In this way, developers can more easily check whether there are problems with the system. If you enable log_errors
in the PHP configuration file, you can write the error report in the program into the error log.
This error report will be automatically recorded to the server's log file. You can also send it to the system's syslog
, that is, the system log. Next, let’s take a look at how to implement such error handling.
Record error reports by specifying files
If you want to use a target file to record error report logs, it is important to What is needed is that the location of the specified file needs to be outside the document root directory. In this case, the possibility of being attacked is low, and the specified file needs to have certain permissions. First, let's take a look at what we need to target php. How to modify the configuration directive of ini
:
<strong>log_errors = On</strong>
; Determine the location of log statement recording<strong>log_errors_max_len = 1024</strong>
;Set the maximum length of each log entry<strong>error_reporting = E_ALL</strong>
;Every error that occurs will be reported to PHP<strong>display_errors = Off</strong>
;Do not display all error reports that meet the rules defined in the previous command##error_log = /usr/local/error.log<strong></strong> ;Specify the location of the log file where the generated error report is written
error_reporting can be recorded in this target file, and the error information can be placed in the target file through the
error_log() function. The error log in the server or this target file.
error_log() The syntax format of the function is as follows:
error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) : boolWhat needs to be noted is:
$message represents the error message that needs to be recorded;
$destination represents the destination, which is the destination to which the error message is sent. Its meaning is described above and is determined by the
$message_type parameter;
$extra_headers represents additional headers. Used when
$message_type is set to 1. This message type uses the same built-in function of mail().
$message_type indicates where setting errors should be sent. Possible message types are the following:
0
: (Default value) Send
$messageto the PHP system log, use The operating system's logging mechanism or a file, depending on what error_log is set in the configuration file;
1
: Send
$messageto The email address set by the parameter $destination. The fourth parameter $extra_headers will only be used in this type; (2 has been deprecated)
3
:
$messageis sent to the file located at $destination. The character $message will not be treated as a new line by default;
4
: Send
$messagedirectly to the SAPI log handler .
<?php $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { error_log('Mysql 数据库连接失败!',0); exit(); } ?>If we take the problem of logging into the Oracle database as an example, the usage example of this function is as follows:
<?php if(!Ora_Logon($username, $password)){ error_log("Oracle数据库不可用!", 0); //将错误消息写入到操作系统日志中 } if(!($foo=allocate_new_foo()){ error_log("不行!", 1, ". mydomain.com"); //发送到管理员邮箱中 } error_log("不行!", 2, "localhost:5000"); //发送到本机对应5000端口的服务器中 error_log("不行!", 3, "/usr/local/errors.log"); //发送到指定的文件中 ?>
代码运行之后就会在php.ini 配置文件中 error_log
一项所设置的目录中生成对应的错误日志文件。接下来我们看一下错误信息记录到操作系统的日志里是什么情况。
通过系统日志记录错误报告
上文中我们讲到了将使用目标文件来记录错误报告日志,接下来我们就来看一下将错误信息放到操作系统的日志里面,这是可以实现的,其中不同的操作系统,它们的日志管理也是不一样的,下面我们都是使用常见的windows举例,Windows 上错误将发送到事件日志里,可以通过事件查看器来查看。
通过什么样的方法才能够在操作系统的日志里有错误信息呢?这时候我们可以通过php.ini 配置文件中 error_log
,接下来我们看一下应该怎样修改php.ini中的配置文件。
修改error_reporting = E_ALL
用来报告所发生的每个错误;修改display_errors = Off
用来不显示满足上条指令所定义规则的所有错误报告;修改log_errors = On
用于决定日志语句记录的位置;修改log_errors_max_len = 1024
用于设置每个日志项的最大长度;修改error_log = syslog
用于指定产生的错误报告写入操作系统的日志里 。
虽然通过前面介绍的 error_log()
函数,可以向 syslog 中发送定制的消息,想要实现将错误信息放到操作系统的日志里面,我们还需要三个函数的帮助,下面我们就来简单的介绍一下:
<strong>openlog()</strong>
函数
该函数是用来打开连接的,用于向系统中写入日志信息做的准备。并且每个日志的消息中都有它的一个参数是字符串形式的。
<strong>syslog()</strong>
函数
该函数拥有两个参数,它的作用是用来给系统中的日志给一个特定消息,第一个参数就是用来设置这个消息的优先级,第二个参数即使提供字符串,这个字符串就是这个特定的消息。
<strong>closelog()</strong>
函数
该函数就是用来关闭连接的,这个连接就是上文中openlog()
函数打开的。
那么接下来我们通过示例来看一下实际操作吧,示例如下:
<?php openlog("PHP中文网", LOG_PID, LOG_USER); syslog(LOG_WARNING, "向 syslog 中发送定时消息,发送时间:".date("Y/m/d H:i:s")); closelog(); ?>
以windows系统为例,打开“此电脑”右键选择“管理”选项,进入计算机管理界面,找到图示中应用程序的选项,就能够看到我们自己定制的警告信息了。如下所示:
其中我们需要注意的是:
你所使用的Web服务器环境决定了是使用指定文件还是使用syslog记录错误日志。可以控制服务器的话就可以利用解析工具来查看和分析日志,推荐使用syslog 激励错误日志,网站在共享服务器的虚拟主机中运行,推荐使用单独的文本文件记录错误日志了。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of Summary of using different methods to log error reports. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.