How to set up error log in php
Setting method: Open the php.ini file and set "error_reporting=E_ALL", "display_errors=Off", "log_errors=On", "error_log = E:/php_log/php_error.log", etc.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
For PHP developers, once a certain When the project is put into use, the display_errors option in the configuration file php.ini should be turned off immediately to avoid being attacked by hackers due to the path, database connection, data table and other information disclosed by these errors. But after any project is put into use, errors will inevitably occur, so how to record some error reports that are useful to developers?
We can log error reports as logs 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 turn on the configuration item log_errors in the PHP configuration file.
Error reports will be 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, the error log can also be recorded to a specified file.
Use the specified file to record the error report log
If you want to use your own specified file to record the error log, be sure to ensure that this file is stored in outside the document root directory to reduce the possibility of attack. And the file must give the PHP script write permission. 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:
error_reporting = E_ALL // 将会向PHP报告发生的每个错误 display_errors = Off // 不显示满足上条 指令所定义规则的所有错误报告 log_errors = On // 决定日志语句记录的位置 log_errors_max_len = 1024 // 设置每个日志项的最大长度 error_log = E:/php_log/php_error.log // 指定产生的错误报告写入的日志文件位置
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 E:/php_log/php_error.log specified by you.
In addition, not only can all errors that meet the rules defined by error_reporting be recorded, but the error_log() function in PHP can also be used to send error information to the error log of the web server or to a file.
The prototype of the error_log() function is as follows:
error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) : bool
The parameter description is as follows:
- $message: the error message that needs to be recorded ;
- $message_type: Set where errors should be sent. Possible message types are:
- 0: (Default) Send $message to PHP's system log, using the operating system's logging mechanism or a file, depending on what error_log is set in the configuration file ;
- 1: Send $message to the email address set by parameter $destination. The fourth parameter $extra_headers will only be used in this type;
- 2: (deprecated) is no longer an option;
- 3: $message is sent to the location $destination file. The character $message will not be treated as a new line by default;
- 4: Send $message directly to the SAPI log handler.
- $destination: Destination, which is the destination to which the error message is sent. Its meaning is described above, determined by the $message_type parameter;
- $extra_headers: additional headers. Used when $message_type is set to 1. This message type uses the same built-in function of mail().
[Example] Take logging in to the Mysql database as an example, and log an error message when the login fails.
<?php $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); if (!$link) { error_log('Mysql 数据库连接失败!',0); exit(); } ?>
Running the above code will generate the corresponding error log file in the directory set by the error_log item in the php.ini configuration file. The content of the file is as follows:
[08-May-2020 13:17:31 PRC] PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 'my_user'@'localhost' (using password: YES) in D:\WWW\index.php on line 2 [08-May-2020 13:17:31 PRC] Mysql 数据库连接失败!
Recommended learning: " PHP video tutorial》
The above is the detailed content of How to set up error log in php. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



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

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

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

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,

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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.

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.
