devthis/console-logg

使用您常用的 Laravel 日志器轻松实现工匠控制台输出

v1.3.0 2023-02-17 13:17 UTC

This package is auto-updated.

Last update: 2024-09-17 16:30:08 UTC


README

PHPUnit suite codecov Latest Stable Version Total Downloads License FOSSA Status

轻松实现 PSR-3 日志器到控制台应用程序的输出

Symfony 控制台日志器 支持

目录

它是做什么的?

应用程序与控制台命令之间共享消息的日志器通道

通常需要一种黑客式的解决方案,例如将共享服务与控制台日志器耦合,或配置多个驱动程序通道。

使用 ConsoleLogg,您可以拥有工匠命令的日志,并且与 http/controllers 一样正常工作。

使用日志器不需要进行任何代码更改

支持

  • 依赖注入/自动装配 LoggerInterface $logger & $logger->debug("yeet")
  • logger()->critical("Send help")
  • Log::alert("She find pictures in my email")
  • Log::info("Found <X> to be processed")
php artisan my:command -vvv
[debug] yeet
[critical] Send help
[alert] She find pictures in my email
[info] Found <X> to be processed

安装

  1. 通过 Composer 安装包

    composer require devthis/console-logg
  2. 启用日志通道 console-logg

config/logging.php

    'channels' => [
        'stack' => [
            'driver' => 'stack',
-            'channels' => ['single'],
+            'channels' => ['console-logg', 'single'],
            'ignore_exceptions' => false,
        ],

Laravel - 日志文档

兼容性

功能

支持 artisan serve

日志输出将在您的本地开发服务器控制台中显示。

真正简单

您的应用程序 不会与 ConsoleLogg 耦合

无需使用任何特性、类或接口。ConsoleLogg 不需要任何自定义代码,它只是工作。

ConsoleLog 服务提供者应自动添加到您的应用程序中,但如果尚未添加,您可以在 config/app.php 中添加它

// generally only required when you have composer installed with --no-scripts

'providers' => [
    //...
    \DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider::class,
];

命令内命令

ConsoleLogg 已通过使用 artisan 命令在命令中使用 嵌套命令调用 测试了兼容性

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyConsoleApp extends Command
{
    protected $description = '';
    protected $signature = 'my:app';

    public function handle(): int
    {
        //other:command may invoke services that use the Laravel Logger
        //these logs will still output to this current console
        $this->call('other:command');
        //...
        
        return 0;
    }
}

轻量级

  • 零外部依赖(除 Laravel 合约之外)
  • 无内存泄漏(需要验证/测试
    • 一次性使用控制台日志器将与命令执行一起附加和分离
    • 命令终止后所有引用将被销毁 (让 PHP 垃圾回收器做其事)
  • 服务提供者在控制台模式下才懒加载工作

用法

详细程度

详细程度可以通过在运行 artisan 时使用 -v 参数来控制

这不是 ConsoleLogg 设置的行为,而是由 Laravel & Symfony 定义的组合

如果需求明显,ConsoleLogg 可能会在未来提供此配置

示例

运行 artisan

查看示例用法

示例 #1 - SQL 查询日志

互联网上有几个指南/答案可以启用您将所有 SQL 查询发送到配置的 Logger。

使用 ConsoleLogg 安装意味着

链接(无顺序)

示例 #2 - 原始代码

⚠️ 提醒:以下代码示例没有任何特殊之处

安装 ConsoleLogg 后,无需任何特性、接口或类/依赖即可使用。

示例服务的源代码

App\Service\MyExampleService 的源代码
namespace App\Service;

use Illuminate\Support\Facades\Log;
use Psr\Log\LoggerInterface;

class MyExampleService {
    private $logger;
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    
    public function doSomethingCool(): void
    {
        // using Laravel's logger with DI/autowiring
        $this->logger->debug("A message that should have value when output to your application in general");
        
        // Facade
        Log::info("or just the facade if you love magic");
        
        // Helper function
        logger()->notice("or this weird helper function I guess");
        
        // ... <imaginary useful code here>
    }
}

控制台应用的源代码

App\Console\Commands\ExampleConsole 的源代码
namespace App\Console\Commands;

use App\Service\ExampleService;
use Illuminate\Console\Command;

class ExampleConsole extends Command
{
    /**
     * The console command description.
     */
    protected $description = '';

    /**
     * The name and signature of the console command.
     */
    protected $signature = 'something';

    public function handle(ExampleService $exampleService): int
    {
        $exampleService->doSomethingCool();

        return 0;
    }
}

运行 artisan

Artisan 命令的输出
not-root@linux:~/MyProject$ php artisan something -vv
[debug] A message that should have value when output to your application in general
[info] or just the facade if you love magic
[notice] or this weird helper function I guess

许可证

Laravel ConsoleLogg 是开源软件,采用 MIT 许可证