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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
