环境
- Windows 11 专业版
- XAMPP v3.3.0
- PHP 8.2.12
总结
用法(默认参数):
error_log('xxxxxx');
默认参数时,error_log()
的输出目标位置是在 php.ini
里配置的,例如:
error_log="C:\xampp\php\logs\php_error_log"
- 如果
C:\xampp\php\logs
目录存在,则error_log()
会输出到该目录的php_error_log
文件里 - 如果
C:\xampp\php\logs
目录不存在,则:- CLI:输出到stderr
- Apache:输出到Apache的error log(比如
C:\xampp\apache\logs\error.log
) php think run
:输出到该命令运行的那个窗口里
特定: error_log()
小巧灵活,简单粗暴,适用于小型项目或者临时的调试。
用法
error_log()
是PHP内置函数,其作用是发送特定信息到指定位置。该位置可能是标准错误输出(stderr),也可能是web server的log,或者是指定文件。
其详细用法和参数设置,参见官方文档 https://www.php.net/manual/en/function.error-log.php
:
error_log(
string $message,
int $message_type = 0,
?string $destination = null,
?string $additional_headers = null
): bool
本文不对其做深入研究,只介绍其默认参数的用法。
CLI
创建PHP文件 test1.php
如下:
<?php
echo 'OK';
error_log('test1');
运行脚本,结果如下:
注意 test1
的字体是红色的,因为它是stderr的输出。
可见,在CLI环境下, error_log()
输出到了stderr。
实际上, error_log()
的默认输出并不是stderr,而是PHP的系统log。
查看 php.ini
文件(注:可用 php --ini
命令查看文件位置),查找 error_log
配置:
PS C:\temp\test0902> cat C:\xampp\php\php.ini | wsl grep error_log
; server-specific log, STDERR, or a location specified by the error_log
;error_log = php_errors.log
;error_log = syslog
; to syslog. Only used when error_log is set to syslog.
; the message. Only used when error_log is set to syslog.
error_log="C:\xampp\php\logs\php_error_log"
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
注:跑个题,我是在PowerShell下运行的 cat
命令,但是PowerShell下没有 grep
命令,不过我安装了WSL,所以我混用了 cat
命令和WSL的 grep
命令:
cat C:\xampp\php\php.ini | wsl grep error_log
回到正题,可见, error_log()
的输出,是 C:\xampp\php\logs\php_error_log
。
然而 C:\xampp\php\logs
目录并不存在,则PHP并不会自动创建该目录,而是退而求其次,把 error_log()
输出到了stderr。
倘若 C:\xampp\php\logs
目录存在,则PHP会把 error_log()
输出到 php_error_log
文件(若文件不存在则会自动创建),而不会输出到stderr。
其它
要想输出信息到stderr,可以使用 fwrite()
函数:
fwrite(STDERR, "This is an error message!");
Apache
首先,如果存在 C:\xampp\php\logs
目录,则 error_log()
会输出到 php_error_log
文件。
如果该目录不存在,则 error_log()
会输出到Apache的error log里,比如: C:\xampp\apache\logs\error.log
。
创建ThinkPHP项目 test0831
,在 app/controller/Index.php
里添加 test5()
方法:
public function test5() {
error_log('test5');
}
在浏览器里访问 http://localhost/test0831/public/index/test5
,然后查看 C:\xampp\apache\logs\error.log
:
[Tue Sep 02 15:28:49.343006 2025] [php:notice] [pid 14908:tid 1904] [client 127.0.0.1:58900] test5
php think run
首先,如果存在 C:\xampp\php\logs
目录,则 error_log()
会输出到 php_error_log
文件。
如果该目录不存在,则 error_log()
会输出到 php think run
的那个窗口里。
同上,在浏览器里访问 http://localhost:8000/index/test5
,然后查看 php think run
的那个窗口:
error_log VS. ThinkPHP框架的log
error_log()
是PHP语言本身的,ThinkPHP的log则是ThinkPHP框架的。
那么问题来了,二者有什么不同?各自适合什么应用场景呢?
error_log()
的特点是:
- 简单直接:PHP语言自带,不依赖任何框架和包
- 灵活:可以输出到文件、邮件、系统日志等多种目的地
- 功能单一:没有log级别,没有log轮转、文件大小限制等管理功能
- 性能影响:每次调用都会直接进行I/O操作,而ThinkPHP的
Log::record()
会在请求结束后统一写入log
由此看来, error_log()
更适合快速、临时的调试,比如小型项目,没有框架log,或者临时查看一个东西,不想混在茫茫的log内容之中,总结起来就是“简单粗暴”,至于大型项目,生产环境的正式log,还是应该使用专业的log框架。
参考
https://www.php.net/manual/en/function.error-log.php
https://www.php.net/manual/zh/function.error-log.php