mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Use a enum for level in LogEntries
This commit is contained in:
parent
4a644d8712
commit
2da7463edf
18 changed files with 322 additions and 149 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
117
src/Entity/LogSystem/LogLevel.php
Normal file
117
src/Entity/LogSystem/LogLevel.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
73
src/Form/Filters/Constraints/EnumConstraintType.php
Normal file
73
src/Form/Filters/Constraints/EnumConstraintType.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 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\Form\Filters\Constraints;
|
||||
|
||||
use App\DataTables\Filters\Constraints\ChoiceConstraint;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EnumType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class EnumConstraintType extends AbstractType
|
||||
{
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setRequired('enum_class');
|
||||
$resolver->setAllowedTypes('enum_class', 'string');
|
||||
|
||||
$resolver->setRequired('choice_label');
|
||||
$resolver->setAllowedTypes('choice_label', ['string', 'callable']);
|
||||
|
||||
$resolver->setDefaults([
|
||||
'compound' => true,
|
||||
'data_class' => ChoiceConstraint::class,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$choices = [
|
||||
'' => '',
|
||||
'filter.choice_constraint.operator.ANY' => 'ANY',
|
||||
'filter.choice_constraint.operator.NONE' => 'NONE',
|
||||
];
|
||||
|
||||
$builder->add('operator', ChoiceType::class, [
|
||||
'choices' => $choices,
|
||||
'required' => false,
|
||||
]);
|
||||
|
||||
$builder->add('value', EnumType::class, [
|
||||
'class' => $options['enum_class'],
|
||||
'choice_label' => $options['choice_label'],
|
||||
'required' => false,
|
||||
'multiple' => true,
|
||||
'attr' => [
|
||||
'data-controller' => 'elements--select-multiple',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@ namespace App\Form\Filters;
|
|||
use App\DataTables\Filters\LogFilter;
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\LogSystem\LogLevel;
|
||||
use App\Entity\LogSystem\PartStockChangedLogEntry;
|
||||
use App\Entity\ProjectSystem\Project;
|
||||
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
||||
|
@ -56,6 +57,7 @@ use App\Entity\UserSystem\Group;
|
|||
use App\Entity\UserSystem\User;
|
||||
use App\Form\Filters\Constraints\ChoiceConstraintType;
|
||||
use App\Form\Filters\Constraints\DateTimeConstraintType;
|
||||
use App\Form\Filters\Constraints\EnumConstraintType;
|
||||
use App\Form\Filters\Constraints\InstanceOfConstraintType;
|
||||
use App\Form\Filters\Constraints\NumberConstraintType;
|
||||
use App\Form\Filters\Constraints\UserEntityConstraintType;
|
||||
|
@ -67,17 +69,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||
|
||||
class LogFilterType extends AbstractType
|
||||
{
|
||||
protected const LEVEL_CHOICES = [
|
||||
'log.level.debug' => AbstractLogEntry::LEVEL_DEBUG,
|
||||
'log.level.info' => AbstractLogEntry::LEVEL_INFO,
|
||||
'log.level.notice' => AbstractLogEntry::LEVEL_NOTICE,
|
||||
'log.level.warning' => AbstractLogEntry::LEVEL_WARNING,
|
||||
'log.level.error' => AbstractLogEntry::LEVEL_ERROR,
|
||||
'log.level.critical' => AbstractLogEntry::LEVEL_CRITICAL,
|
||||
'log.level.alert' => AbstractLogEntry::LEVEL_ALERT,
|
||||
'log.level.emergency' => AbstractLogEntry::LEVEL_EMERGENCY,
|
||||
];
|
||||
|
||||
protected const TARGET_TYPE_CHOICES = [
|
||||
'log.type.collection_element_deleted' => CollectionElementDeleted::class,
|
||||
'log.type.database_updated' => DatabaseUpdatedLogEntry::class,
|
||||
|
@ -117,9 +108,10 @@ class LogFilterType extends AbstractType
|
|||
|
||||
|
||||
|
||||
$builder->add('level', ChoiceConstraintType::class, [
|
||||
$builder->add('level', EnumConstraintType::class, [
|
||||
'label' => 'log.level',
|
||||
'choices' => self::LEVEL_CHOICES,
|
||||
'enum_class' => LogLevel::class,
|
||||
'choice_label' => fn(LogLevel $level): string => 'log.level.' . $level->toPSR3LevelString(),
|
||||
]);
|
||||
|
||||
$builder->add('eventType', InstanceOfConstraintType::class, [
|
||||
|
|
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\LogSystem;
|
||||
|
||||
use App\Entity\LogSystem\LogLevel;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use App\Entity\LogSystem\AbstractLogEntry;
|
||||
|
@ -34,8 +35,11 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
*/
|
||||
class EventLogger
|
||||
{
|
||||
public function __construct(protected int $minimum_log_level, protected array $blacklist, protected array $whitelist, protected EntityManagerInterface $em, protected Security $security, protected ConsoleInfoHelper $console_info_helper)
|
||||
protected LogLevel $minimum_log_level;
|
||||
|
||||
public function __construct(int $minimum_log_level, protected array $blacklist, protected array $whitelist, protected EntityManagerInterface $em, protected Security $security, protected ConsoleInfoHelper $console_info_helper)
|
||||
{
|
||||
$this->minimum_log_level = LogLevel::tryFrom($minimum_log_level);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,7 +112,7 @@ class EventLogger
|
|||
|
||||
public function shouldBeAdded(
|
||||
AbstractLogEntry $logEntry,
|
||||
?int $minimum_log_level = null,
|
||||
?LogLevel $minimum_log_level = null,
|
||||
?array $blacklist = null,
|
||||
?array $whitelist = null
|
||||
): bool {
|
||||
|
@ -118,7 +122,7 @@ class EventLogger
|
|||
$whitelist ??= $this->whitelist;
|
||||
|
||||
//Don't add the entry if it does not reach the minimum level
|
||||
if ($logEntry->getLevel() > $minimum_log_level) {
|
||||
if ($logEntry->getLevel()->lessImportThan($minimum_log_level)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,10 @@
|
|||
{{ block('text_constraint_widget') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block enum_constraint_widget %}
|
||||
{{ block('text_constraint_widget') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block parameter_constraint_widget %}
|
||||
{% import 'components/collection_type.macro.html.twig' as collection %}
|
||||
<tr {{ stimulus_controller('pages/parameters_autocomplete', {"url": path('typeahead_parameters', {"query": "__QUERY__", "type": "part"}) }) }} >
|
||||
|
|
|
@ -60,22 +60,6 @@ use PHPUnit\Framework\TestCase;
|
|||
|
||||
class AbstractLogEntryTest extends TestCase
|
||||
{
|
||||
public function levelDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[0, 'emergency'],
|
||||
[1, 'alert'],
|
||||
[2, 'critical'],
|
||||
[3, 'error'],
|
||||
[4, 'warning'],
|
||||
[5, 'notice'],
|
||||
[6, 'info'],
|
||||
[7, 'debug'],
|
||||
[8, 'blabla', true],
|
||||
[-1, 'test', true],
|
||||
];
|
||||
}
|
||||
|
||||
public function targetTypeDataProvider(): array
|
||||
{
|
||||
return [
|
||||
|
@ -95,28 +79,6 @@ class AbstractLogEntryTest extends TestCase
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider levelDataProvider
|
||||
*/
|
||||
public function testLevelIntToString(int $int, string $expected_string, bool $expect_exception = false): void
|
||||
{
|
||||
if ($expect_exception) {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
}
|
||||
$this->assertSame($expected_string, AbstractLogEntry::levelIntToString($int));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider levelDataProvider
|
||||
*/
|
||||
public function testLevelStringToInt(int $expected_int, string $string, bool $expect_exception = false): void
|
||||
{
|
||||
if ($expect_exception) {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
}
|
||||
$this->assertSame($expected_int, AbstractLogEntry::levelStringToInt($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider targetTypeDataProvider
|
||||
*/
|
||||
|
|
84
tests/Entity/LogSystem/LogLevelTest.php
Normal file
84
tests/Entity/LogSystem/LogLevelTest.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?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\Tests\Entity\LogSystem;
|
||||
|
||||
use App\Entity\LogSystem\LogLevel;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LogLevelTest extends TestCase
|
||||
{
|
||||
|
||||
public function testToPSR3LevelString(): void
|
||||
{
|
||||
$this->assertSame('debug', LogLevel::DEBUG->toPSR3LevelString());
|
||||
$this->assertSame('info', LogLevel::INFO->toPSR3LevelString());
|
||||
$this->assertSame('notice', LogLevel::NOTICE->toPSR3LevelString());
|
||||
$this->assertSame('warning', LogLevel::WARNING->toPSR3LevelString());
|
||||
$this->assertSame('error', LogLevel::ERROR->toPSR3LevelString());
|
||||
$this->assertSame('critical', LogLevel::CRITICAL->toPSR3LevelString());
|
||||
$this->assertSame('alert', LogLevel::ALERT->toPSR3LevelString());
|
||||
$this->assertSame('emergency', LogLevel::EMERGENCY->toPSR3LevelString());
|
||||
}
|
||||
|
||||
public function testFromPSR3LevelString(): void
|
||||
{
|
||||
$this->assertSame(LogLevel::DEBUG, LogLevel::fromPSR3LevelString('debug'));
|
||||
$this->assertSame(LogLevel::INFO, LogLevel::fromPSR3LevelString('info'));
|
||||
$this->assertSame(LogLevel::NOTICE, LogLevel::fromPSR3LevelString('notice'));
|
||||
$this->assertSame(LogLevel::WARNING, LogLevel::fromPSR3LevelString('warning'));
|
||||
$this->assertSame(LogLevel::ERROR, LogLevel::fromPSR3LevelString('error'));
|
||||
$this->assertSame(LogLevel::CRITICAL, LogLevel::fromPSR3LevelString('critical'));
|
||||
$this->assertSame(LogLevel::ALERT, LogLevel::fromPSR3LevelString('alert'));
|
||||
$this->assertSame(LogLevel::EMERGENCY, LogLevel::fromPSR3LevelString('emergency'));
|
||||
}
|
||||
|
||||
public function testMoreImportOrEqualThan(): void
|
||||
{
|
||||
$this->assertTrue(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::DEBUG));
|
||||
$this->assertFalse(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::INFO));
|
||||
$this->assertFalse(LogLevel::DEBUG->moreImportOrEqualThan(LogLevel::NOTICE));
|
||||
$this->assertTrue(LogLevel::EMERGENCY->moreImportOrEqualThan(LogLevel::DEBUG));
|
||||
}
|
||||
|
||||
public function testMoreImportThan(): void
|
||||
{
|
||||
$this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::DEBUG));
|
||||
$this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::INFO));
|
||||
$this->assertFalse(LogLevel::DEBUG->moreImportThan(LogLevel::NOTICE));
|
||||
$this->assertTrue(LogLevel::EMERGENCY->moreImportThan(LogLevel::DEBUG));
|
||||
}
|
||||
|
||||
public function testLessImportThan(): void
|
||||
{
|
||||
$this->assertFalse(LogLevel::DEBUG->lessImportThan(LogLevel::DEBUG));
|
||||
$this->assertTrue(LogLevel::DEBUG->lessImportThan(LogLevel::INFO));
|
||||
$this->assertTrue(LogLevel::DEBUG->lessImportThan(LogLevel::NOTICE));
|
||||
$this->assertFalse(LogLevel::EMERGENCY->lessImportThan(LogLevel::DEBUG));
|
||||
}
|
||||
|
||||
public function testLessImportOrEqualThan(): void
|
||||
{
|
||||
$this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::DEBUG));
|
||||
$this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::INFO));
|
||||
$this->assertTrue(LogLevel::DEBUG->lessImportOrEqualThan(LogLevel::NOTICE));
|
||||
$this->assertFalse(LogLevel::EMERGENCY->lessImportOrEqualThan(LogLevel::DEBUG));
|
||||
}
|
||||
}
|
|
@ -42,6 +42,7 @@ declare(strict_types=1);
|
|||
namespace App\Tests\Services\LogSystem;
|
||||
|
||||
use App\Entity\LogSystem\AbstractLogEntry;
|
||||
use App\Entity\LogSystem\LogLevel;
|
||||
use App\Entity\LogSystem\UserLoginLogEntry;
|
||||
use App\Entity\LogSystem\UserLogoutLogEntry;
|
||||
use App\Services\LogSystem\EventLogger;
|
||||
|
@ -67,21 +68,21 @@ class EventLoggerTest extends WebTestCase
|
|||
{
|
||||
$event1 = new UserLoginLogEntry('127.0.0.1');
|
||||
$event2 = new UserLogoutLogEntry('127.0.0.1');
|
||||
$event2->setLevel(AbstractLogEntry::LEVEL_CRITICAL);
|
||||
$event2->setLevel(LogLevel::CRITICAL);
|
||||
|
||||
//Test without restrictions
|
||||
$this->assertTrue($this->service->shouldBeAdded($event1, 7, [], []));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [], []));
|
||||
|
||||
//Test minimum log level
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, 2, [], []));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, 2, [], []));
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::CRITICAL, [], []));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::CRITICAL, [], []));
|
||||
|
||||
//Test blacklist
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, 7, [UserLoginLogEntry::class], []));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, 7, [UserLoginLogEntry::class], []));
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [UserLoginLogEntry::class], []));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::DEBUG, [UserLoginLogEntry::class], []));
|
||||
|
||||
//Test whitelist
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, 7, [], [UserLogoutLogEntry::class]));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, 7, [], [UserLogoutLogEntry::class]));
|
||||
$this->assertFalse($this->service->shouldBeAdded($event1, LogLevel::DEBUG, [], [UserLogoutLogEntry::class]));
|
||||
$this->assertTrue($this->service->shouldBeAdded($event2, LogLevel::DEBUG, [], [UserLogoutLogEntry::class]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue