symfony 使用monolog 记录日志

sanlanlan 2019-10-5 标签: monolog 浏览:1371 评论:0

关于symfony 如何使用monolog官网有介绍,这边只是做一些记录笔记。

1.如何把日志信息写入不同的文件中

使用channels 来配置:

config.yml 添加此配置

monolog:
    channels: [channel1, channel2]
    handlers:
        channel1:
            type: stream
            path:  %kernel.logs_dir%/channel1.log
            channels: [channel1]

        channel2:
            type: stream
            path:  %kernel.logs_dir%/channel2.log
            channels: [channel2]


然后再controller 中这样调用:


$this->get('monolog.logger.channel1')->info("log");

如果想要在此的基础上,添加每天自动生成一个文件功能可以修改配置成:

type配置为:type:  rotating_file

然后添加属性:max_files: 10  //要保留的日志文件的最大数量,默认是零,即,无限个文件

详细看官网配置:http://symfony.com/doc/current/logging.html

2.自定义一个日志formatter

  Monolog 支持自定义日志格式,只要实现 Monolog\Formatter\FormatterInterface 接口

具体详见官网:http://symfony.com/doc/current/logging/formatter.html

3.Monolog 优化及打造 ELK 友好的日志格式

使用monolog 搭配elk (elasticsearch) 可以图形化你的日志,
还没研究,可以参考:https://segmentfault.com/p/1210000008009016

本文相关标签: symfony monolog log

发表评论: