You can achieve this with a channels.
The custom channel could be created (in this case user_channel
). The handler user_handler
is created that only log records for the user_channel
. In controller the specific logger for a channel is requested. Everything that is logged with this logger will go to user_channel
. user_handler
will put only that messages to the log file.
# 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
Then in controller you can directly access the log handler.
public function indexAction(){
$logger = $this->get('monolog.logger.user_channel');
$logger->debug('User {X} is logout');
}
The log entity will be written to '%kernel.logs_dir%/user.log'
.