search
HomeBackend DevelopmentPHP TutorialPHP error logging sharing

PHP error logging sharing

May 23, 2018 pm 05:01 PM
phpshareRecord

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,使这些错误信息记录到操作系统的日志里。

---------------------------------------- ----------------
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. ?>

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. ?>

相关推荐:

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
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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version