You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
PHP

<?php
namespace App\Service;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
use Stringable;
use Throwable;
/**
* Service for logging
*/
class LoggerService implements LoggerInterface {
use LoggerTrait;
/**
* @var LoggerInterface The logger service base
*/
public readonly LoggerInterface $loggerBase;
/**
* Initialization
*
* @param LoggerInterface $loggerBase The logger service base
*/
public function __construct (LoggerInterface $loggerBase) {
$this->loggerBase = $loggerBase;
}
/**
* @inheritDoc
*/
public function log ($level, string|Stringable $message, array $context = []): void {
$this->loggerBase->log($level, $message, $context);
}
/**
* Log an exception
*
* @param Throwable $exception The exception
* @param array $context The context (automatically add the exception in "throwable")
* @param string $level The log level
*
* @return void
*/
public function exception (Throwable $exception, array $context = [], string $level = LogLevel::ERROR): void {
$context['throwable'] = $exception;
$this->log(
$level,
'Exception [' . get_class($exception) . ']: ' . $exception->getMessage(),
$context
);
}
}