简单的php自定义错误日志
平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。
在写日志前,我们问问自己:为什么我们有时要记录自定义的日志呢?而不用系统默认的日志记录方式呢?
我认为有两个原因:
1.团队需要一个统一格式的日志方便管理
2.大量无用错误日志占据硬盘空间,仅需记录有意义的日志。
那么,实践一下。
1.打开你的php.ini
2.打开日志记录,将
log_errors = Off
改成
log_errors = On
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>
5.试着在刚才的代码后写下一段错误代码
<span style="color: #0000ff;">echo</span> 1/0;
看看你定义的路径下是否多了一个日志文件呢?:)
注:以下级别的错误不能由用户定义的函数来处理: 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开启了全部后,以上的错误都会作为系统错误日志而记录在你定义的文件中。

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
