Use a enum for level in LogEntries

This commit is contained in:
Jan Böhmer 2023-06-18 17:25:55 +02:00
parent 4a644d8712
commit 2da7463edf
18 changed files with 322 additions and 149 deletions

View file

@ -46,7 +46,7 @@ use App\Entity\UserSystem\User;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
use Psr\Log\LogLevel;
use Psr\Log\LogLevel as PsrLogLevel;
use App\Repository\LogEntryRepository;
/**
@ -63,15 +63,6 @@ use App\Repository\LogEntryRepository;
#[ORM\Index(columns: ['datetime'], name: 'log_idx_datetime')]
abstract class AbstractLogEntry extends AbstractDBElement
{
final public const LEVEL_EMERGENCY = 0;
final public const LEVEL_ALERT = 1;
final public const LEVEL_CRITICAL = 2;
final public const LEVEL_ERROR = 3;
final public const LEVEL_WARNING = 4;
final public const LEVEL_NOTICE = 5;
final public const LEVEL_INFO = 6;
final public const LEVEL_DEBUG = 7;
protected const TARGET_TYPE_NONE = 0;
protected const TARGET_TYPE_USER = 1;
protected const TARGET_TYPE_ATTACHEMENT = 2;
@ -93,19 +84,6 @@ abstract class AbstractLogEntry extends AbstractDBElement
protected const TARGET_TYPE_PARAMETER = 18;
protected const TARGET_TYPE_LABEL_PROFILE = 19;
/**
* @var array This const is used to convert the numeric level to a PSR-3 compatible log level
*/
protected const LEVEL_ID_TO_STRING = [
self::LEVEL_EMERGENCY => LogLevel::EMERGENCY,
self::LEVEL_ALERT => LogLevel::ALERT,
self::LEVEL_CRITICAL => LogLevel::CRITICAL,
self::LEVEL_ERROR => LogLevel::ERROR,
self::LEVEL_WARNING => LogLevel::WARNING,
self::LEVEL_NOTICE => LogLevel::NOTICE,
self::LEVEL_INFO => LogLevel::INFO,
self::LEVEL_DEBUG => LogLevel::DEBUG,
];
protected const TARGET_CLASS_MAPPING = [
self::TARGET_TYPE_USER => User::class,
@ -146,10 +124,11 @@ abstract class AbstractLogEntry extends AbstractDBElement
#[ORM\Column(name: 'datetime', type: Types::DATETIME_MUTABLE)]
protected \DateTimeInterface $timestamp;
/** @var int The priority level of the associated level. 0 is highest, 7 lowest
/**
* @var LogLevel The priority level of the associated level. 0 is highest, 7 lowest
*/
#[ORM\Column(type: 'tinyint', name: 'level')]
protected int $level = self::LEVEL_WARNING;
#[ORM\Column(name: 'level', type: 'tinyint', enumType: LogLevel::class)]
protected LogLevel $level = LogLevel::WARNING;
/** @var int The ID of the element targeted by this event
*/
@ -267,16 +246,10 @@ abstract class AbstractLogEntry extends AbstractDBElement
}
/**
* Get the priority level of this log entry. 0 is highest and 7 lowest level.
* See LEVEL_* consts in this class for more info.
* Get the priority level of this log entry.
*/
public function getLevel(): int
public function getLevel(): LogLevel
{
//It is always alerting when a wrong int is saved in DB...
if ($this->level < 0 || $this->level > 7) {
return self::LEVEL_ALERT;
}
return $this->level;
}
@ -285,13 +258,9 @@ abstract class AbstractLogEntry extends AbstractDBElement
*
* @return $this
*/
public function setLevel(int $level): self
public function setLevel(LogLevel $level): self
{
if ($level < 0 || $this->level > 7) {
throw new InvalidArgumentException(sprintf('$level must be between 0 and 7! %d given!', $level));
}
$this->level = $level;
return $this;
}
@ -300,7 +269,7 @@ abstract class AbstractLogEntry extends AbstractDBElement
*/
public function getLevelString(): string
{
return self::levelIntToString($this->getLevel());
return $this->level->toPSR3LevelString();
}
/**
@ -310,8 +279,7 @@ abstract class AbstractLogEntry extends AbstractDBElement
*/
public function setLevelString(string $level): self
{
$this->setLevel(self::levelStringToInt($level));
LogLevel::fromPSR3LevelString($level);
return $this;
}
@ -402,39 +370,6 @@ abstract class AbstractLogEntry extends AbstractDBElement
return $this->extra;
}
/**
* This function converts the internal numeric log level into an PSR3 compatible level string.
*
* @param int $level The numerical log level
*
* @return string The PSR3 compatible level string
*/
final public static function levelIntToString(int $level): string
{
if (!isset(self::LEVEL_ID_TO_STRING[$level])) {
throw new InvalidArgumentException('No level with this int is existing!');
}
return self::LEVEL_ID_TO_STRING[$level];
}
/**
* This function converts a PSR3 compatible string to the internal numeric level string.
*
* @param string $level the PSR3 compatible string that should be converted
*
* @return int the internal int representation
*/
final public static function levelStringToInt(string $level): int
{
$tmp = array_flip(self::LEVEL_ID_TO_STRING);
if (!isset($tmp[$level])) {
throw new InvalidArgumentException('No level with this string is existing!');
}
return $tmp[$level];
}
/**
* Converts a target type id to a full qualified class name.
*

View file

@ -88,11 +88,13 @@ use InvalidArgumentException;
class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventUndoInterface
{
protected string $typeString = 'collection_element_deleted';
protected int $level = self::LEVEL_INFO;
public function __construct(AbstractDBElement $changed_element, string $collection_name, AbstractDBElement $deletedElement)
{
parent::__construct();
$this->level = LogLevel::INFO;
$this->setTargetElement($changed_element);
$this->extra['n'] = $collection_name;
$this->extra['c'] = self::targetTypeClassToID($deletedElement::class);

View file

@ -38,12 +38,12 @@ class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentI
public function __construct(AbstractDBElement $new_element)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setTargetElement($new_element);
//Creation of new users is maybe more interesting...
if ($new_element instanceof User || $new_element instanceof Group) {
$this->level = self::LEVEL_NOTICE;
$this->level = LogLevel::NOTICE;
}
}

View file

@ -40,12 +40,12 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter
public function __construct(AbstractDBElement $deleted_element)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setTargetElement($deleted_element);
//Deletion of a user is maybe more interesting...
if ($deleted_element instanceof User || $deleted_element instanceof Group) {
$this->level = self::LEVEL_NOTICE;
$this->level = LogLevel::NOTICE;
}
}

View file

@ -38,7 +38,7 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
public function __construct(AbstractDBElement $changed_element)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setTargetElement($changed_element);
}

View file

@ -0,0 +1,117 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Entity\LogSystem;
use \Psr\Log\LogLevel as PSRLogLevel;
enum LogLevel: int
{
case EMERGENCY = 0;
case ALERT = 1;
case CRITICAL = 2;
case ERROR = 3;
case WARNING = 4;
case NOTICE = 5;
case INFO = 6;
case DEBUG = 7;
/**
* Converts the current log level to a PSR-3 log level string.
* @return string
*/
public function toPSR3LevelString(): string
{
return match ($this) {
self::EMERGENCY => PSRLogLevel::EMERGENCY,
self::ALERT => PSRLogLevel::ALERT,
self::CRITICAL => PSRLogLevel::CRITICAL,
self::ERROR => PSRLogLevel::ERROR,
self::WARNING => PSRLogLevel::WARNING,
self::NOTICE => PSRLogLevel::NOTICE,
self::INFO => PSRLogLevel::INFO,
self::DEBUG => PSRLogLevel::DEBUG,
};
}
/**
* Creates a log level (enum) from a PSR-3 log level string.
* @param string $level
* @return self
*/
public static function fromPSR3LevelString(string $level): self
{
return match ($level) {
PSRLogLevel::EMERGENCY => self::EMERGENCY,
PSRLogLevel::ALERT => self::ALERT,
PSRLogLevel::CRITICAL => self::CRITICAL,
PSRLogLevel::ERROR => self::ERROR,
PSRLogLevel::WARNING => self::WARNING,
PSRLogLevel::NOTICE => self::NOTICE,
PSRLogLevel::INFO => self::INFO,
PSRLogLevel::DEBUG => self::DEBUG,
default => throw new \InvalidArgumentException("Invalid log level: $level"),
};
}
/**
* Checks if the current log level is more important than the given one.
* @param LogLevel $other
* @return bool
*/
public function moreImportThan(self $other): bool
{
//Smaller values are more important
return $this->value < $other->value;
}
/**
* Checks if the current log level is more important or equal than the given one.
* @param LogLevel $other
* @return bool
*/
public function moreImportOrEqualThan(self $other): bool
{
//Smaller values are more important
return $this->value <= $other->value;
}
/**
* Checks if the current log level is less important than the given one.
* @param LogLevel $other
* @return bool
*/
public function lessImportThan(self $other): bool
{
//Bigger values are less important
return $this->value > $other->value;
}
/**
* Checks if the current log level is less important or equal than the given one.
* @param LogLevel $other
* @return bool
*/
public function lessImportOrEqualThan(self $other): bool
{
//Bigger values are less important
return $this->value >= $other->value;
}
}

