特定于包的 Laravel 日志文件

问题描述 投票:0回答:1

我正在编写几个 Laravel 包,我想知道是否可以让包写入特定的日志文件,但仅限于与包相关的消息?

我尝试在packages/myorg/mypackage/config(如下)中创建一个logging.php文件,但它似乎没有做任何事情。

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [
    'default' => env('LOG_CHANNEL', 'stack'),
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/mypackage.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ]
    ]
];

我正在使用“jeroen-g/laravel-packager”来设置包。似乎是在 ServiceProvider bootForConsole 中手动加载 mypackage.config

protected function bootForConsole(): void
{
    // Publishing the configuration file.
    $this->publishes([
        mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
    ], 'mypackage.config');
}

我不确定如何添加自定义日志记录。我仍在学习 Laravel,我不太确定主要应用程序 config/logging.php 的读取内容或方式,因此我不太确定如何为附加包注入自定义版本。

编辑: 我发现一篇文章建议在 ServiceManager boot() 方法中使用以下内容:

$this->app->make('config')->set('logging.channels.mychannel', [
    /* settings */
]);

我使用包配置来设置 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] 然后可以执行与上面相同的操作:

$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');

但这仍然需要代码中的某些内容。到目前为止我发现的下一个最好的事情是将我的 config/logging.php 更改为 config/logging.channels.php 并包含类似的内容:

return [
    'mychannel' => [
        'driver' => 'single',
        'path' => storage_path('logs/mypackage.log'),
        'level' => env('LOG_LEVEL', 'debug'),
    ]
];

然后在服务提供者的register()方法中添加:

    $this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');

我尝试从原始的“logging.php”中执行此操作,通道数组嵌套在“logging”键中,但 array_merge 似乎没有合并嵌套元素,因此我的通道从未出现在logging.channels中。

但是,我不确定这是否理想。我仍然想知道是否有“更好”或最佳实践方法来添加自定义包日志记录参数,以及是否需要以任何方式(以及如何)发布它。

php laravel monolog
1个回答
0
投票

这是让您的自定义包执行一些登录到目标应用程序的

storage/logging
目录的简单方法。

在包中,我定义了一个

config/logging.php
,它镜像 Laravels 默认日志记录配置文件,但仅定义了一个通道(根据您的包要求进行调整):

return [
    'channels' => [
        'my-package' => [
            'driver' => 'daily',
            'path' => storage_path('logs/my-package.log'),
            'level' => 'debug',
            'days' => 7,
        ],
    ],
];

在自定义包的服务提供商中,您需要将包的自定义日志记录通道合并到目标应用程序配置中:

public function register()
{
    // Merge any of your default package config (as needed)
    $this->mergeConfigFrom(__DIR__ . '/../config/my-package.php', 'my-package');

    // Next we specifically merge the logging channels provided by this package
    $this->mergeLoggingChannels();

    // ...
}

private function mergeLoggingChannels()
{
    // This is the custom package logging configuration we just created earlier
    $packageLoggingConfig = require __DIR__ . '/../config/logging.php';

    $config = $this->app->make('config');

    // For now we manually merge in only the logging channels. We could also merge other logging config here as well if needed.
    // We do this merging manually since mergeConfigFrom() does not do a deep merge and we want to merge only the channels array
    $config->set('logging.channels', array_merge(
        $packageLoggingConfig['channels'] ?? [],
        $config->get('logging.channels', [])
    ));
}
© www.soinside.com 2019 - 2024. All rights reserved.