jplhomer/laravel-axiom

Laravel logging handler for Axiom

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

This package is auto-updated.

Last update: 2024-04-19 14:28:38 UTC


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.