Table of Contents
1.打开你的php.ini" > 1.打开你的php.ini
2.打开日志记录,将" > 2.打开日志记录,将
3.将php.ini保存退出并重启web服务器" >3.将php.ini保存退出并重启web服务器
4.在你的代码最前面加上如下代码" >4.在你的代码最前面加上如下代码
5.试着在刚才的代码后写下一段错误代码" > 5.试着在刚才的代码后写下一段错误代码
Home Backend Development PHP Tutorial 简单的php自定义异常日志

简单的php自定义异常日志

Jun 13, 2016 pm 12:14 PM
error file log template warning

简单的php自定义错误日志

平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。

在写日志前,我们问问自己:为什么我们有时要记录自定义的日志呢?而不用系统默认的日志记录方式呢?

我认为有两个原因:

1.团队需要一个统一格式的日志方便管理

2.大量无用错误日志占据硬盘空间,仅需记录有意义的日志

那么,实践一下。


1.打开你的php.ini

2.打开日志记录,将

log_errors = Off
Copy after login

改成

log_errors = On
Copy after login

3.将php.ini保存退出并重启web服务器

4.在你的代码最前面加上如下代码

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">错误处理函数</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">function</span> myErrorHandler(<span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errstr</span>, <span style="color: #800080;">$errfile</span>, <span style="color: #800080;">$errline</span><span style="color: #000000;">)</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 6</span>     <span style="color: #800080;">$log_file</span> = "./php_%s_log_".<span style="color: #008080;">date</span>("Ymd").".log";<span style="color: #008000;">//</span><span style="color: #008000;">定义日志文件存放目录和文件名</span><span style="color: #008080;"> 7</span>     <span style="color: #800080;">$template</span> = ''<span style="color: #000000;">;</span><span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$errno</span><span style="color: #000000;">) {</span><span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">case</span> <span style="color: #ff00ff;">E_USER_ERROR</span>:<span style="color: #008080;">10</span>         <span style="color: #800080;">$template</span> .= "用户ERROR级错误,必须修复 错误编号[<span style="color: #800080;">$errno</span>] <span style="color: #800080;">$errstr</span> "<span style="color: #000000;">;</span><span style="color: #008080;">11</span>         <span style="color: #800080;">$template</span> .= "错误位置 文件<span style="color: #800080;">$errfile</span>,第 <span style="color: #800080;">$errline</span> 行\n"<span style="color: #000000;">;</span><span style="color: #008080;">12</span>         <span style="color: #800080;">$log_file</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$log_file</span>,'error'<span style="color: #000000;">);</span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">exit</span>(1);<span style="color: #008000;">//</span><span style="color: #008000;">系统退出</span><span style="color: #008080;">14</span>         <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>     <span style="color: #0000ff;">case</span> <span style="color: #ff00ff;">E_USER_WARNING</span>:<span style="color: #008080;">17</span>         <span style="color: #800080;">$template</span> .= "用户WARNING级错误,建议修复 错误编号[<span style="color: #800080;">$errno</span>] <span style="color: #800080;">$errstr</span> "<span style="color: #000000;">;</span><span style="color: #008080;">18</span>         <span style="color: #800080;">$template</span> .= "错误位置 文件<span style="color: #800080;">$errfile</span>,第 <span style="color: #800080;">$errline</span> 行\n"<span style="color: #000000;">;</span><span style="color: #008080;">19</span>         <span style="color: #800080;">$log_file</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$log_file</span>,'warning'<span style="color: #000000;">);</span><span style="color: #008080;">20</span>         <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span>     <span style="color: #0000ff;">case</span> <span style="color: #ff00ff;">E_USER_NOTICE</span>:<span style="color: #008080;">23</span>         <span style="color: #800080;">$template</span> .= "用户NOTICE级错误,不影响系统,可不修复 错误编号[<span style="color: #800080;">$errno</span>] <span style="color: #800080;">$errstr</span> "<span style="color: #000000;">;</span><span style="color: #008080;">24</span>         <span style="color: #800080;">$template</span> .= "错误位置 文件<span style="color: #800080;">$errfile</span>,第 <span style="color: #800080;">$errline</span> 行\n"<span style="color: #000000;">;</span><span style="color: #008080;">25</span>     <span style="color: #800080;">$log_file</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$log_file</span>,'notice'<span style="color: #000000;">);</span><span style="color: #008080;">26</span>         <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span>     <span style="color: #0000ff;">default</span>:<span style="color: #008080;">29</span>         <span style="color: #800080;">$template</span> .= "未知错误类型: 错误编号[<span style="color: #800080;">$errno</span>] <span style="color: #800080;">$errstr</span>  "<span style="color: #000000;">;</span><span style="color: #008080;">30</span>         <span style="color: #800080;">$template</span> .= "错误位置 文件<span style="color: #800080;">$errfile</span>,第 <span style="color: #800080;">$errline</span> 行\n"<span style="color: #000000;">;</span><span style="color: #008080;">31</span>         <span style="color: #800080;">$log_file</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$log_file</span>,'unknown'<span style="color: #000000;">);</span><span style="color: #008080;">32</span>         <span style="color: #0000ff;">break</span><span style="color: #000000;">;</span><span style="color: #008080;">33</span> <span style="color: #000000;">    }</span><span style="color: #008080;">34</span>     <span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$log_file</span>,<span style="color: #800080;">$template</span>,<span style="color: #000000;">FILE_APPEND);</span><span style="color: #008080;">35</span> <span style="color: #008080;">36</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">37</span> <span style="color: #000000;">}</span><span style="color: #008080;">38</span> <span style="color: #008080;">39</span> <span style="color: #800080;">$error_handler</span> = <span style="color: #008080;">set_error_handler</span>("myErrorHandler");<span style="color: #008000;">//</span><span style="color: #008000;">开启自定义错误日志</span>
Copy after login

5.试着在刚才的代码后写下一段错误代码

<span style="color: #0000ff;">echo</span> 1/0;
Copy after login

看看你定义的路径下是否多了一个日志文件呢?:)

以下级别的错误不能由用户定义的函数来处理: E_ERROR、 E_PARSE、 E_CORE_ERROR、 E_CORE_WARNING、 E_COMPILE_ERROR、 E_COMPILE_WARNING,和在 调用 set_error_handler() 函数所在文件中产生的大多数 E_STRICT。

不过当你开启了错误日志系统(php.ini中的log_error = on)并且指定了系统日志文件(同样也是php.ini中的error_log=路径名),并且error_reporting开启了全部后,以上的错误都会作为系统错误日志而记录在你定义的文件中。

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)

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

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

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

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

How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

PHP Warning: Invalid argument supplied for foreach() - Solution PHP Warning: Invalid argument supplied for foreach() - Solution Aug 26, 2023 pm 09:42 PM

PHPWarning:Invalidargumentsuppliedforforeach()-Solution When developing web pages or applications using PHP, you often encounter various errors and warnings. One of the common warnings is "Invalidargumentsuppliedforforeach()", which is usually produced when using a foreach loop to iterate over an array. This question seems simple, but if you don't

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

See all articles