Symfony 4, Hide some unnecessary logs with custom handler

Tram Ho


Symfony generates a lot of logs like, like process init, begin transaction, ini, . Most of these logs provide less important information, but they interfere with the log system and especially cost customers (when using cloud services like AWS Cloudwatch log to store and filter logs). Here are ways to hide it


Use log level settings

In the monolog.yml file, set the log level variable to a level that you feel is necessary, serving good information for debugging and investigation tasks.

The levels of setting log levels you can refer to

Use a custom handler (better)

Because these residual logs are mostly debug and log info, it is also at the same level as the logs I often add to investigate, so it is difficult to use the above method, so use a top handle to filter messages only. Specifically, the solution I think is quite good

  • Create a custom handle at the beginning of the log string.

  • In service.yml add define class.

  • In the Customize/Log folder add the class:

As you can see above, the $listMessageRemoved variable contains the messages that you want to remove from the log, the mechanism here is based on the handle() method, returning true means that this message has been processed so it is not needed. sent through the next handle (here is the cloudwatch handler), according to the documention:

=> Finally we have a delicious solution, to remove the log to waste customers’ money, hopefully it will help everyone

Share the news now

Source : Viblo