Logging
This commit is contained in:
parent
f1d46dacb6
commit
75ce822b84
6 changed files with 399 additions and 1 deletions
|
|
@ -45,6 +45,7 @@ trait DataObject
|
|||
*/
|
||||
public static function fromArray(array $input): ?static
|
||||
{
|
||||
$logger = new Log();
|
||||
$parameters = RuleFactory::getParametersMeta(static::class);
|
||||
foreach ($parameters as $parameter) {
|
||||
$parameterName = $parameter->reflection->getName();
|
||||
|
|
@ -66,12 +67,15 @@ trait DataObject
|
|||
continue;
|
||||
}
|
||||
}
|
||||
$logger->inputRaw($input);
|
||||
|
||||
$rules = static::getRules();
|
||||
$logger->rules($rules);
|
||||
|
||||
$validator = static::withValidator($input, $rules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$logger->validationErrors($validator->errors()->toArray());
|
||||
return static::fails($validator);
|
||||
}
|
||||
|
||||
|
|
@ -94,6 +98,7 @@ trait DataObject
|
|||
$parameter->reflection,
|
||||
);
|
||||
}
|
||||
$logger->input($mappedInput);
|
||||
return App::make(static::class, $mappedInput);
|
||||
}
|
||||
|
||||
|
|
|
|||
67
src/Log.php
Normal file
67
src/Log.php
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Psr\Log\LogLevel;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
readonly class Log
|
||||
{
|
||||
public LoggerInterface $logger;
|
||||
public function __construct()
|
||||
{
|
||||
$raw = config('dto.log.logger');
|
||||
if (is_callable($raw)) {
|
||||
$this->logger = App::call($raw);
|
||||
return;
|
||||
}
|
||||
if (is_object($raw)) {
|
||||
$this->logger = $raw;
|
||||
return;
|
||||
}
|
||||
if (is_string($raw) && class_exists($raw)) {
|
||||
$this->logger = App::make($raw);
|
||||
return;
|
||||
}
|
||||
$this->logger = new NullLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,array<int, string|Rule>> $rules
|
||||
*/
|
||||
public function rules(array $rules): void
|
||||
{
|
||||
$level = config('dto.log.rules') ?? LogLevel::DEBUG;
|
||||
$this->logger->log($level, print_r($rules, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $input
|
||||
*/
|
||||
public function input(array $input): void
|
||||
{
|
||||
$level = config('dto.log.input') ?? LogLevel::DEBUG;
|
||||
$this->logger->log($level, print_r($input, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, null|int|float|string|array> $input
|
||||
*/
|
||||
public function inputRaw(array $input): void
|
||||
{
|
||||
$level = config('dto.log.raw_input') ?? LogLevel::DEBUG;
|
||||
$this->logger->log($level, print_r($input, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<int, string>> $errors
|
||||
*/
|
||||
public function validationErrors(array $errors): void
|
||||
{
|
||||
$level = config('dto.log.validation_errors') ?? LogLevel::INFO;
|
||||
$this->logger->log($level, print_r($errors, true));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,20 @@
|
|||
|
||||
use Icefox\DTO\Factories\CollectionFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Psr\Log\LogLevel;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
return [
|
||||
'cast' => [],
|
||||
'rules' => [
|
||||
Collection::class => CollectionFactory::rules(...),
|
||||
],
|
||||
'log' => [
|
||||
'logger' => NullLogger::class,
|
||||
'internal' => LogLevel::WARNING,
|
||||
'rules' => LogLevel::DEBUG,
|
||||
'input' => LogLevel::DEBUG,
|
||||
'raw_input' => LogLevel::DEBUG,
|
||||
'validation_errors' => LogLevel::INFO,
|
||||
],
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue