简体   繁体   中英

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 .
Does it exist a way to change this format?

I would like the filename to be like 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 . It will also delete files older than $maxFiles. You should use logrotate for high profile setups though, this is just meant as a quick and dirty solution.

As you can see in the original RotatingFileHandler : you could possibly change the rotate dateformat

public function setFilenameFormat($filenameFormat, $dateFormat)

But I don't see any configuration option in the symfony monolog reference . 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

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

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

date_format: 'YmdHms'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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