search
Homephp教程php手册php使用error_log函数记录运行日志的使用总结(转帖)

php使用error_log函数记录运行日志的使用总结(转帖)

Jun 15, 2016 pm 08:00 PM
errorlogphpcodeOpen sourceprogrammingprogramming languageRecordsoftware development


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的日志记录如果有更复杂需求,可适用其他第三方软件实现。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft