Skip to content

Commit

Permalink
Merge pull request #33 from mteu/feature/limit-logger-responsibility-…
Browse files Browse the repository at this point in the history
…by-loglevel
  • Loading branch information
mteu authored Sep 6, 2024
2 parents efc64a1 + 1e1a40e commit dcd8b47
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 30 deletions.
53 changes: 53 additions & 0 deletions Classes/Log/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS extension "mteu/typo3-stream-writer".
*
* Copyright (C) 2024 Martin Adler <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace mteu\StreamWriter\Log;

/**
* @codeCoverageIgnore
*/
enum LogLevel: string
{
case ALERT = 'alert';
case CRITICAL = 'critical';
case DEBUG = 'debug';
case EMERGENCY = 'emergency';
case ERROR = 'error';
case INFO = 'info';
case NOTICE = 'notice';
case WARNING = 'warning';

public function priority(): int
{
return match ($this) {
self::EMERGENCY => 8,
self::ALERT => 7,
self::CRITICAL => 6,
self::ERROR => 5,
self::WARNING => 4,
self::NOTICE => 3,
self::INFO => 2,
self::DEBUG => 1,
};
}
}
64 changes: 54 additions & 10 deletions Classes/Log/Writer/StreamWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace mteu\StreamWriter\Log\Writer;

use mteu\StreamWriter\Log\Config\StandardStream;
use mteu\StreamWriter\Log\LogLevel;
use TYPO3\CMS\Core\Log\Exception\InvalidLogWriterConfigurationException;
use TYPO3\CMS\Core\Log\LogRecord;
use TYPO3\CMS\Core\Log\Writer\AbstractWriter;
Expand All @@ -40,7 +41,7 @@ final class StreamWriter extends AbstractWriter
/**
* @var class-string[]
*/
private array $ignoredComponents = [];
private readonly array $ignoredComponents;

/**
* @var class-string[] $exceptionHandlers
Expand All @@ -50,6 +51,8 @@ final class StreamWriter extends AbstractWriter
\TYPO3\CMS\Frontend\ContentObject\Exception\ExceptionHandlerInterface::class,
];

private readonly LogLevel $maxLevel;

private readonly StandardStream $outputStream;

/**
Expand All @@ -60,14 +63,29 @@ public function __construct(array $options = ['outputStream' => StandardStream::
{
parent::__construct();

$this->outputStream = $this->validateWriterOptions($options);
$this->maxLevel = $this->determineMaximalLevel($options);
$this->outputStream = $this->getOutputStreamOption($options);
$this->ignoredComponents = $this->getIgnoredComponentsOption($options);
}

/**
* @param mixed[] $options
* @return class-string[]
*/
private function getIgnoredComponentsOption(array $options): array
{
if (array_key_exists('ignoreComponents', $options)) {
return $options['ignoreComponents'];
}

return [];
}

/**
* @param mixed[] $options
* @throws InvalidLogWriterConfigurationException
*/
private function validateWriterOptions(array $options): StandardStream
private function getOutputStreamOption(array $options): StandardStream
{
if (!array_key_exists('outputStream', $options) ||
$options['outputStream'] === '' ||
Expand All @@ -86,10 +104,6 @@ private function validateWriterOptions(array $options): StandardStream
);
}

if (array_key_exists('ignoreComponents', $options)) {
$this->ignoredComponents = $options['ignoreComponents'];
}

return $options['outputStream'];
}

Expand All @@ -99,6 +113,17 @@ public function writeLog(LogRecord $record): WriterInterface
return $this;
}

if (!$this->levelIsWithinBounds($record)) {
return $this;
}

$this->writeToResource($record);

return $this;
}

private function writeToResource(LogRecord $record): void
{
$resource = fopen($this->outputStream->value, 'w');

if ($resource === false) {
Expand All @@ -117,11 +142,9 @@ public function writeLog(LogRecord $record): WriterInterface
),
);

if ($output === false) {
if ($output === false || $output === 0) {
throw new \RuntimeException('Unable to write to ' . $this->outputStream->value . '.', 1722331958);
}

return $this;
}

private function generateMessageForExceptionHandler(LogRecord $record): string
Expand Down Expand Up @@ -163,4 +186,25 @@ private function isExceptionHandler(string $component): bool

