【问题标题】:lumen-logging with dynamic path at run time在运行时使用动态路径进行流明日志记录
【发布时间】:2019-10-03 08:57:29
【问题描述】:

我在bootstrap/app.php文件中使用过这个功能

$app->configureMonologUsing(function ($monolog) {
 $maxFiles = 7;

 $rotatingLogHandler = (new Monolog\Handler\RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
    ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true));

 $monolog->setHandlers([$rotatingLogHandler]);

 return $monolog;
 });

正在根据日期生成日志文件。但是每次我必须更改日志文件路径时,我都想为用户明智地生成日志文件,例如,

storeage/logs/USERID/DATEWISELOG.log

而不是将日志文件创建为storeage/logs/DATEWISELOG.log

是否可以根据用户生成logfile路径?

【问题讨论】:

    标签: laravel logging lumen monolog


    【解决方案1】:

    由于您并不总是拥有经过身份验证的用户,因此在技术上会很困难。

    然后日志处理程序在大多数提供程序(包括数据库之一)之前启动,因此很难使其依赖于登录用户。

    除非你自己破解一些东西。

    【讨论】:

      【解决方案2】:

      我终于得到了答案

      bootstrap/app.php

      $app->configureMonologUsing(function ($monolog) {
       $maxFiles = 7;
      
       $rotatingLogHandler = (new Monolog\Handler\RotatingFileHandler(config("path"), $maxFiles))
          ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true));
      
       $monolog->setHandlers([$rotatingLogHandler]);
      
       return $monolog;
       });
      

      当用户登录后,将 config("path") 的值更改如下,

      config(['path' => storage_path('logs')."/".$user->user_id."/lumen.log"]);
      

      它会在运行时改变 config 的值。

      【讨论】:

        猜你喜欢
        • 2016-04-26
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 1970-01-01
        • 2017-12-18
        • 1970-01-01
        • 2012-05-11
        • 2011-03-01
        相关资源
        最近更新 更多