简体   繁体   English

如何为仅以symfony登录的用户创建自定义独白文件

[英]How to create a custom monolog file only for users login in symfony

I need implement a simple log system for users. 我需要为用户实现一个简单的日志系统。 In the bundle actions of users (for example), i would like to use monolog with some samples like this. 在用户的捆绑操作中(例如),我想将独白与某些示例结合使用。

public function indexAction(){
        $logger = $this->get('logger');
        $logger->users('User {X} is logout');
}

And it log, saves in specific file this log (users.log) for example 并记录日志,例如将该日志(users.log)保存在特定文件中

You can achieve this with a channels. 您可以通过渠道来实现。

The custom channel could be created (in this case user_channel ). 可以创建自定义渠道(在本例中为user_channel )。 The handler user_handler is created that only log records for the user_channel . 创建的处理程序user_handler仅记录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_channel user_handler will put only that messages to the log file. 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

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' . 日志实体将被写入'%kernel.logs_dir%/user.log'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM