📜 ⬆️ ⬇️

Logging in Yii 2.0 and PSR-3

In the 2017th year and quite a large part of the PHP community is trying to use
PSR: PHP standard recommendation , the purpose of which is to give the ability to replace individual parts of the frameworks. Among the PSRs, there are PSR-3, which describes the interface for the logger. At the moment, there are many PSR-3 compatible libraries, including the widely used Monolog .


On the topic of PSR-3 and Yii 2.0 often ask the following questions:


  1. Why is Yii 2.0 not compatible with PSR-3?
  2. How can I write messages to the PSR-3 compatible logger?
  3. Are there plans to make the Yii logger compatible with PSR-3 and cut it into a separate package?

We analyze each in detail.


Why is Yii 2.0 not compatible with PSR-3?


Logger in Yii 2.0 is one of the well-made components. It is stable, expands easily, works quickly and can do a lot of things. This component was developed in the early stages of the then still secret pre-alpha version of the framework and has not changed significantly since then. The design is very close to the original one, which was in Yii 1.0, so 2008 can be considered the year of its formation.


The year is important because the PHP-FIG group dealing with PSRs was formed much later. During the alpha version of Yii 2.0, we already had a proven working logger design that showed itself well on Yii 1.0 and Yii 1.1. At the time, the PSR-3 was not very attractive. The loggers, which implement it, did not know anything special and, therefore, there were no advantages to the implementation of this PSR in the Yii framework. We decided not to do it.


Now it's time to consider the solution again for version 2.1, since there are significant advantages to compatibility with the PSR-3.


How can I write messages to the PSR-3 compatible logger?


Logging in Yii is pretty flexible. The central dispatcher component collects messages and scatters them for individual logging purposes , which, in turn, write messages to files, send mail, and so on.


Logging goals are easy to expand and create, so writing your own is not that difficult. On the other hand, why write something when there are popular PSR-3 loggers and, especially,
Monolog ? So I want to use ready-made. For example, send messages to chat Slack.


I have implemented an extension that allows using any compatible PSR-3 logger as a logging target.


The extension brings the Yii logging levels to similar PSR-3 levels and delivers messages and related data to the PSR-3 processing for the logger. The configuration is quite simple and is described on the expansion page .


Are there plans to make the Yii logger compatible with PSR-3 and cut it into a separate package?


Yii never tried to be a collection of independent libraries. Independent components - a good thing, but independence has a price. We have to refuse to reuse the code from the core of the framework, which results in a rather large amount of code as such and, often, re-allocated solutions.


Logger, in principle, is not very related to what was written above. It practically does not depend on the framework. On the contrary - the framework depends on it. An attempt to make a separate library of the logger may succeed, and since the design is quite good, there is a chance to get its place among such independent decisions as Monolog. It's worth a try and I will try if the campaign on Patreon gives me enough time to work on OpenSource.


')

Source: https://habr.com/ru/post/323584/


All Articles