Skip to content

Commit

Permalink
Merge pull request #1 from AnniWeb/master
Browse files Browse the repository at this point in the history
Обновление psr/log
  • Loading branch information
alex19pov31 authored Oct 10, 2023
2 parents 3844631 + df8e0ba commit 3b97105
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 28 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $simpleTextLogger = new SimpleTextLogger(
'Y/m/d H:i:s', // формат даты/времени
"{date} {level}:\t{message}" // формат сообщения
)
$loggerManager = new LoggerManager($simpleTextLogger); // создаем новый менеджер с логером по-умолчанию
$loggerManager = new LoggerManager($simpleTextLogger); // создаем новый менеджер с логером по-умолчанию и типом default

$journalLogger = new JournalLogger('my.module');
$loggerManager->setLogger($journalLogger, LogLevel::ERROR); // логи с ошибками будут записаны в журнал событий битрикса
Expand All @@ -37,4 +37,7 @@ $loggerManager->error('Some error message', [ // сооб
'ITEM_ID' => 1,
]);
$loggerManager->warning('Some warning message'); // сообщение будет записано в файл LOG_FILENAME

// создаем новый менеджер с логером по-умолчанию и типом test
$loggerManager = new LoggerManager(\Bx\Logger\TypedLoggerFactory::createTypedLogger($simpleTextLogger, 'test'));
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=8.1",
"ext-json": "*",
"psr/log": "^1.1.4"
"psr/log": "^1|^2|^3"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.6",
Expand Down
5 changes: 3 additions & 2 deletions lib/debuglogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

use Bitrix\Main\Diag\Debug;
use Psr\Log\AbstractLogger;
use Stringable;

