PHP | PSR Micro Framework | Logger

in #php7 years ago

If think what is missing, it becomes clear that there is not enough logging.
Error handling works well, but it is not clear where and when these errors occurred in my application.
If I catch errors in a try/catch block, the error will not get into the server logs.
To fix this, I need to write these errors somewhere.
The most direct and simple solution is to add an entry to the file directly in our error handler written earlier.
But, I set the goal here to learn, so we write as it should! ;)
Smart dudes have come up with good solutions already. For a start, I read about the Psr Logger Interface
Then I connected the good solution Monolog Logging for PHP.

How I connected Monolog to the app:

  • Installed using composer
    install_monolog.png
  • Passed the object with the second parameter to the ErrorHandler Middleware constructor and called it in the right place
    include_to_error_handler.png
  • Connected in a DI container (using the Psr Logger Interface as the connection basis)
    In the designer Logger I transfer the name. We can specify any name. Next, I specify the handler that will write errors to the file.
    include_monolog_logger_1.png
    include_monolog_logger_2.png
  • Checked work. I added throw LogicException in the controller action.
    about_throw_exception.png
  • Next, I looked at the log file and saw that the error is recorded.
    log_file_1.png
    Magic! This is all thanks to the community and psr recommendations.

Link to commit

Question! What else did I not consider? Under which scenario would it be desirable to write data to the log?
In the next post I will add this.

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.081
BTC 61655.78
ETH 1609.66
USDT 1.00
SBD 0.47