2.PHP怎么记录异常和发送异常
2.PHP如何记录错误和发送错误
1. 将错误记录到指定的文件中,配置过程如下
* 1). log_errors=on
* 2). error_log=/tmp/php_error.log
1.使用error_log()函数记录错误日志
error_log(‘要记录的错误信息!’);
<code class="language-php hljs ">header(<span class="hljs-string">'content-type:text/html; charset=utf-8'</span>);<span class="hljs-comment">//开启所有的错误报告</span>error_reporting(-<span class="hljs-number">1</span>);<span class="hljs-comment">//设置时区</span>ini_set(<span class="hljs-string">'date.timezone'</span>, <span class="hljs-string">'PRC'</span>);<span class="hljs-comment">//禁用页面显示错误</span>ini_set(<span class="hljs-string">'display_errors'</span>, <span class="hljs-number">0</span>);<span class="hljs-comment">//开启日志记录功能</span>ini_set(<span class="hljs-string">'log_errors'</span>, <span class="hljs-number">1</span>);<span class="hljs-comment">//设置错误日志保存的位置</span>ini_set(<span class="hljs-string">'error_log'</span>, <span class="hljs-string">'/tmp/custom_error.log'</span>);<span class="hljs-comment">//忽略重复的错误</span>ini_set(<span class="hljs-string">'ignore_repeated_errors'</span>, <span class="hljs-string">'on'</span>);<span class="hljs-comment">//忽略重复的错误来源</span>ini_set(<span class="hljs-string">'ignore_repeated_source'</span>, <span class="hljs-string">'on'</span>);error_log(<span class="hljs-string">'我要把你记录到我的自定义错误日志中'</span>.date(<span class="hljs-string">'Y-m-d H:i:s'</span>, time()));trigger_error(<span class="hljs-string">'trigger_error-----我要把你记录到我的系统错误日志中'</span>.date(<span class="hljs-string">'Y-m-d H:i:s'</span>, time()), E_USER_NOTICE);settype(<span class="hljs-variable">$var</span>, <span class="hljs-string">'king'</span>);</code>
2.将错误记录到系统日志中
* 1.ini_set('error_log', 'syslog');
常用
<code class="language-php hljs ">方法一<span class="hljs-comment">//开启所有的错误报告</span>error_reporting(-<span class="hljs-number">1</span>);<span class="hljs-comment">//禁用页面显示错误</span>ini_set(<span class="hljs-string">'display_errors'</span>, <span class="hljs-number">0</span>);<span class="hljs-comment">//开启日志记录功能</span>ini_set(<span class="hljs-string">'log_errors'</span>, <span class="hljs-number">1</span>);<span class="hljs-comment">//设置错误日志保存的位置------(系统日志)</span>ini_set(<span class="hljs-string">'error_log'</span>, <span class="hljs-string">'syslog'</span>);<span class="hljs-comment">//忽略重复的错误</span>ini_set(<span class="hljs-string">'ignore_repeated_errors'</span>, <span class="hljs-string">'on'</span>);<span class="hljs-comment">//忽略重复的错误来源</span>ini_set(<span class="hljs-string">'ignore_repeated_source'</span>, <span class="hljs-string">'on'</span>);<span class="hljs-comment">// error_log('error_log-----我要把你记录到我的系统错误日志中'.date('Y-m-d H:i:s', time()));</span>trigger_error(<span class="hljs-string">'trigger_error-----我要把你记录到我的系统错误日志中'</span>.date(<span class="hljs-string">'Y-m-d H:i:s'</span>, time()), E_USER_NOTICE);<span class="hljs-comment">// settype($var, 'king');</span>方法二<span class="hljs-comment">//====================出于安全问题不用===========================</span><span class="hljs-comment">//打开到系统日志的链接</span>openlog(<span class="hljs-string">'PHP5.6.0'</span>, LOG_PID, LOG_SYSLOG);syslog(LOG_ERR, <span class="hljs-string">'this is a test syslog'</span>.date(<span class="hljs-string">'Y-m-d H:i:s'</span>, time()));closelog();</code>
2.将错误日志通过邮件发送
* 1.error_log('msg', 1, mail_addr);
第二个参数设置为1
**bool
error_log ( string$message
[, int$message_type = 0
[, string$destination
[, string$extra_headers
]]] )
<code class=" hljs cmake"><span class="hljs-keyword">message</span>应该被记录的错误信息。message_type设置错误应该发送到何处。可能的信息类型有以下几个:error_log() 日志类型<span class="hljs-number">0</span> <span class="hljs-keyword">message</span> 发送到 PHP 的系统日志,使用 操作系统的日志机制或者一个文件,取决于 error_log 指令设置了什么。 这是个默认的选项。<span class="hljs-number">1</span> <span class="hljs-keyword">message</span> 发送到参数 destination 设置的邮件地址。 第四个参数 extra_headers 只有在这个类型里才会被用到。<span class="hljs-number">2</span> 不再是一个选项。<span class="hljs-number">3</span> <span class="hljs-keyword">message</span> 被发送到位置为 destination 的文件里。 字符 <span class="hljs-keyword">message</span> 不会默认被当做新的一行。<span class="hljs-number">4</span> <span class="hljs-keyword">message</span> 直接发送到 SAPI 的日志处理程序中。destination目标。它的含义描述于以上,由 message_type 参数所决定。extra_headers额外的头。当 message_type 设置为 <span class="hljs-number">1</span> 的时候使用。 该信息类型使用了 mail() 的同一个内置函数。</code>
<code class="language-php hljs "><span class="hljs-comment">//开启所有的错误报告</span>error_reporting(-<span class="hljs-number">1</span>);<span class="hljs-comment">//禁用页面显示错误</span>ini_set(<span class="hljs-string">'display_errors'</span>, <span class="hljs-number">0</span>);<span class="hljs-comment">//开启日志记录功能</span>ini_set(<span class="hljs-string">'log_errors'</span>, <span class="hljs-number">1</span>);<span class="hljs-comment">//忽略重复的错误</span>ini_set(<span class="hljs-string">'ignore_repeated_errors'</span>, <span class="hljs-string">'on'</span>);<span class="hljs-comment">//忽略重复的错误来源</span>ini_set(<span class="hljs-string">'ignore_repeated_source'</span>, <span class="hljs-string">'on'</span>);<span class="hljs-comment">//设置第二个参数为1将,使用邮件发送错误日志信息</span>error_log(<span class="hljs-string">'error_log-----我要把你记录到我的系统错误日志中'</span>.date(<span class="hljs-string">'Y-m-d H:i:s'</span>, time()), <span class="hljs-number">1</span>, <span class="hljs-string">[email protected]'</span>);</code>
版权声明:本文为博主原创文章,未经博主允许不得转载。

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 is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

Solve the "error:incompletetypeisnotallowed" problem in C++ code. During the C++ programming process, you sometimes encounter some compilation errors. One of the common errors is "error:incompletetypeisnotallowed". This error is usually caused by operating on an incomplete type. This article will explain the cause of this error and provide several solutions. firstly, I

It is very common to use axios in Vue applications. axios is a Promise-based HTTP client that can be used in browsers and Node.js. During the development process, the error message "Uncaught(inpromise)Error: Requestfailedwithstatuscode500" sometimes appears. For developers, this error message may be difficult to understand and solve. This article will explore this

Solution to "0271: real time clock error" that cannot boot: 1. Press F1, and in the interface that appears, move the option bar to the third item "Date/Time"; 2. Manually change the system time to the current one time; 3. Press F10 and select yes in the pop-up dialog box; 4. Re-open the notebook to boot normally.

Solve the "error:expectedinitializerbefore'datatype'" problem in C++ code. In C++ programming, sometimes we encounter some compilation errors when writing code. One of the common errors is "error:expectedinitializerbefore'datatype'". This error usually occurs in a variable declaration or function definition and may cause the program to fail to compile correctly or

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my
