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

如何仅为在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

图片转代码服务由CSDN问答提供 功能建议

我需要为用户实现一个简单的日志系统。 在用户的捆绑操作中(例如),我想使用monolog和一些这样的样本。

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

它记录,在特定文件中保存此日志(users.log),例如

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题