您可以通過渠道實現這一目標。
可以創建自定義通道(在這種情況下爲user_channel
)。處理程序user_handler
被創建爲僅記錄user_channel
的記錄。在控制器中,請求一個頻道的特定記錄器。使用此記錄器記錄的所有內容將轉至user_channel
。 user_handler
將只將該消息放入日誌文件。
# app/config/config.yml
monolog:
channels: ['user_channel']
handlers:
user_handler:
level: debug
type: stream
path: '%kernel.logs_dir%/user.log'
channels: ['user_channel']
main:
level: debug
type: stream
path: '%kernel.logs_dir%/log.log'
channels: ['!user_channel'] #In case you don't want other handler to receive user_channel messages
然後在控制器中,您可以直接訪問日誌處理程序。
public function indexAction(){
$logger = $this->get('monolog.logger.user_channel');
$logger->debug('User {X} is logout');
}
日誌實體將被寫入'%kernel.logs_dir%/user.log'
。