平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。 在写日志前,我们问问自己 :为什么我们有时要记录自定义的日志呢?而不用系统默认的日志记录方式呢? 我认为有两
平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。
在写日志前,我们问问自己:为什么我们有时要记录自定义的日志呢?而不用系统默认的日志记录方式呢?
我认为有两个原因:
1.团队需要一个统一格式的日志方便管理
2.大量无用错误日志占据硬盘空间,仅需记录有意义的日志。
那么,实践一下。
log_errors = Off
改成
log_errors = On
<span> 1</span> <span>php </span><span> 2</span> <span> 3</span> <span>//</span><span>错误处理函数</span> <span> 4</span> <span>function</span> myErrorHandler(<span>$errno</span>, <span>$errstr</span>, <span>$errfile</span>, <span>$errline</span><span>) </span><span> 5</span> <span>{ </span><span> 6</span> <span>$log_file</span> = "./php_%s_log_".<span>date</span>("Ymd").".log";<span>//</span><span>定义日志文件存放目录和文件名</span> <span> 7</span> <span>$template</span> = ''<span>; </span><span> 8</span> <span>switch</span> (<span>$errno</span><span>) { </span><span> 9</span> <span>case</span> <span>E_USER_ERROR</span>: <span>10</span> <span>$template</span> .= "用户ERROR级错误,必须修复 错误编号[<span>$errno</span>] <span>$errstr</span> "<span>; </span><span>11</span> <span>$template</span> .= "错误位置 文件<span>$errfile</span>,第 <span>$errline</span> 行\n"<span>; </span><span>12</span> <span>$log_file</span> = <span>sprintf</span>(<span>$log_file</span>,'error'<span>); </span><span>13</span> <span>exit</span>(1);<span>//</span><span>系统退出</span> <span>14</span> <span>break</span><span>; </span><span>15</span> <span>16</span> <span>case</span> <span>E_USER_WARNING</span>: <span>17</span> <span>$template</span> .= "用户WARNING级错误,建议修复 错误编号[<span>$errno</span>] <span>$errstr</span> "<span>; </span><span>18</span> <span>$template</span> .= "错误位置 文件<span>$errfile</span>,第 <span>$errline</span> 行\n"<span>; </span><span>19</span> <span>$log_file</span> = <span>sprintf</span>(<span>$log_file</span>,'warning'<span>); </span><span>20</span> <span>break</span><span>; </span><span>21</span> <span>22</span> <span>case</span> <span>E_USER_NOTICE</span>: <span>23</span> <span>$template</span> .= "用户NOTICE级错误,不影响系统,可不修复 错误编号[<span>$errno</span>] <span>$errstr</span> "<span>; </span><span>24</span> <span>$template</span> .= "错误位置 文件<span>$errfile</span>,第 <span>$errline</span> 行\n"<span>; </span><span>25</span> <span>$log_file</span> = <span>sprintf</span>(<span>$log_file</span>,'notice'<span>); </span><span>26</span> <span>break</span><span>; </span><span>27</span> <span>28</span> <span>default</span>: <span>29</span> <span>$template</span> .= "未知错误类型: 错误编号[<span>$errno</span>] <span>$errstr</span> "<span>; </span><span>30</span> <span>$template</span> .= "错误位置 文件<span>$errfile</span>,第 <span>$errline</span> 行\n"<span>; </span><span>31</span> <span>$log_file</span> = <span>sprintf</span>(<span>$log_file</span>,'unknown'<span>); </span><span>32</span> <span>break</span><span>; </span><span>33</span> <span> } </span><span>34</span> <span>file_put_contents</span>(<span>$log_file</span>,<span>$template</span>,<span>FILE_APPEND); </span><span>35</span> <span>36</span> <span>return</span> <span>true</span><span>; </span><span>37</span> <span>} </span><span>38</span> <span>39</span> <span>$error_handler</span> = <span>set_error_handler</span>("myErrorHandler");<span>//</span><span>开启自定义错误日志</span>
<span>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开启了全部后,以上的错误都会作为系统错误日志而记录在你定义的文件中。