Laravel logger
From AWVVO
Custom logger
<?php
namespace App\Util;
use Illuminate\Support\Facades\Log;
class Logger
{
public static function info(...$args): void
{
$trace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))
->first(fn ($t) => isset($t['file']) && str_contains($t['file'], base_path('app')));
$location = $trace
? basename($trace['file']) . ':' . ($trace['line'] ?? '') . ' -> ' . ($trace['function'] ?? '')
: 'unknown location';
$message = collect($args)->map(function ($arg) {
return is_scalar($arg) ? $arg : json_encode($arg);
})->implode(' | ');
Log::info("[$location] $message");
}
}
Examples
Logger::info('aaa');
Logger::info('aaa', 123);
Logger::info('aaa', 123, true, Carbon::now(), ['a' => 'aaa', 'b' => 123]);