View file

@ -55,7 +55,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
}
//Same as every other element change log entry
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setTargetElement($lot);

View file

@ -69,10 +69,9 @@ class SecurityEventLogEntry extends AbstractLogEntry
public function __construct(string $type, string $ip_address, bool $anonymize = true)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->setIPAddress($ip_address, $anonymize);
$this->setEventType($type);
$this->level = self::LEVEL_NOTICE;
$this->level = LogLevel::NOTICE;
}
public function setTargetElement(?AbstractDBElement $element): AbstractLogEntry

View file

@ -36,7 +36,7 @@ class UserLoginLogEntry extends AbstractLogEntry
public function __construct(string $ip_address, bool $anonymize = true)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setIPAddress($ip_address, $anonymize);
}

View file

@ -33,7 +33,7 @@ class UserLogoutLogEntry extends AbstractLogEntry
public function __construct(string $ip_address, bool $anonymize = true)
{
parent::__construct();
$this->level = self::LEVEL_INFO;
$this->level = LogLevel::INFO;
$this->setIPAddress($ip_address, $anonymize);
}

View file

@ -32,7 +32,7 @@ class UserNotAllowedLogEntry extends AbstractLogEntry
public function __construct(string $path)
{
parent::__construct();
$this->level = static::LEVEL_WARNING;
$this->level = LogLevel::WARNING;
$this->extra['a'] = $path;
}