improve code style

This commit is contained in:
Daniel Seifert 2025-02-08 22:23:57 +01:00
parent 54022196ce
commit d0a906c403
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
2 changed files with 28 additions and 19 deletions

View File

@ -59,8 +59,7 @@ class LoggerFactory
int $logLevel = Logger::INFO, int $logLevel = Logger::INFO,
?int $maxFiles = null, ?int $maxFiles = null,
array $specialHandlers = [] array $specialHandlers = []
): Logger ): Logger {
{
$logger = new Logger($loggerName); $logger = new Logger($loggerName);
$handler = $this->applySpecialHandlers( $handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles), $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
@ -78,8 +77,7 @@ class LoggerFactory
string $filePath, string $filePath,
int $logLevel = Logger::INFO, int $logLevel = Logger::INFO,
?int $maxFiles = null ?int $maxFiles = null
): AbstractProcessingHandler ): AbstractProcessingHandler {
{
return is_null($maxFiles) ? return is_null($maxFiles) ?
new StreamHandler($filePath, $logLevel) : new StreamHandler($filePath, $logLevel) :
new RotatingFileHandler($filePath, $maxFiles, $logLevel); new RotatingFileHandler($filePath, $maxFiles, $logLevel);
@ -94,8 +92,7 @@ class LoggerFactory
int $logLevel = Logger::INFO, int $logLevel = Logger::INFO,
?int $maxFiles = null, ?int $maxFiles = null,
array $specialHandlers = [] array $specialHandlers = []
): Logger ): Logger {
{
if (!class_exists(Registry::class)) { if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
} }
@ -129,8 +126,7 @@ class LoggerFactory
public function applySpecialHandlers( public function applySpecialHandlers(
AbstractProcessingHandler $handler, AbstractProcessingHandler $handler,
array $specialHandlers = [] array $specialHandlers = []
): HandlerInterface ): HandlerInterface {
{
if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlers, true)) { if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlers, true)) {
$handler = $this->setBuffering($handler); $handler = $this->setBuffering($handler);
} elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlers), true)) { } elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlers), true)) {
@ -170,16 +166,14 @@ class LoggerFactory
AbstractHandler $handler, AbstractHandler $handler,
int $bufferLimit = 0, int $bufferLimit = 0,
int $loglevel = Logger::DEBUG int $loglevel = Logger::DEBUG
): BufferHandler ): BufferHandler {
{
return new BufferHandler($handler, $bufferLimit, $loglevel); return new BufferHandler($handler, $bufferLimit, $loglevel);
} }
public function setLogItemsOnErrorOnly( public function setLogItemsOnErrorOnly(
AbstractHandler $handler, AbstractHandler $handler,
int $activationLevel = Logger::ERROR int $activationLevel = Logger::ERROR
): FingersCrossedHandler ): FingersCrossedHandler {
{
return new FingersCrossedHandler( return new FingersCrossedHandler(
$handler, $handler,
new ErrorLevelActivationStrategy($activationLevel) new ErrorLevelActivationStrategy($activationLevel)
@ -190,8 +184,7 @@ class LoggerFactory
AbstractHandler $handler, AbstractHandler $handler,
int $deduplicationLevel = Logger::ERROR, int $deduplicationLevel = Logger::ERROR,
int $time = 60 int $time = 60
): DeduplicationHandler ): DeduplicationHandler {
{
return new DeduplicationHandler($handler, null, $deduplicationLevel, $time); return new DeduplicationHandler($handler, null, $deduplicationLevel, $time);
} }
} }

View File

@ -1,5 +1,18 @@
<?php <?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
namespace D3\LoggerFactory\tests; namespace D3\LoggerFactory\tests;
use D3\LoggerFactory\LoggerFactory; use D3\LoggerFactory\LoggerFactory;
@ -14,6 +27,9 @@ use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
use RuntimeException; use RuntimeException;
/**
* @coversNothing
*/
class LoggerFactoryTest extends ApiTestCase class LoggerFactoryTest extends ApiTestCase
{ {
/** /**
@ -181,17 +197,17 @@ class LoggerFactoryTest extends ApiTestCase
yield 'simple buffering' => [[LoggerFactory::SPECIAL_HANDLERS_BUFFERING], BufferHandler::class]; yield 'simple buffering' => [[LoggerFactory::SPECIAL_HANDLERS_BUFFERING], BufferHandler::class];
yield 'advanced buffering' => [ yield 'advanced buffering' => [
[LoggerFactory::SPECIAL_HANDLERS_BUFFERING => [LoggerFactory::BUFFERING_OPTION_LIMIT => 10]], [LoggerFactory::SPECIAL_HANDLERS_BUFFERING => [LoggerFactory::BUFFERING_OPTION_LIMIT => 10]],
BufferHandler::class BufferHandler::class,
]; ];
yield 'simple logOnErrorOnly' => [[LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY], FingersCrossedHandler::class]; yield 'simple logOnErrorOnly' => [[LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY], FingersCrossedHandler::class];
yield 'advanced logOnErrorOnly' => [ yield 'advanced logOnErrorOnly' => [
[LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY => [LoggerFactory::LOGONERRORONLY_LEVEL => Logger::DEBUG]], [LoggerFactory::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY => [LoggerFactory::LOGONERRORONLY_LEVEL => Logger::DEBUG]],
FingersCrossedHandler::class FingersCrossedHandler::class,
]; ];
yield 'simple deduplicate' => [[LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE], DeduplicationHandler::class]; yield 'simple deduplicate' => [[LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE], DeduplicationHandler::class];
yield 'advanced deduplicate' => [ yield 'advanced deduplicate' => [
[LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE => [LoggerFactory::MAKEUNIQUE_OPTION_TIME => 30]], [LoggerFactory::SPECIAL_HANDLERS_MAKE_UNIQUE => [LoggerFactory::MAKEUNIQUE_OPTION_TIME => 30]],
DeduplicationHandler::class DeduplicationHandler::class,
]; ];
} }