我使用Cascade'ed Monolog并使用YAML配置记录器。这是我的配置的一部分:

formatters:
    dashed:
        class: Monolog\Formatter\LineFormatter
        format: "%datetime%-%channel%.%level_name% - %message%\n"

这是格式化的日志行:
2016-12-13 17:49:16-app.INFO - <message>
format获得毫秒时间戳的正确\Monolog\Formatter\LineFormatter值是多少?

最佳答案

在我的Symfony 2.6项目中,我有一个自定义的日志处理器,因此我将日志实现为服务,如果可以帮助的话,这是service.yml声明的代码;如果查看logger_formatter,则类构造函数的第二个参数是日期格式:

mybundle.logger:
      class:     Symfony\Bridge\Monolog\Logger
      arguments: [mybundle] # channel
      calls:
          - [pushHandler, [@mybundle.logger_handler]]
          - [pushProcessor, [@mybundle.logger_processor]]

mybundle.logger_processor:
      class:     myBundle\Logging\LogProcessor
      arguments:  ["@session"]

mybundle.logger_handler:
      class:     myBundle\Logging\myBundleRotatingFileHandler #Monolog\Handler\RotatingFileHandler
      arguments: ["@session", %kernel.logs_dir%/LOGGER_SID/%kernel.environment%.mybundle.log, 0, 400] #DEBUG = 100; INFO = 200; NOTICE = 250; WARNING = 300; ERROR = 400; CRITICAL = 500; ALERT = 550; EMERGENCY = 600;
      calls:
          - [setFormatter, [@mybundle.logger_formatter]]
          - [setFilenameFormat, ['{filename}','Y-m-d']]

mybundle.logger_formatter:
      class: Monolog\Formatter\LineFormatter
      arguments:
          - "[%%datetime%%]\t%%extra.remote_addr%%\t%%level_name%%\t%%message%%\t%%extra.request_uri%%\n"
          - "Y-m-d H:i:s.u"

10-02 20:27