繁体   English   中英

如何在 Laravel 8.0 中自定义登录文件名?

[英]how to customize file name of log in Laravel 8.0?

我正在搜索自定义日志文件名的解决方案。 我得到了一个在 5.6 版本项目中工作的解决方案。 但在那之后意味着超过 5.6 它不起作用。 我尝试使用许多解决方案但没有用。 每次文件名生成laravel.log 但我想得到它像laravel-{vendor_code}-{date}.log

我使用了代码如下的解决方案。

自定义日志文件.php

<?php

    namespace App\Logging;
    
    use Illuminate\Http\Request;
    use Monolog\Handler\RotatingFileHandler;
    
    class CustomLogFile
    {
        /**
         * Customize the given logger instance.
         *
         * @param  \Illuminate\Log\Logger  $logger
         * @return void
         */
        public function __invoke($logger )
        {
            $code = 'NA';
            $headers = apache_request_headers();
            if( isset( $headers['DBAuth'] ))
            {
                $code =  dnc($headers['DBAuth']) ;
            }
            elseif ( isset($_REQUEST['code']) )
            {
                $code = $_REQUEST['code'];
            }
    
            foreach ($logger->getHandlers() as $handler) {
                if ($handler instanceof RotatingFileHandler) {
                    $sapi = php_sapi_name();
                    $handler->setFilenameFormat("{filename}-$code-{date}", 'Y-m-d');
                }
            }
        }
    }

配置/日志记录.php

'default' => env('LOG_CHANNEL', 'stack'),
   ......

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',         
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],

        'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

这里这段​​代码在 Laravel 的5.6 v中作为我的条件工作,但不超过5.6 v

我尝试检查$logger->getHandlers() 然后我得到数组为空。 所以会不会是这个原因。

这是 Laravel 8 的工作解决方案:

自定义日志文件.php


class CustomLogFile
{

    public function __invoke(array $config): Logger
    {
        $log = new Logger($config['logname']);
        $level = $log::toMonologLevel($config['level'] ?: 'debug');

        $vendor_code = $_REQUEST['code'] ?? 'NA';
        $date = Carbon::now()->toDateString();

        $logPath = storage_path("logs/$vendor_code-$date.log");
        $log->pushHandler(new StreamHandler($logPath, $level, false));

        return $log;
    }
}

配置/日志记录.php

'channels' => [
//...
        'by_vendor_code' => [
            'driver' => 'custom',
            'via' => UserLogger::class,
            'logname' => 'by_vendor_code',
            'level' => 'debug',
        ],
//...
    ],

任何地方的用法示例:

logs('by_vendor_code')->info('this vendor want to: ', ['context' => 'some context data']);

可能是它的答案在env 文件中是这样的

LOG_CHANNEL=daily
LOG_LEVEL=debug

还要更改 Config/logging.php 中的日期设置

'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 1,  // 14----> 1

因为在 6.2 版本中我得到了那个结果。 所以在这里回答我会尽快确认 8.* 版本。

暂无
暂无

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

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