This may be the best logstore in the php world-monolog, phpmonolog

Source: Internet
Author: User

This may be the best logstore in the php world-monolog, phpmonolog

For some historical reasons, php does not have a built-in log interface, so it has not been a logstore that has been fully functional and widely used for a long time. In my career, if the system needs to record some application logs, it basically encapsulates a log class and then writes some fields to the disk file.

In this way, it is inevitable to create the wheel again and again, and without a specification, the logs recorded are not easy to analyze. But after reading this article, I hope that you will not be able to create this kind of log, because it is almost impossible for you to create a circle better than what we will introduce today: monolog.

Monolog is a logstore developed for php 5.3 and later versions. However, note that the current main version only supports php 7 and later versions. If your server environment is still php 5, you can use 1 of monolog. version x.

It is worth mentioning that monolog is a log class library compliant with psr-3 specifications and conforms to psr-4 loading specifications. If you are not familiar with the relevant information, refer to the following link.

If you want to introduce monolog in your code, you only need to execute:

Composer require monolog/monolog

Commit log has several important concepts:

First: handler log Manager

The data structure for handler storage is a "stack". A log instance can have multiple handler instances. The pushHandler method of the Logger instance is used to press the handler into a parameter of the HandlerInterface type. If you set multiple handler, when you add a log, it will start to spread from the top of the stack. the handler who cares about this level of log will process this log. All handler will inherit the abstract class AbstractProcessingHandler, and you only need to write the abstract method in it. At the same time, this abstract class will inherit the abstract class AbstractHandler. The constructor of this abstract class has two parameters: level and bubble. The former indicates the minimum log level that the handler cares about and is an integer. The latter indicates whether the log is passed down after being processed by the current handler. See the following code:

Use Monolog \ Logger;

Use Monolog \ Handler \ StreamHandler;

Use Monolog \ Handler \ ErrorLogHandler;

$ Logger = new Logger ('My _ logger ');

$ Logger-> pushHandler (new StreamHandler (_ DIR _. '/my_app.log', Logger: INFO ));

$ Logger-> pushHandler (new ErrorLogHandler (ErrorLogHandler: OPERATING_SYSTEM, Logger: ERROR, false ));

$ Logger-> info ('Code King Education -- probably the most valuable ittraining ');

In the above Code, this log is processed by ErrorLogHandler, and the bubble parameter of ErrorLogHandler is set to false, the log will not be written into my_app.log.

Second: formatter setting the log format

Each handler can set the log format of the record separately. The AbstractHandler abstract class has a setFormatter method, which accepts a FormatterInterface type parameter. We can see that the formatter of monolog is inherited from NormalizerFormatter, which implements the format and formatBatch methods. We modified the sample code above to let two handler record logs of different formats:

Use Monolog \ Logger;

Use Monolog \ Handler \ StreamHandler;

Use Monolog \ Handler \ ErrorLogHandler;

Use Monolog \ Formatter \ JsonFormatter;

$ Logger = new Logger ('My _ logger ');

$ Stream_handler = new StreamHandler (_ DIR _. '/my_app.log', Logger: INFO );

$ Stream_handler-> setFormatter (new JsonFormatter ());

$ Logger-> pushHandler ($ stream_handler );

$ Logger-> pushHandler (new ErrorLogHandler (ErrorLogHandler: OPERATING_SYSTEM, Logger: INFO ));

$ Logger-> info ('Code King Education -- probably the most valuable ittraining ');

Now we can see that the log recorded in my_app.log is changed to the json format that is easier to parse.

Third: processor, used to add additional information to logs

The structure for storing the processor is also a "stack", which means you can also configure multiple processor for a Logger instance using the pushProcessor method. We noticed that pushProcessor accepts a callable, that is, a function or class method is required. However, the official processor is a class, and you can find it by clicking a source code, in fact, these classes use the _ invoke magic method. Therefore, when called as callable, _ invoke is automatically called. Next we modify the example above to add more information to our logs:

Use Monolog \ Logger;

Use Monolog \ Handler \ StreamHandler;

Use Monolog \ Handler \ ErrorLogHandler;

Use Monolog \ Formatter \ JsonFormatter;

Use Monolog \ Processor \ UidProcessor;

Use Monolog \ Processor \ ProcessIdProcessor;

$ Logger = new Logger ('My _ logger ');

$ Stream_handler = new StreamHandler (_ DIR _. '/my_app.log', Logger: INFO );

$ Stream_handler-> setFormatter (new JsonFormatter ());

$ Logger-> pushHandler ($ stream_handler );

$ Logger-> pushHandler (new ErrorLogHandler (ErrorLogHandler: OPERATING_SYSTEM, Logger: INFO ));

$ Logger-> pushProcessor (new UidProcessor );

$ Logger-> pushProcessor (new ProcessIdProcessor );

$ Logger-> info ('Code King Education -- probably the most valuable ittraining ');

After executing this code again, we can see that uid and process_id are added to the end of the day.

I believe that you have a systematic understanding of monolog usage and the overall architecture of the database. At this time, you should be able to find that, developing a handler, formatter, and processor on your own is so simple that you only need to implement two interfaces and then write an anonymous function to completely control your log processing methods and log formats.

In program development, it is very important to make it easy to expand like monolog. To do this, you only need to observe the following points:

1. Dependency interface instead of implementation (dependency inversion and design mode)

2. comply with industry standards (SRS) when there are standards)

After learning about monolog usage and design architecture, why not implement a set of code to record logs to relational data on mongolog :)

For more PHP advanced learning materials, please join group 632109190 for discussion and learning.

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.