承接 jplhomer/laravel-axiom 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jplhomer/laravel-axiom

最新稳定版本:v1.1

Composer 安装命令:

composer require jplhomer/laravel-axiom

包简介

Laravel logging handler for Axiom

README 文档

README

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

This package provides a logging handler for Axiom. It allows you to send logs to Axiom from your Laravel application.

You can install the package via composer:

composer require jplhomer/laravel-axiom

Then, add a new axiom channel to your config/logging.php file:

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

Finally, be sure to set your AXIOM_API_TOKEN and AXIOM_DATASET environment variables in .env. You can create a token in the Axiom dashboard.

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

Performance Considerations

Since Axiom logs are sent over HTTP, you may want to consider the performance impact of sending logs during request time. By default, this package will send logs to Axiom synchronously. This means each time you log something, your application will wait for the request to Axiom to complete before continuing to process the request.

A better solution is to send structured request logs after the response has been sent. To accomplish this, you can create a terminable middleware that sends the logs to Axiom after the response has been sent to the user.

<?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);
    }
}

Then, register the middleware in your Http Kernel:

// app/Http/Kernel.php

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

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 6.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-17