PHP 错误日志设置 - MAC
2020.04.26 15:12:14 字数 459 阅读 74
-
如果有错误发生 (触发了错误),默认情况下不会将错误信息记录(保存) 下来。
-
我们可以对此进行设置,以决定以下两点:
# 1、设置 log\_errors 以决定是否记录键误:
# php.ini 中设置:
log_errors = On(开) 或 Off(关);
# 代码文件中设置:
ini_set('log_errors", 1 或 0);
# 2、设置 error_log 以决定记录到哪里:
# 通常,就设置为一个文件名,php 系统会在网站的每个文件夹下都建立该文件,并记录错误。
# php.ini 中设置:
error_log = ./error_log.txt;
# 代码中:
ini_set('error_log', './error_log.txt'); -
php.ini 日志方式,记得重启服务器:
; Besides displaying errors, PHP can also log errors to locations such as a
; server-specific log, STDERR, or a location specified by the error\_log
; directive found below. While errors should not be displayed on productions
; servers they should still be monitored and logging is a great way to do that.
; Default Value: Off
; Development Value: On
; Production Value: On
; http://php.net/log-errors
log\_errors = On
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error\_log = php\_errors.log
; Log errors to syslog (Event Log on Windows).
;error\_log = syslog
error\_log = ./error\_log.txt
- 代码日志方式,下面代码案例,下面网页中输出日志我已经配置了 php.ini 所以代码里面就不用配置了:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta >
<title>Document</title>
</head>
<body>
<?php
// 设置需要存放错误日志
ini_set('log_errors', 'On');
// 设置日志存放路径
ini_set('error_log', './error_log.txt');
// 错误的导入文件以及输出未定义的对象
include 'lib/nav1.html';
echo '<br>当前的页码为:' . $page;
?>
</body>
</html>
-
运行代码之后报错,但是会发现指定的目录中竟然没有生成日志文件,
我们那需要开启文件写入权限:代码文件路径: /Users/dengzemiao/Sites/dzm/php-1/28 - 错误日志设置. php
根据我们配置的日志路径:./error_log.txt
也就是跟代码一个文件夹,那么我们需要打开日志这个文件夹的写入权限
chmod -R 777 /Users/dengzemiao/Sites/dzm/php-1
或
sudo chmod -R 777 /Users/dengzemiao/Sites/dzm/php-1
打开权限之后,再次运行代码,应该就出来日志文件了
image.png
注意:我现在配置的日志目录是服务器根目录 /Users/dengzemiao/Sites 里面,但是我在桌面上随便创建一个文件夹并试图存放日志,也添加了权限,发现无效,所以建议放在配置的根目录里面设置打开权限。
- 无效代码: