The log system in the PHP framework

Source: Internet
Author: User
Tags php framework kohana

 

Now in a company to do PHP background Development Program APE (we do not have front-end, do activities will do front-end things), just start to the company spent 2 weeks to drive out a front-end plus backstage activities (remember that at that time did not come out on the weekend plus two days 、、、), to the past 4 months, Can use one afternoon to drop a not very complex activities, of course, there are a lot of things to do now, such as extensibility, reusability, because do more, will accumulate a lot of things like small plug-ins, so it will soon ... But I found that all day "standing in demand" is bad, so I will not learn the system, the framework of things, because all the trivial needs to be trapped, there is no time to do some important part of the framework of things, and when your colleagues, bosses accustomed to you to do those things, Every time you encounter those without technical content but have to do things will not hesitate to push to you (selfish point, each program ape like me, all want to master a technical content, so that the program ape Pride), over time you will be accustomed to 、、、、、、 so really miserable, so, I will spare some time to learn the things in the framework and strive not to be a simple yard farmer.

Introduction

Friends who have contacted the PHP framework may know that the log is important in the project, he can help us to locate the wrong place, make the program more friendly (properly handled without directly throwing a lot of only the program ape really move English), debugging time will be very convenient, can also record some important operations and so on, In short, a complete project without a log system, has been developed on the road covered with thorns, potholes, will certainly stumble.

Introduction

To master the PHP log system, you must first understand these things thoroughly.

first, several functions of PHP

1 set_exception_handler(callback $exception _handler); // exception capture Custom handler registration

1 set_error_handler(callback $error _handler); // Error Trapping custom handler registration

1 register_shutdown_function(callback $callback); // exception termination error capture handler registration during program execution

These three functions give developers a great deal of autonomy in error-handling control, and they are credited with logging information in the log system.

When an exception (exception) problem occurs in the program, the PHP kernel throws an exception and then prints the error message to the consumer, and if the exception handler is registered, the exception thrown by PHP is passed to the custom registered exception capture function, which contains the processing we want to do , the error message is logged, including the error details, the error location, and the exception is terminated when the function finishes processing the exception.

When error occurs in the program, the error handler that we register in the function converts the wrong message into an error exception object passed to the exception handler, which is the first step of the $exception_handler function.

As a continuation of the shutdown error, we register an abnormal termination handler function, the function through Error_get_last () to get to the last shutdown when the Error object, and then the same as the previous generation of an error exception object, Pass the object to our registered exception handler.

Can see, in fact, whether it is an exception or error, is to transform their information into abnormal processing function to understand the exception information, and then to the exception processing function processing, non-abnormal information like a woman with makeup, exception handlers do not know these non-abnormal information, only the loading and unloading (non-abnormal information itself into abnormal information, The exact word should be thrown), the exception handling only know.

Error handling process in PHP log system

So now the problem, these functions will generally be combined with an exception handling class library, plus an error logging class library to work, the exception handling class library contains the 3 functions to be registered, the Logging class library is called in $exception_handler, used to reasonably record and place the log file location , several of the functions mentioned above are usually loaded at the entrance of the program framework, as follows:

This is used in the form of an array (class,function).

1 Set_exception_handler (array("MyException", "Exceptionhandler")); 2 Set_error_handler (array("MyException", "ErrorHandler")); 3 register_shutdown_function (array("MyException", "Shutdownhandler"));

second, the log records related class library

The first part of the introduction of the thing is only the exception, error, shutdown capture, this is only the first step, and then to the captured information to reasonable processing, such as logging the log information to the local file system (this operation is in the array ("MyException", " Exceptionhandler "), this place uses the log record class library. (The class library below is a reference to the design of the Kohana log system).

Japanese-style logging is also very simple as long as the information is added to the end of the file line, this is easy to achieve, I believe that we can achieve their own, but to design a convenient, efficient, extended logging class library is not so simple, after a long period of practice summary optimization can be, The Logging class library in the Kohana framework is already quite mature, so let's use it for reference.

Believe that users who have used Kohana must be familiar with the log records in the Kohana framework, unfamiliar or not, I will probably say below, in the Kohana source of application/bootstrap.php file in the first 109-- 112 lines can see the following code:

109 /* *  the * Attach The file write to logging. Multiple writers is supported. 111  */  Kohana::$log->attach (new log_file (APPPATH. Logs '));

This is to add a logging object to the log object , note the olive color to hit the bottom of the two, they are different class library instances, in the Kohana, the logging object is divided into two parts, the first part is the log object, To maintain a list of logging objects, how does this make sense, in fact, like a container containing one or more logging objects (this is the second part, these logging objects are really to record the log), there is an array of error levels for each object to record, When the error level is met, the record is not satisfied and will be omitted. Here are my own simplified logging methods after renaming:

1 Self::$loglog::instance (); 2 Self::$log->attach (new LogWriter ("./data/debug"),log::debug); 3 Self::$log->attach (new LogWriter ("./data/notice"),log:: notice);

I'm here to better understand the "container" named Log, the recorded instance named LogWriter, you can see me at the entrance of the program easily add two different log types, the first is to record all error number than log::D ebug small error (Error level higher than he), and recorded in the folder according to the rules./data/debug below, the second is the record level is equal to or higher than the Log::notice error, of course, you can also make more detailed specific what error good, pass the array on the line, this is I feel convenient, fast place, We can add error logs and separate log directories according to the requirements, and a picture below may help to understand:

The relationship between log and LogWriter

From the diagram above you will see that log is a container that contains specific different LogWriter objects, each of which may have to record different information, and when the error message is to be brushed into a file, each LogWriter instance is run. See if you want to record errors in errormessage, and the level in ErrorMessage is not included in the LogWriter.

How does this work with the first part? In fact, when exception catches an exception, it calls to add an error message (including the error location, error code, error message, and so on) to the errormessage array in the Log container, and then when the program finishes writing the information to the file, note that Perhaps you are reading Kohana code is found no obvious direct write to the log to go, this inside Kohana optimization is better, because PHP one execution may have multiple errors, if an error you go to record once this will occupy the extra IO and time before the program returns, So Kohana's practice is to default all errors, exceptions, logs stored in the log:: $errormessage, and in the case of the instantiation of the log writer operation Register Register_shutdown_function, This function is performed after the program terminates or executes, and the first part is used, so the log record will not have a big impact on the execution of this PHP.

iii. Summary of examples

Here you should already understand the log system is probably, already can write a "log system" to use, below to see my "log System" example, this is the GitHub address, there are code and examples, need to see.

Https://github.com/AizuYan/phplog.git

This article copyright belongs to the author ([email protected]) and the blog garden in total, without the author's consent to prohibit any form of reprint, reproduced article must be in the article page obvious location to the author and the original text connection, otherwise reserves the right to pursue legal responsibility.

Log system in the PHP framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.