return false;
}

/**
* @param mixed[] $options
*/
private function determineMaximalLevel(array $options): LogLevel
{
$default = LogLevel::CRITICAL;

if (!array_key_exists('maxLevel', $options)) {
return $default;
}

return LogLevel::tryFrom($options['maxLevel']) ?? $default;
}

private function levelIsWithinBounds(LogRecord $record): bool
{
$logLevelPriority = LogLevel::tryFrom($record->getLevel())?->priority();

return $logLevelPriority <= $this->maxLevel->priority();
}
}
28 changes: 28 additions & 0 deletions Documentation/writer-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# LogWriter Configuration

## Options
The TYPO3 Stream Writer currently supports following options:

| Option | Required | Value | Description |
|--------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `outputStream` | required | [`StandardStream::Err`](../Classes/Log/Config/StandardStream.php) or <br/>[`StandardStream::Out`](../Classes/Log/Config/StandardStream.php) | Direct your stream output either to `php://stdout` or `php://sterr` |
| `ignoreComponents` | optional | Array of classes to be excluded from logging via this LogWriter | Please note that classes are currently repesented by the full qualified class name syntax the TYPO3 Logging Framework uses.<br/> _This is likely to change to accept actual class strings there, too_. |
| `maxLevel` | optional | `Psr\Log\LogLevel` | Although, you may go with their respective `string` representations, it is recommended to stick to `Psr\Log\LogLevel` for compatibility. |
| | | | |

## Example

```php
$GLOBALS['TYPO3_CONF_VARS']['LOG']['writerConfiguration'] = [
\Psr\Log\LogLevel::DEBUG => [
StreamWriter::class => [
'outputStream' => StandardStream::Out,
'ignoreComponents' => [
'TYPO3.CMS.Core.Authentication.BackendUserAuthentication',
'TYPO3.CMS.Frontend.Authentication.FrontendUserAuthentication',
],
'maxLevel' => Psr\Log\LogLevel::WARNING,
],
],
];
```
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

[![TYPO3 12](https://img.shields.io/badge/TYPO3-12-orange.svg)](https://get.typo3.org/version/12)
[![TYPO3 13](https://img.shields.io/badge/TYPO3-13-orange.svg)](https://get.typo3.org/version/13)

[![CGL](https://github.com/mteu/typo3-stream-writer/actions/workflows/cgl.yaml/badge.svg)](https://github.com/mteu/typo3-stream-writer/actions/workflows/cgl.yaml)
[![Tests](https://github.com/mteu/typo3-stream-writer/actions/workflows/tests.yaml/badge.svg?branch=main)](https://github.com/mteu/typo3-stream-writer/actions/workflows/tests.yaml)
[![codecov](https://codecov.io/gh/mteu/typo3-stream-writer/graph/badge.svg?token=XIx5ikuAYF)](https://codecov.io/gh/mteu/typo3-stream-writer)
[![Coverage Status](https://coveralls.io/repos/github/mteu/typo3-stream-writer/badge.svg)](https://coveralls.io/github/mteu/typo3-stream-writer)
[![Maintainability](https://api.codeclimate.com/v1/badges/edd606b0c4de053a2762/maintainability)](https://codeclimate.com/github/mteu/typo3-stream-writer/maintainability)

# TYPO3 Stream Writer
# TYPO3 Stream Writer 📜
</div>

This TYPO3 CMS extensions adds a custom `LogWriter` to the TYPO3 Logging Framework allowing the CMS to log messages to
Expand Down Expand Up @@ -52,9 +51,12 @@ $GLOBALS['TYPO3_CONF_VARS']['LOG']['writerConfiguration'] = [
'TYPO3.CMS.Core.Authentication.BackendUserAuthentication',
'TYPO3.CMS.Frontend.Authentication.FrontendUserAuthentication',
],
'maxLevel' => Psr\Log\LogLevel::WARNING,
],
],
];
```
> 💡 Learn more about the LogWriter configuration in [`WriterConfiguration`](Documentation/writer-configuration.md).
## ⭐ License
This project is licensed under [GNU General Public License 3.0 (or later)](LICENSE).
Loading

0 comments on commit dcd8b47

Please sign in to comment.