dream12001 2016-12-16 11:16
浏览 92
已采纳

如何仅为在symfony中登录的用户创建自定义monolog文件

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

  • 写回答

1条回答 默认 最新

  • douzhong4222 2016-12-16 19:59
    关注

    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'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么