class DebugLogger extends AbstractLogger
{
/**
* @var string
*/
private $fileName;
private string $fileName;

public function __construct(string $fileName)
{
$this->fileName = $fileName;
}

public function log($level, $message, array $context = array())
public function log($level, string|Stringable $message, array $context = []): void
{
$isDumpMode = $context['DUMP'] === true;

Expand Down
8 changes: 5 additions & 3 deletions lib/filelogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace Bx\Logger;

use Psr\Log\AbstractLogger;
use Stringable;

class FileLogger extends AbstractLogger
{
/**
* @param mixed $level
* @param string $message
* @param $level
* @param string|Stringable $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, string|Stringable $message, array $context = []): void
{
AddMessage2Log(
"{$level}: " . Utils::interpolate($message, $context),
Expand Down
10 changes: 10 additions & 0 deletions lib/interfaces/typedloggerinterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Bx\Logger\Interfaces;

use Psr\Log\LoggerInterface;

interface TypedLoggerInterface extends LoggerInterface
{
public function getType(): string;
}
14 changes: 7 additions & 7 deletions lib/journallogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

use Bitrix\Main\Application;
use Bitrix\Main\EventLog\Internal\EventLogTable;
use Exception;
use Psr\Log\AbstractLogger;
use Stringable;

class JournalLogger extends AbstractLogger
{
/**
* @var string
*/
private $defaultModuleId;
private string $defaultModuleId;
/**
* @var string
*/
private $defaultAuditTypeId;
private string $defaultAuditTypeId;

public function __construct(string $moduleId = '', string $auditTypeId = '')
{
Expand All @@ -25,12 +25,12 @@ public function __construct(string $moduleId = '', string $auditTypeId = '')
}

/**
* @param mixed $level
* @param string $message
* @param $level
* @param string|Stringable $message
* @param array $context
* @throws Exception
* @return void
*/
public function log($level, $message, array $context = [])
public function log($level, string|Stringable $message, array $context = []): void
{
global $USER;
$appContext = Application::getInstance()->getContext();
Expand Down
22 changes: 13 additions & 9 deletions lib/loggermanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,42 @@

use Bitrix\Main\Result;
use Bx\Logger\Interfaces\LoggerManagerInterface;
use Bx\Logger\Interfaces\TypedLoggerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Stringable;
use Throwable;

class LoggerManager implements LoggerManagerInterface
{
use LoggerTrait;
private const DEFAULT_LOG = 'default';

/**
* @var LoggerInterface[]
*/
private $logger = [];
private array $logger = [];

public function __construct(LoggerInterface $defaultLogger)
{
$this->logger['default'] = $defaultLogger;
$this->logger[$this::DEFAULT_LOG] = $defaultLogger;
}

/**
* @param LoggerInterface $logger
* @param string $loggerType
*/
public function setLogger(LoggerInterface $logger, string $loggerType = 'default')
public function setLogger(LoggerInterface $logger): void
{
$this->logger[$loggerType] = $logger;
$type = $logger instanceof TypedLoggerInterface ? $logger->getType() : $this::DEFAULT_LOG;
$this->logger[$type ?: $this::DEFAULT_LOG] = $logger;
}

/**
* @return LoggerInterface
*/
private function getDefaultLogger(): LoggerInterface
{
return $this->logger['default'];
return $this->logger[$this::DEFAULT_LOG];
}

/**
Expand Down Expand Up @@ -96,11 +99,12 @@ public function logResult(Result $result, string $successMessage)
}

/**
* @param mixed $level
* @param string $message
* @param $level
* @param string|Stringable $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, string|Stringable $message, array $context = []): void
{
$this->getLoggerByType($level)->log($level, $message, $context);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/simpletextlogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bitrix\Main\Type\DateTime;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Stringable;

class SimpleTextLogger implements LoggerInterface
{
Expand All @@ -13,15 +14,15 @@ class SimpleTextLogger implements LoggerInterface
/**
* @var string
*/
private $filePath;
private string $filePath;
/**
* @var string
*/
private $messageFormat;
private string $messageFormat;
/**
* @var string
*/
private $dateFormat;
private string $dateFormat;

public function __construct(string $filePath, string $dateFormat = null, string $messageFormat = null)
{
Expand All @@ -36,7 +37,7 @@ public function __construct(string $filePath, string $dateFormat = null, string
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, string|Stringable $message, array $context = []): void
{
$fp = fopen($this->filePath, 'ab');
if (!$fp) {
Expand Down
96 changes: 96 additions & 0 deletions lib/typedlogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Bx\Logger;

use Bx\Logger\Interfaces\TypedLoggerInterface;
use Psr\Log\LoggerInterface;
use Stringable;

class TypedLogger implements TypedLoggerInterface
{
private LoggerInterface $originalLogger;
private string $type;

public function __construct(LoggerInterface $logger, string $type)
{
$this->originalLogger = $logger;
$this->type = $type;
}

/**
* @inheritDoc
*/
public function emergency(Stringable|string $message, array $context = []): void
{
$this->originalLogger->emergency($message, $context);
}

/**
* @inheritDoc
*/
public function alert(Stringable|string $message, array $context = []): void
{
$this->originalLogger->alert($message, $context);
}

/**
* @inheritDoc
*/
public function critical(Stringable|string $message, array $context = []): void
{
$this->originalLogger->critical($message, $context);
}

/**
* @inheritDoc
*/
public function error(Stringable|string $message, array $context = []): void
{
$this->originalLogger->error($message, $context);
}

/**
* @inheritDoc
*/
public function warning(Stringable|string $message, array $context = []): void
{
$this->originalLogger->warning($message, $context);
}

/**
* @inheritDoc
*/
public function notice(Stringable|string $message, array $context = []): void
{
$this->originalLogger->notice($message, $context);
}

/**
* @inheritDoc
*/
public function info(Stringable|string $message, array $context = []): void
{
$this->originalLogger->info($message, $context);
}

/**
* @inheritDoc
*/
public function debug(Stringable|string $message, array $context = []): void
{
$this->originalLogger->debug($message, $context);
}

/**
* @inheritDoc
*/
public function log($level, Stringable|string $message, array $context = []): void
{
$this->originalLogger->log($level, $message, $context);
}

public function getType(): string
{
return $this->type;
}
}
19 changes: 19 additions & 0 deletions lib/typedloggerfactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bx\Logger;

use Bx\Logger\Interfaces\TypedLoggerInterface;
use Psr\Log\LoggerInterface;

class TypedLoggerFactory
{
/**
* @param LoggerInterface $logger
* @param string $type
* @return TypedLoggerInterface
*/
public static function createTypedLogger(LoggerInterface $logger, string $type): TypedLoggerInterface
{
return new TypedLogger($logger, $type);
}
}

0 comments on commit 3b97105

Please sign in to comment.