简体   繁体   English

更改自定义独白文件的文件名

[英]Change filename on custom monolog file

I use a rotating monolog handler 我使用旋转的独白处理程序

monolog:
        channels: ['import']
        handlers:
            import_client:
                level: debug
                type: rotating_file
                max_files: 10
                path: '%kernel.logs_dir%/import.log'
                channels: [import_client]

All works fine except I don't like the filename. 除了我不喜欢文件名之外,其他所有方法都正常。 I get import-2018-02-22.log . 我得到import-2018-02-22.log
Does it exist a way to change this format? 是否存在更改此格式的方法?

I would like the filename to be like import-"date(YmdHis)".log . 我希望文件名像import-"date(YmdHis)".log

Is possible to rewrite the filename format? 可以重写文件名格式吗? Did you have any solutions ? 您有什么解决方案吗?

The RotatingFileHandler Logs records to a file and creates one logfile per day . RotatingFileHandler将记录记录到文件并每天创建一个日志文件。 It will also delete files older than $maxFiles. 它还将删除早于$ maxFiles的文件。 You should use logrotate for high profile setups though, this is just meant as a quick and dirty solution. 但是,您应该将logrotate用于高配置设置,这仅是一种快速而肮脏的解决方案。

As you can see in the original RotatingFileHandler : you could possibly change the rotate dateformat 如您在原始RotatingFileHandler中所看到的:您可以更改旋转日期格式

public function setFilenameFormat($filenameFormat, $dateFormat)

But I don't see any configuration option in the symfony monolog reference . 但是我在symfony monolog参考中看不到任何配置选项。 You could call a service using 您可以使用以下方式致电服务

services:
    app.custom_rotating_service:
        # ...
        calls:
            - method: setFilenameFormat
              arguments:
                  - 'yourFilenameFormat'
                  - 'Ymd'

It seems to me you would get into soemthing complex for no added value of a date format. 在我看来,如果没有日期格式的附加值,您会陷入麻烦。

TLDR TLDR

  • It's not possible to have a logfile By Hour/Minute with monolog 不可能有按小时/分钟的日志文件和独白
  • Changing Date format or the rotation frequency to months/year of the handler seems (to me) doable but not supported by the symfony monolog configuration. 在我看来,将日期格式或轮换频率更改为处理程序的月/年似乎可行,但symfony monolog配置不支持。 you could create a service and try to call the method automatically on service isntance creation 您可以创建服务,并尝试在创建服务等距时自动调用该方法
  • You should use logrotate if you have a custom need of rotating log 如果您有旋转日志的自定义需求,则应使用logrotate

我找到了解决方案,需要在配置处理程序中添加一个新参数:

date_format: 'YmdHms'

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

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