Skip to content

Commit

Permalink
Refactored logger, so that we expose our own adapter. (#325)
Browse files Browse the repository at this point in the history
Refactored logger, so that we expose our own adapter, which covers functionality of previously used `AbstractOuzoLogger`.

Added ability to enhance context parameters.
  • Loading branch information
bbankowski authored Oct 21, 2024
1 parent ffe9daa commit 6b16790
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 121 deletions.
66 changes: 0 additions & 66 deletions src/Ouzo/Core/Logger/AbstractOuzoLogger.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Ouzo/Core/Logger/ContextEnhancer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/*
* Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
* This file is made available under the MIT License (view the LICENSE file for more information).
*/

namespace Ouzo\Logger;

interface ContextEnhancer
{
public function enhanceContext(array $context): array;
}
8 changes: 4 additions & 4 deletions src/Ouzo/Core/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Logger
{
private static array $loggers = [];

public static function getLogger(string $name, string $configuration = 'default'): LoggerInterface
public static function getLogger(string $name, string $configuration = 'default'): LoggerAdapter
{
$logger = Arrays::getNestedValue(self::$loggers, [$name, $configuration]);
if (!$logger) {
$logger = self::loadLogger($name, $configuration);
if (is_null($logger)) {
$logger = new LoggerAdapter($name, $configuration, self::loadLogger($name, $configuration));
Arrays::setNestedValue(self::$loggers, [$name, $configuration], $logger);
}
return $logger;
Expand All @@ -43,7 +43,7 @@ public static function clearLogger(string $name, string $configuration = 'defaul
private static function loadLogger(string $name, string $configuration): LoggerInterface
{
$logger = Config::getValue('logger', $configuration);
if (!$logger || !isset($logger['class'])) {
if (is_null($logger) || !isset($logger['class'])) {
return new SyslogLogger($name, $configuration);
}
return new $logger['class']($name, $configuration);
Expand Down
98 changes: 98 additions & 0 deletions src/Ouzo/Core/Logger/LoggerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/*
* Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
* This file is made available under the MIT License (view the LICENSE file for more information).
*/

namespace Ouzo\Logger;

use Ouzo\Config;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Strings;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class LoggerAdapter
{
private MessageFormatter $messageFormatter;
private ?ContextEnhancer $contextEnhancer;
private array $minimalLevels;

public function __construct(
private string $name,
string $configuration,
private LoggerInterface $logger,
)
{
$loggerConfiguration = Config::getValue('logger', $configuration) ?: [];
$messageFormatter = Arrays::getValue($loggerConfiguration, 'formatter', DefaultMessageFormatter::class);
$contextEnhancer = Arrays::getValue($loggerConfiguration, 'context_enhancer');

$this->messageFormatter = new $messageFormatter();
$this->contextEnhancer = is_null($contextEnhancer) ? null : new $contextEnhancer();
$this->minimalLevels = Arrays::getValue($loggerConfiguration, 'minimal_levels', []);
}

public function emergency(string $message, array $context = []): void
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

public function alert(string $message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}

public function critical(string $message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

public function error(string $message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}

public function warning(string $message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}

public function notice(string $message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}

public function info(string $message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}

public function debug(string $message, array $context = []): void
{
if ($this->isDebug()) {
$this->log(LogLevel::DEBUG, $message, $context);
}
}

public function log(string $level, $message, array $context = []): void
{
$ouzoLevel = LogLevelTranslator::toSyslogLevel($level);
$minimalLevel = $this->minimalLevels ? Arrays::getValue($this->minimalLevels, $this->name, LOG_DEBUG) : LOG_DEBUG;
if ($ouzoLevel <= $minimalLevel) {
$message = $this->messageFormatter->format($this->name, $level, $message);
if (!is_null($this->contextEnhancer)) {
$context = $this->contextEnhancer->enhanceContext($context);
}
if (!empty($context)) {
$message = Strings::sprintAssoc($message, $context);
}
$this->logger->log($level, $message, $context);
}
}

private function isDebug(): bool
{
return Config::getValue('debug') === true;
}
}
25 changes: 11 additions & 14 deletions src/Ouzo/Core/Logger/StdOutputLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
namespace Ouzo\Logger;

use Ouzo\Utilities\Clock;
use Psr\Log\AbstractLogger;

class StdOutputLogger extends AbstractOuzoLogger
class StdOutputLogger extends AbstractLogger
{
public function __construct(string $name, string $configuration, private string $outputStreamIdentifier = 'php')
{
parent::__construct($name, $configuration);
}

public function log($level, $message, array $context = []): void
{
$stdOut = $this->getStreamForLogLevel($level);
$date = Clock::nowAsString();
$fileHandle = fopen($stdOut, 'a');
fwrite($fileHandle, "$date: $message\n");
fclose($fileHandle);
}

private function errorStreamName(): string
Expand All @@ -25,23 +34,11 @@ private function standardStreamName(): string
return "{$this->outputStreamIdentifier}://stdout";
}


private function getStreamForLogLevel(string $logLevel): string
{
if (LogLevelTranslator::toSyslogLevel($logLevel) >= LOG_WARNING) {
return $this->standardStreamName();
}
return $this->errorStreamName();
}

public function log($level, $message, array $context = [])
{
$stdOut = $this->getStreamForLogLevel($level);
$this->logWithFunction(function ($message) use ($stdOut) {
$date = Clock::nowAsString();
$fileHandle = fopen($stdOut, 'a');
fwrite($fileHandle, "$date: $message\n");
fclose($fileHandle);
}, $level, $message, $context);
}
}
21 changes: 10 additions & 11 deletions src/Ouzo/Core/Logger/SyslogLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@
namespace Ouzo\Logger;

use Ouzo\Config;
use Psr\Log\AbstractLogger;

class SyslogLogger extends AbstractOuzoLogger
class SyslogLogger extends AbstractLogger
{
const MAX_MESSAGE_SIZE = 1024;

private SyslogAdapter $syslogAdapter;
private ?array $loggerConfiguration;

public function __construct($name, $configuration, SyslogAdapter $syslogAdapter = null)
public function __construct(string $name, string $configuration, SyslogAdapter $syslogAdapter = null)
{
parent::__construct($name, $configuration);
$this->syslogAdapter = $syslogAdapter ?: new SyslogAdapter();
$this->loggerConfiguration = Config::getValue('logger', $configuration);
}

public function __destruct()
{
closelog();
}

public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
$loggerConfiguration = $this->getLoggerConfiguration();
$syslogLevel = LogLevelTranslator::toSyslogLevel($level);
$this->logWithFunction(function ($message) use ($loggerConfiguration, $syslogLevel) {
if ($loggerConfiguration) {
$this->syslogAdapter->open($loggerConfiguration);
}
$this->logMessage($syslogLevel, $message);
}, $level, $message, $context);
if (!is_null($this->loggerConfiguration)) {
$this->syslogAdapter->open($this->loggerConfiguration);
}
$this->logMessage($syslogLevel, $message);
}

private function logMessage(string $level, string $message): void
Expand Down
Loading

0 comments on commit 6b16790

Please sign in to comment.