Home Backend Development PHP Tutorial 2.PHP怎么记录异常和发送异常

2.PHP怎么记录异常和发送异常

Jun 13, 2016 pm 12:22 PM
error errors log message set

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>
Copy after login

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>
Copy after login

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>
Copy after login
<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&#160;protected]'</span>);</code>
Copy after login

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Jun 22, 2023 pm 06:40 PM

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 problem of 'error: incomplete type is not allowed' in C++ code Solve the problem of 'error: incomplete type is not allowed' in C++ code Aug 26, 2023 pm 08:54 PM

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

What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? Jun 24, 2023 pm 05:33 PM

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

0271: What should I do if the computer cannot be turned on due to real time clock error? 0271: What should I do if the computer cannot be turned on due to real time clock error? Mar 13, 2023 am 11:30 AM

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: expected initializer before 'datatype'' problem in C++ code Solve the 'error: expected initializer before 'datatype'' problem in C++ code Aug 25, 2023 pm 01:24 PM

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

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

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 PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

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"

Solution to PHP Fatal error: Call to undefined function mysqli_connect() Solution to PHP Fatal error: Call to undefined function mysqli_connect() Jun 23, 2023 am 09:40 AM

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

See all articles