PHP code example of jplhomer / laravel-axiom

1. Go to this page and download the library: Download jplhomer/laravel-axiom library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jplhomer / laravel-axiom example snippets


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



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

// app/Http/Kernel.php

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