php使用error_log函数记录运行日志的使用总结(转帖)
php使用error_log函数记录运行日志的使用总结
(2012-09-26 22:10:37)
转载▼
标签:
php
日志
调试
error_log
access_log
分类: PHP-CI
运行环境:centos+apache+mysql
背景:
http的访问日志可通过apache的access_log和error_log查看。
那么,当需要调试或记录php的运行日志时,如何实现?
syslog()方法?
主要用于记录系统日志,不太适用。
fopen fput fclose?
可以,但操作繁琐
error_log()?
这是目前为止我发现的最好的使用原生函数的方法。
那么就详细介绍下error_log方法。
php手册这样解释:
error_log() 函数向服务器错误记录、文件或远程目标发送一个错误。
若成功,返回 true,否则返回 false。
语法
error_log(error,type,destination,headers)
参数 描述
error 必需。要记录的错误消息。
type
可选。规定错误记录的类型。
可能的记录类型:
0 - 默认。根据在 php.ini 文件中的 error_log 配置,错误被发送到服务器日志系统或文件。
1 - 错误被发送到 destination 参数中的地址。只有该类型使用 headers 参数。
2 - 通过 PHP debugging 连接来发送错误。该选项只在 PHP 3 中可用。
3 - 错误发送到文件目标字符串。
destination 可选。规定向何处发送错误消息。该参数的值依赖于 "type" 参数的值。
headers
可选。只在 "type" 为 1 时使用。
规定附加的头部,比如 From, Cc 以及 Bcc。由 CRLF (\r\n) 分隔。
注释:在发送电子邮件时,必须包含 From 头部。可以在 php.ini 文件中或者通过此参数设置。
举例说明实现过程:
1 我们可以在centos里创建一个文件,用于接收php日志。
touch /usr/local/apache/logs/php_debug_log
chown daemon:daemon /usr/local/apache/logs/php_debug_log
2 php code:
$ok = error_log(date('Y-m-d H:i:s') . 'this is debug string',3,'/usr/local/apache/logs/php_debug_log');
3 通过apache运行php程序,可通过以下命令在linux shell端查看日志记录信息。
tail -f /usr/local/apache/logs/php_debug_log
4 完毕
注意,php_debug_log文件的own和所属组必须为apache配置文件httpd.conf中指定的user和group才能使的error_log()函数有权限写入日志到该文件。
但是 apache的access_log和error_log日志文件由通过root账户启动的apache进程写入,对access_log和error_log的所属者和组无要求,均可写入日志。
最后,感谢小董的支持,才能让我快速掌握这些。
大家对php的日志记录如果有更复杂需求,可适用其他第三方软件实现。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot 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.

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft