Home > Backend Development > PHP Tutorial > Logging system in PHP framework

Logging system in PHP framework

藏色散人
Release: 2023-04-07 13:46:02
forward
5126 people have browsed it

Logging system in PHP framework

Introduction

Friends who have been exposed to the PHP framework may know the important role of logs in the project , it can help us locate the error location and make the program more friendly (if handled properly, it will not directly throw out a lot of English that only programmers can really use). It will also be very convenient during debugging, and it can also record some important information. Operations, etc. In short, if a complete project does not have a logging system, the development road will be full of thorns and potholes, and it will definitely be stumbling.

Introduction

To master the PHP logging system, you must first have a thorough understanding of these things.

1. Several functions of php

set_exception_handler(callback $exception_handler); //Exception capture custom processing function registration

set_error_handler(callback $error_handler); //Error capture custom handler function registration

register_shutdown_function(callback $callback); //Error capture handler function registration when the program terminates abnormally during execution

These three functions are in error Processing control provides developers with a lot of room for autonomy, and they are responsible for recording log information in the log system.

When an exception occurs in the program, the PHP kernel will throw an exception error and then print the error message to the user. If the exception handling function is registered, the exception thrown by PHP will be forwarded to itself. The defined registered exception catching function contains the processing we need to do and records the error information (including error details and error location). After the function handles the exception, the exception will terminate.

When an error occurs in the program, the error handling function we registered will convert the error information into an error exception object in the function and pass it to the exception handling function, which is the $exception_handler function in the first step.

When a shutdown error occurs during the resumption, the abnormal termination processing function we registered will be executed. This function obtains the error object of the last shutdown through error_get_last(), and then generates an error like the previous part. exception object, pass this object to our registered exception handling function.

As you can see, in fact, whether it is an exception or an error, it converts its own information into the exception information recognized by the exception handling function, and then hands it over to the exception handling function for processing. Non-exception information is like makeup. Just like women, the exception handler does not recognize these non-exception information. Only by removing the loading and unloading (the non-exception information itself is converted into exception information, to be precise, it should be thrown), the exception handler will recognize it.

Logging system in PHP framework

Error handling process in the php log system

Now comes the problem. These functions are usually combined with an exception handling library, plus An error logging class library is used to work. The exception handling class library contains 3 functions to be registered. The logging class library is called in $exception_handler to reasonably record and place the location of the log file. The above mentioned A function is generally loaded and registered at the entrance of the program framework, as follows:

The array (class, function) method is used here.

set_exception_handler(array("Myexception","exceptionHandler"));
set_error_handler(array("Myexception","errorHandler"));
register_shutdown_function(array("Myexception","shutdownHandler"));
Copy after login

2. Logging related class libraries

The things introduced in the first part only capture exceptions, errors, and shutdowns. This is only the first step. Next, Next, we need to process the captured information reasonably, such as recording the log information to the local file system (this operation is in array("Myexception", "exceptionHandler")), where the logging class library is used . (The class library mentioned below draws on the design of the kohana log system).

Japanese-style logging is also very simple. All you need to do is add the information to the end of the file. This is easy to implement. I believe everyone can implement it by themselves, but it is necessary to design a convenient, efficient, and expanded log. The logging class library is not that simple. It needs to be summarized and optimized after a long period of practice. The logging class library in the kohana framework is relatively mature, so we can use it here for reference.

I believe that users who have used kohana must be familiar with the logging in the kohana framework. It doesn’t matter if they are not familiar. I will briefly talk about it below, in the application/bootstrap.php file in the kohana source code. You can see the following code at lines 109-112:

/**
* Attach the file write to logging. Multiple writers are supported.
*/
Kohana::$log->attach(new Log_File(APPPATH.'logs'));
Copy after login

This is to add a logging object to the log object. Pay attention to the two with olive background. They are different class library instances. In kohana , the logging object is divided into two parts. The first part is the log object, which is used to maintain a list of logging objects. How to understand this? It is actually like a container, which contains one or more logging objects (this It is the second part, these logging objects are actually used to record logs), and there is an array of error levels to be recorded for each object. It will only be recorded when the error level is met, and it will be omitted if it does not. The following is my own simplified logging method after renaming:

self::$log = Log::instance();
self::$log->attach(new Logwriter("./data/debug"),Log::DEBUG);
self::$log->attach(new Logwriter("./data/notice"),Log::NOTICE);
Copy after login

我这里面为了更好地理解,将“容器”命名为Log,记录的实例命名为Logwriter,可以看到我在程序入口处很容易的添加了两不同的日志种类,第一个是记录所有错误号比Log::DEBUG小的错误(错误级别比他高),并按规则记录在文件夹./data/debug下面;

第二个是记录级别等于或高于Log::NOTICE的错误,当然了你还可以更详细制定具体哪些错误好,传递数组就行了,这个就是我感觉方便、快捷的地方,我们可以根据需求来添加错误日志、分不同的日志目录,下面看一幅图也许会有助于理解:

Logging system in PHP framework

log与logwriter的关系

通过上面的图你就会看到Log是一个容器,包含了具体的不同的logwriter对象,每个对象可能要记录不同的信息,当错误信息要刷到文件中的时候,会运行每一个Logwriter实例,看看自己是否要记录errormessage中的错误,errormessage中的level不包含在Logwriter内时忽略。

这本分和第一部分怎么合作的呢?其实很简单,当exception捕获的异常时会调用添加一条错误信息(包括错误位置、错误代号、错误信息等信息)到Log容器中的errormessage数组中,然后当程序结束之后在将这些信息写入文件;

这里还要注意下,也许你在阅读kohana代码是发现没有明显的直接写入到日志中去,这里面kohana优化的比较好,因为php的一次执行可能出现多个错误,如果来一个错误你就去记录一次这样会在程序返回之前占用多余的io和时间;

所以kohana的做法是默认将所有的错误、异常、日志存放在Log::$errormessage中,并在实例化的时候讲Log中的writer操作注册register_shutdown_function,这个函数的作用是在程序异常终止或者执行完成之后执行,前面第一部分也有使用到,这样日志记录就不会对本次php的执行产生带大的影响。

更多PHP相关知识,请访问PHP教程

The above is the detailed content of Logging system in PHP framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template