jplhomer/laravel-axiom

Laravel 对 Axiom 的日志处理器

v1.1 2024-04-19 14:27 UTC

This package is auto-updated.

Last update: 2024-09-08 08:24:16 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包为 Axiom 提供了一个日志处理器。[Axiom](https://axiom.co/)。它允许您从 Laravel 应用程序将日志发送到 Axiom。

您可以通过 composer 安装此包

composer require jplhomer/laravel-axiom

然后,在您的 config/logging.php 文件中添加一个新的 axiom 通道

$channels = [
    // ...
    'axiom' => [
        'driver' => 'monolog',
        'handler' => Jplhomer\Axiom\AxiomLogHandler::class,
        'level' => env('LOG_LEVEL', 'debug'),
        'with' => [
            'apiToken' => env('AXIOM_API_TOKEN'),
            'dataset' => env('AXIOM_DATASET'),
        ],
    ],
]

最后,请确保在 .env 中设置您的 AXIOM_API_TOKENAXIOM_DATASET 环境变量。您可以在 [Axiom 控制台](https://app.axiom.co/barkpass-lxgt/settings/api-tokens)中创建一个令牌。

LOG_CHANNEL=axiom
AXIOM_API_TOKEN=your-api-token
AXIOM_DATASET=your-dataset

性能考虑

由于 Axiom 日志通过 HTTP 发送,您可能需要考虑在请求时间发送日志的性能影响。默认情况下,此包将同步地将日志发送到 Axiom。这意味着每次您记录某些内容时,您的应用程序将等待 Axiom 的请求完成,然后再继续处理请求。

更好的解决方案是在响应发送后发送结构化请求日志。为此,您可以为发送响应后发送日志创建一个 可终止的中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class RequestLogger
{
    /**
     * Log all the things that are relevant to the incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $context = [
            'request_host' => $request->getHost(),
            'request_path' => str($request->path())->startsWith('/') ? $request->path() : "/{$request->path()}",
            'request_query' => $request->getQueryString(),
            'request_method' => $request->method(),
            'request_user_agent' => $request->userAgent(),
        ];

        Log::withContext($context);

        // Note: You can use `Log::withContext()` to add context in other parts of your application, too!

        return $next($request);
    }

    public function terminate(Request $request, Response $response): void
    {
        $path = '/' . str($request->path())->ltrim('/');

        $startTime = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT');

        $context = [
            'status_code' => $response->getStatusCode(),
            'processing_time_ms' => round((microtime(true) - $startTime) * 1000, 2),
            'request_controller_action' => $request->route()?->getActionName(),
        ];

        Log::info("[{$response->getStatusCode()}] {$request->method()} {$path}", $context);
    }
}

然后,在您的 Http Kernel 中注册中间件

// app/Http/Kernel.php

protected $middleware = [
    // ...
    \App\Http\Middleware\RequestLogger::class,
];

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

有关如何报告安全漏洞,请参阅我们的 安全策略

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件