Log System in PHP Framework

Source: Internet
Author: User
Tags kohana
: This article mainly introduces the log system in the PHP Framework. if you are interested in the PHP Tutorial, refer to it.

Now, I am working on PHP background development programs in a company (our group has no front-end and will do front-end things during activities ), at the beginning, I spent two weeks driving out a front-end and back-end activity at the company (I remember that I had not been able to work for two days at the weekend ,,,), it has been more than four months now. you can drop an activity that is not complex in one afternoon. of course, you will consider a lot of things, such as scalability and reusability, A lot of things similar to small plug-ins will be accumulated, so it will soon ...... However, I found that "standing in demand for demand" is very poor throughout the day, so that I won't learn about the system or framework, because they are all trapped by trivial demands, you don't have time to do something important to the framework, and when your colleagues and superiors get used to what you do, every time you encounter something that is not technical but has to be done, you will not hesitate to push it to you. (to be selfish, every programmer is like me, all want to master a piece of technical content, so that you have the pride of programmers.) over time, you will get used to, and it's really miserable, I will take some time to learn things in the framework and try not to be a simple coders.

Introduction

Friends who have been familiar with the php framework may know that the log plays an important role in the project and can help us locate the wrong location, make the program more friendly (if it is handled properly, it will not directly throw a lot of English words that only the program ape can really change), it will be very convenient during debugging, and some important operations can be recorded, etc, in short, if a complete project has no log system, it will be covered with thorns and pitfalls.

Introduction

To master the PHP log system, you must have a thorough understanding of these items.

I. several php functions

1 set_exception_handler (callback $ exception_handler); // Custom handler function registration for exception capture

1 set_error_handler (callback $ error_handler); // error capture Custom handler function registration

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

These three functions provide developers with a lot of autonomous space in error processing control, and they are credited for recording log information in the log system.

When an exception occurs in the program, the php kernel will throw an exception error and print the error information to the user. if the exception processing function is registered, the exception thrown by php will be transferred to the custom registered exception capture function, which contains the processing we want to perform and records the error information (including the error details and location ), after an exception is processed by the function, the exception is terminated.

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

When a shutdown error occurs during the reconnection, the system executes the exception termination processing function we registered. this function obtains the error object for the final shutdown through error_get_last (), and then gets the same as the previous one, generate an error exception object and pass it to the registered exception handling function.

As you can see, both exceptions and errors convert your information into the exception information recognized by the exception handling function, and then hand it over to the exception handling function for processing, non-exception information is like a makeup woman. The Exception handling program does not know such non-exception information and only loads and loads the non-exception information, to be accurate, it should be thrown.) exception handling is only known.

Php log system error handling process

Now the problem arises. these functions are generally used together with an exception processing class library, and an error log record class library to work. The exception processing class library contains the three functions to be registered, the log record library is called in $ prediction_handler to reasonably record and place log files. the functions mentioned above are generally loaded and registered at the entrance of the program framework, as shown below:

Array (class, function) is used here.

1 set_exception_handler(array("Myexception","exceptionHandler"));2 set_error_handler(array("Myexception","errorHandler"));3 register_shutdown_function(array("Myexception","shutdownHandler"));

II. log record-related libraries

The first part only captures exceptions, errors, and shutdown. this is only the first step. Next, we need to properly process the captured information, for example, to record the log information to the local file system (this operation is in array ("Myexception", "exceptionHandler"), the log record class library is used in this place. (The class library is designed based on the kohana log system ).

Japanese logging is also very easy as long as the information is added to the end of the file, this is easy to implement, I believe everyone can implement it by themselves, however, it is not easy to design a convenient, efficient, and extended log record library. it takes a long period of practical summarization and optimization, the log record library in the kohana framework is mature, so it is used for reference here.

I believe that users who have used kohana must be familiar with the log records in the kohana framework. it doesn't matter if they are not familiar with it. I will give a rough talk about the application/bootstrap in the kohana source code. the following code is displayed in line 10-1 12 of the PHP file:

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

This is to add a log record object to the log object. Note that the two objects are different class library instances. in kohana, log record objects are divided into two parts, the first part is the log object, which is used to maintain a list of log record objects. how can this be understood? it is actually like a container, it contains one or more logging objects (this is the second part, these logging objects are actually used to record logs), and an array of error levels to be recorded for each object, when the error level is met, the record is recorded. if the error level is not met, the record is omitted. Below is my simplified log record method after renaming:

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

For better understanding, I name the "container" as Log and the recorded instance as Logwriter. we can see that two different Log types are easily added at the program entrance, the first one is to record all errors with lower error numbers than Log: DEBUG (higher error levels than him) and record the errors in folders according to rules. under/data/debug, the second is the record level equal to or higher than the Log: NOTICE error. of course, you can also elaborate on the specific errors and pass the array, this is a convenient and convenient place for me. we can add error logs and separate different log directories as needed. The following figure may help us to understand:

Relationship between log and logwriter

Through the above figure, you will see that Log is a container that contains different specific logwriter objects. each object may have to record different information. when the error information is to be flushed into the file, each Logwriter instance is run to check whether errors in errormessage are recorded. the level in errormessage is ignored if it is not included in Logwriter.

How does this work with the first part? In fact, it is very easy to add an error message (including the error location, error code, and error information) to the errormessage array in the Log container when exceptions are caught by exceptions, then, when the program ends, it will write the information into the file. Note that you may find that the kohana code is not explicitly written directly to the log, here, kohana is better optimized because multiple errors may occur during one php execution, if an error is returned, you should record it once. this will occupy excessive io and time before the program returns. Therefore, kohana stores all errors, exceptions, and logs in Log by default:: $ errormessage and register register_shutdown_function for writer operations in Log during instantiation. This function is executed after the program ends abnormally or the execution is complete, the first part is also used, so that the log record will not have a big impact on the execution of this php.

III. instance summary

Now you have learned about the log system. you can compile a "log system" by yourself. let's look at my "log system" example. this is the github address, the code and examples are provided. if you need them, you can check them out.

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

The copyright of this article belongs to the author (luluyrt@163.com) and the blog Park, without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

The above introduces the log system in the PHP framework, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.