浅谈php自定义错误日志,浅谈php自定义
浅谈php自定义错误日志,浅谈php自定义
平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的《最佳日志实践》觉得写一个清晰明了,结构分明的日志还是非常有必要的。
在写日志前,我们问问自己:为什么我们有时要记录自定义的日志呢?而不用系统默认的日志记录方式呢?
我认为有两个原因:
1.团队需要一个统一格式的日志方便管理
2.大量无用错误日志占据硬盘空间,仅需记录有意义的日志。
那么,实践一下。
1.打开你的php.ini
2.打开日志记录,将
复制代码 代码如下:
log_errors = Off
改成
复制代码 代码如下:
log_errors = On
3.将php.ini保存退出并重启web服务器
4.在你的代码最前面加上如下代码
复制代码 代码如下:
//错误处理函数
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
$log_file = "./php_%s_log_".date("Ymd").".log";//定义日志文件存放目录和文件名
$template = '';
switch ($errno) {
case E_USER_ERROR:
$template .= "用户ERROR级错误,必须修复 错误编号[$errno] $errstr ";
$template .= "错误位置 文件$errfile,第 $errline 行\n";
$log_file = sprintf($log_file,'error');
exit(1);//系统退出
break;
case E_USER_WARNING:
$template .= "用户WARNING级错误,建议修复 错误编号[$errno] $errstr ";
$template .= "错误位置 文件$errfile,第 $errline 行\n";
$log_file = sprintf($log_file,'warning');
break;
case E_USER_NOTICE:
$template .= "用户NOTICE级错误,不影响系统,可不修复 错误编号[$errno] $errstr ";
$template .= "错误位置 文件$errfile,第 $errline 行\n";
$log_file = sprintf($log_file,'notice');
break;
default:
$template .= "未知错误类型: 错误编号[$errno] $errstr ";
$template .= "错误位置 文件$errfile,第 $errline 行\n";
$log_file = sprintf($log_file,'unknown');
break;
}
file_put_contents($log_file,$template,FILE_APPEND);
return true;
}
$error_handler = set_error_handler("myErrorHandler");//开启自定义错误日志
5.试着在刚才的代码后写下一段错误代码
echo 1/0;
看看你定义的路径下是否多了一个日志文件呢?:)
注:以下级别的错误不能由用户定义的函数来处理: E_ERROR、 E_PARSE、 E_CORE_ERROR、 E_CORE_WARNING、 E_COMPILE_ERROR、 E_COMPILE_WARNING,和在 调用 set_error_handler() 函数所在文件中产生的大多数 E_STRICT。
不过当你开启了错误日志系统(php.ini中的log_error = on)并且指定了系统日志文件(同样也是php.ini中的error_log=路径名),并且error_reporting开启了全部后,以上的错误都会作为系统错误日志而记录在你定义的文件中。
以上就是本文所述的全部内容了,希望大家能对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











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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

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 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 still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
