Home Backend Development PHP Tutorial PHP error logging sharing

PHP error logging sharing

May 23, 2018 pm 05:01 PM
php share Record

For PHP developers, once a product is put into use, the first thing is to turn off the display_errors option to avoid being attacked by hackers due to the paths, database connections, data tables and other information disclosed by these errors. .
After a product is put into use, there will inevitably be error messages, so how to record this information that is very useful to developers?
Just turn on PHP's log_errors. By default, it is recorded to the log file of the WEB server, such as Apache's error.log file.
Of course, you can also record error logs to specified files.

# vim /etc/php.ini
display_errors = Off
log_errors = On
error_log = /var/log/php-error.log
另外也可以设定error_log = syslog,使这些错误信息记录到操作系统的日志里。
Copy after login

---------------------------------------- ----------------
PHP error log For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid being exposed by these errors. Paths, database connections, data tables and other information were hacked. However, after any product is put into use, errors will inevitably occur, so how to record some error reports that are useful to developers? We can log errors in separate text files. The recording of error logs can help developers or managers check whether there are problems with the system. If you need to write error reports in the program to the error log, just enable the configuration directive log_errors in the PHP configuration file. Error reports are logged to the log file of the web server by default, for example, to the error log file error.log of the Apache server. Of course, you can also record the error log to a specified file or send it to the system syslog. The details are as follows:
1. Use the specified file to record the error report log
Use the specified file to record the error report log. Use the specified file to record the error report log. Error report log uses a specified file to record the error report log. If you use a specified file to record the error log, be sure to store this file outside the document root directory to reduce the possibility of being attacked. And the file must be written by the user who executes the PHP script (the owner of the Web server process). Assume that in the Linux operating system, the error.log file in the /usr/local/ directory is used as the error log file, and the Web server process user is set to have write permissions. Then in the PHP configuration file, set the value of the error_log directive to the absolute path of the error log file.
You need to modify the configuration instructions in php.ini as follows:
1. error_reporting = E_ALL; Every error that occurs will be reported to PHP
2. display_errors = Off; Do not display the above instruction. All error reports for defined rules
3. log_errors = On; Determine the location of log statement recording
4. log_errors_max_len = 1024; Set the maximum length of each log entry
5. error_log = /usr/local /error.log ;Specify the location of the log file where the generated error report is written
After the PHP configuration file is set as above, restart the web server. In this way, when executing any PHP script file, all error reports generated will not be displayed in the browser, but will be recorded in the error log /usr/local/error.log specified by you. In addition, not only can all errors that meet the rules defined by error_reporting be recorded, but also a user-defined error message can be sent using the error_log() function in PHP.
The prototype of this function is as follows:
1. bool error_log (string message [, int message_type [, string destination [, string extra_headers]])
This function will send error information to the Web server error Log file, a TCP server or to a specified file. This function returns TRUE if successful and FALSE if failed. The first parameter message is required, which is the error message to be sent. If only this parameter is used, the message will be sent at the location set in the configuration file php.ini. The second parameter message_type is an integer value: 0 means sending it to the log of the operating system; 1 uses PHP's Mail() function to send the message to an E-mail address, and the fourth parameter extra_headers will also be used; 2 means Send the error message to the TCP server. At this time, the third parameter destination represents the destination IP and Port; 3. Save the information to the file destination.
If we take the problem of logging into the Oracle database as an example, the use of this function is as follows:

1. <?php 
2. if(!Ora_Logon($username, $password)){ 
 3. error_log("Oracle数据库不可用!", 0); //将错误消息写入到操作系统日志中 
4. } 
5. if(!($foo=allocate_new_foo()){ 
6. error_log("出现*烦了!", 1, ". mydomain.com"); //发送到管理员邮箱中 
7. } 
8. error_log("搞砸了!", 2, "localhost:5000"); //发送到本机对应5000端口的服务器中 
9. error_log("搞砸了!", 3, "/usr/local/errors.log"); //发送到指定的文件中 
10. ?>
Copy after login

2、 错误信息记录到操作系统的日志里
错 误信息记录到操作系统的日志里错误信息记录到操作系统的日志里错误信息记录到操作系统的日志里 错误报告也可以被记录到操作系统日志里,但不同的操作系统之间的日志管理有点区别。在Linux上错误语句将送往syslog,而在Windows上错误 将发送到事件日志里。如果你不熟悉syslog,起码要知道它是基于UNIX的日志工具,它提供了一个API来记录与系统和应用程序执行有关的消息。 Windows事件日志实际上与UNIX的syslog相同,这些日志通常可以通过事件查看器来查看。如果希望将错误报告写到操作系统的日志里,可以在配 置文件中将error_log指令的值设置为syslog。
具体需要在php.ini中修改的配置指令如下所示:
1. error_reporting = E_ALL ;将会向PHP报告发生的每个错误
2. display_errors = Off ;不显示 满足上条指令所定义规则的所有错误报告
3. log_errors = On ;决定日志语句记录的位置
4. log_errors_max_len = 1024 ;设置每个日志项的最大长度
5. error_log = syslog ;指定产生的错误报告写入操作系统的日志里
除了一般的错误输出之外,PHP还允许向系统syslog中发送定制的消息。虽然通过前面介绍的error_log()函数,也可以向syslog中发送定制的消息,但在PHP中为这个特性提供了需要一起使用的4个专用函数。
分别介绍如下:
define_syslog_variables()
在使用openlog()、syslog及closelog()三个函数之前必须先调用该函数。因为在调用该函数时,它会根据现在的系统环境为下面三个函数初使用化一些必需的常量。
openlog()
打开一个和当前系统中日志器的连接,为向系统插入日志消息做好准备。并将提供的第一个字符串参数插入到每个日志消息中,该函数还需要指定两个将在日志上下文使用的参数,可以参考官方文档使用。
syslog()
该 函数向系统日志中发送一个定制消息。需要两个必选参数,第一个参数通过指定一个常量定制消息的优先级。例如LOG_WARNING表示一般的警 告,LOG_EMERG表示严重地可以预示着系统崩溃的问题,一些其他的表示严重程度的常量可以参考官方文档使用。第二个参数则是向系统日志中发送的定制 消息,需要提供一个消息字符串,也可以是PHP引擎在运行时提供的错误字符串。
closelog()
该函数在向系统日志中发送完成定制消息以后调用,关闭由openlog()函数打开的日志连接。
如果在配置文件中,已经开启向syslog发送定制消息的指令,就可以使用前面介绍的四个函数发送一个警告消息到系统日志中,并通过系统中的syslog解析工具,查看和分析由PHP程序发送的定制消息,如下所示:

1. 
2. define_syslog_variables(); 
3. openlog("PHP5", LOG_PID , LOG_USER); 
4. syslog(LOG_WARNING, "警告报告向syslog中发送的演示, 警告时间:".date("Y/m/d H:i:s")); 
5. closelog(); 
6. ?>
Copy after login

相关推荐:

nginx php-fpm 输出php错误日志的配置方法

记录PHP错误日志 display

详解PHP错误日志的获取方法_PHP

The above is the detailed content of PHP error logging sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles