Initial commit
This commit is contained in:
130
application/Espo/Core/Log/DefaultFormatter.php
Normal file
130
application/Espo/Core/Log/DefaultFormatter.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Core\Exceptions\HasBody;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\LogRecord;
|
||||
use Throwable;
|
||||
|
||||
class DefaultFormatter extends LineFormatter
|
||||
{
|
||||
private const LINE_FORMAT = "[%datetime%] %level_name%: %code% %message% %request% %exception%\n";
|
||||
private const DATE_FORMAT = 'Y-m-d H:i:s';
|
||||
|
||||
public function __construct(
|
||||
private bool $includeTraces = false,
|
||||
) {
|
||||
parent::__construct(
|
||||
format: self::LINE_FORMAT,
|
||||
dateFormat: self::DATE_FORMAT,
|
||||
ignoreEmptyContextAndExtra: true,
|
||||
includeStacktraces: $this->includeTraces,
|
||||
);
|
||||
}
|
||||
|
||||
public function format(LogRecord $record): string
|
||||
{
|
||||
$line = parent::format($record);
|
||||
|
||||
$line = $this->interpolate($record, $line);
|
||||
$line = $this->addCode($record, $line);
|
||||
$line = $this->addRequest($record, $line);
|
||||
$line = $this->addException($record, $line);
|
||||
|
||||
return trim($line) . "\n";
|
||||
}
|
||||
|
||||
private function addCode(LogRecord $record, string $line): string
|
||||
{
|
||||
$exception = $record->context['exception'] ?? null;
|
||||
|
||||
if (!$exception instanceof Throwable) {
|
||||
return str_replace('%code% ', '', $line);
|
||||
}
|
||||
|
||||
$codePart = "({$exception->getCode()})";
|
||||
|
||||
return str_replace('%code% ', $codePart . ' ', $line);
|
||||
}
|
||||
|
||||
private function addException(LogRecord $record, string $line): string
|
||||
{
|
||||
$exception = $record->context['exception'] ?? null;
|
||||
|
||||
if (!$exception instanceof Throwable) {
|
||||
return str_replace('%exception%', '', $line);
|
||||
}
|
||||
|
||||
if (!$exception instanceof HasBody || !$exception->getBody()) {
|
||||
$part = ":: {$exception->getFile()}({$exception->getLine()})";
|
||||
|
||||
$line = str_replace('%exception%', $part, $line);
|
||||
} else {
|
||||
$line = str_replace('%exception%', '', $line);
|
||||
}
|
||||
|
||||
if (!$this->includeTraces) {
|
||||
return $line;
|
||||
}
|
||||
|
||||
$line .= $this->normalizeException($exception);
|
||||
|
||||
return $line;
|
||||
}
|
||||
|
||||
private function addRequest(LogRecord $record, string $line): string
|
||||
{
|
||||
$request = $record->context['request'] ?? null;
|
||||
|
||||
if (!$request instanceof Request) {
|
||||
return str_replace('%request% ', '', $line);
|
||||
}
|
||||
|
||||
$requestPart = ":: {$request->getMethod()} {$request->getResourcePath()}";
|
||||
|
||||
return str_replace('%request%', $requestPart, $line);
|
||||
}
|
||||
|
||||
private function interpolate(LogRecord $record, mixed $line): string
|
||||
{
|
||||
$replace = [];
|
||||
|
||||
foreach ($record->context as $key => $val) {
|
||||
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
|
||||
$replace['{' . $key . '}'] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
$line = strtr($line, $replace);
|
||||
return $line;
|
||||
}
|
||||
}
|
||||
143
application/Espo/Core/Log/DefaultHandlerLoader.php
Normal file
143
application/Espo/Core/Log/DefaultHandlerLoader.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Handler\FormattableHandlerInterface;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\Logger;
|
||||
|
||||
use ReflectionClass;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @phpstan-type DefaultHandlerLoaderData array{
|
||||
* className?: ?class-string<HandlerInterface>,
|
||||
* params?: ?array<string, mixed>,
|
||||
* level?: string|null,
|
||||
* formatter?: ?FormatterData,
|
||||
* }
|
||||
* @phpstan-type FormatterData array{
|
||||
* className?: ?class-string<FormatterInterface>,
|
||||
* params?: ?array<string, mixed>,
|
||||
* }
|
||||
*/
|
||||
class DefaultHandlerLoader
|
||||
{
|
||||
/**
|
||||
* @param DefaultHandlerLoaderData $data
|
||||
*/
|
||||
public function load(array $data, ?string $defaultLevel = null): HandlerInterface
|
||||
{
|
||||
$params = $data['params'] ?? [];
|
||||
$level = $data['level'] ?? $defaultLevel;
|
||||
|
||||
if ($level) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$params['level'] = Logger::toMonologLevel($level);
|
||||
}
|
||||
|
||||
$className = $data['className'] ?? null;
|
||||
|
||||
if (!$className) {
|
||||
throw new RuntimeException("Log handler does not have className specified.");
|
||||
}
|
||||
|
||||
$handler = $this->createInstance($className, $params);
|
||||
|
||||
$formatter = $this->loadFormatter($data);
|
||||
|
||||
if ($formatter && $handler instanceof FormattableHandlerInterface) {
|
||||
$handler->setFormatter($formatter);
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param DefaultHandlerLoaderData $data
|
||||
*/
|
||||
public function loadFormatter(array $data): ?FormatterInterface
|
||||
{
|
||||
$formatterData = $data['formatter'] ?? null;
|
||||
|
||||
if (!$formatterData || !is_array($formatterData)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$className = $formatterData['className'] ?? null;
|
||||
|
||||
if (!$className) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$params = $formatterData['params'] ?? [];
|
||||
|
||||
return $this->createInstance($className, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of object
|
||||
* @param class-string<T> $className
|
||||
* @param array<string, mixed> $params
|
||||
* @return T
|
||||
*/
|
||||
private function createInstance(string $className, array $params): object
|
||||
{
|
||||
$class = new ReflectionClass($className);
|
||||
|
||||
$constructor = $class->getConstructor();
|
||||
|
||||
if (!$constructor) {
|
||||
return $class->newInstanceArgs([]);
|
||||
}
|
||||
|
||||
$argumentList = [];
|
||||
|
||||
foreach ($constructor->getParameters() as $parameter) {
|
||||
$name = $parameter->getName();
|
||||
|
||||
if (array_key_exists($name, $params)) {
|
||||
$value = $params[$name];
|
||||
} else if ($parameter->isDefaultValueAvailable()) {
|
||||
$value = $parameter->getDefaultValue();
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
$argumentList[] = $value;
|
||||
}
|
||||
|
||||
return $class->newInstanceArgs($argumentList);
|
||||
}
|
||||
}
|
||||
53
application/Espo/Core/Log/EspoRotatingFileHandlerLoader.php
Normal file
53
application/Espo/Core/Log/EspoRotatingFileHandlerLoader.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Espo\Core\Log\Handler\EspoRotatingFileHandler;
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\Level;
|
||||
use Monolog\Logger;
|
||||
|
||||
class EspoRotatingFileHandlerLoader implements HandlerLoader
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Config $config
|
||||
) {}
|
||||
|
||||
public function load(array $params): HandlerInterface
|
||||
{
|
||||
$filename = $params['filename'] ?? 'data/logs/espo.log';
|
||||
$levelCode = $params['level'] ?? Level::Notice->value;
|
||||
$level = Logger::toMonologLevel($levelCode);
|
||||
|
||||
return new EspoRotatingFileHandler($this->config, $filename, 0, $level);
|
||||
}
|
||||
}
|
||||
142
application/Espo/Core/Log/Handler/DatabaseHandler.php
Normal file
142
application/Espo/Core/Log/Handler/DatabaseHandler.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log\Handler;
|
||||
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Core\ApplicationState;
|
||||
use Espo\Core\ORM\EntityManagerProxy;
|
||||
use Espo\Entities\AppLogRecord;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\Level;
|
||||
use Monolog\LogRecord;
|
||||
use Throwable;
|
||||
|
||||
class DatabaseHandler implements HandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Level $level,
|
||||
private EntityManagerProxy $entityManager,
|
||||
private ApplicationState $applicationState
|
||||
) {}
|
||||
|
||||
public function isHandling(LogRecord $record): bool
|
||||
{
|
||||
if (!$this->applicationState->hasUser()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($record->context['isSql'] ?? false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $record->level->value >= $this->level->value;
|
||||
}
|
||||
|
||||
public function handle(LogRecord $record): bool
|
||||
{
|
||||
if (!$this->isHandling($record)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$logRecord = $this->entityManager->getRDBRepositoryByClass(AppLogRecord::class)->getNew();
|
||||
|
||||
$level = ucfirst($record->level->toPsrLogLevel());
|
||||
$message = $this->interpolate($record, $record->message);
|
||||
|
||||
$logRecord
|
||||
->setLevel($level)
|
||||
->setMessage($message);
|
||||
|
||||
$this->setException($record, $logRecord);
|
||||
$this->setRequest($record, $logRecord);
|
||||
|
||||
$this->entityManager->saveEntity($logRecord);
|
||||
} catch (Throwable) {
|
||||
// Nowhere to log.
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function interpolate(LogRecord $record, string $line): string
|
||||
{
|
||||
$replace = [];
|
||||
|
||||
foreach ($record->context as $key => $val) {
|
||||
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
|
||||
$replace['{' . $key . '}'] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
return strtr($line, $replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LogRecord[] $records
|
||||
*/
|
||||
public function handleBatch(array $records): void
|
||||
{
|
||||
foreach ($records as $record) {
|
||||
$this->handle($record);
|
||||
}
|
||||
}
|
||||
|
||||
public function close(): void
|
||||
{}
|
||||
|
||||
private function setException(LogRecord $record, AppLogRecord $logRecord): void
|
||||
{
|
||||
$exception = $record->context['exception'] ?? null;
|
||||
|
||||
if (!$exception instanceof Throwable) {
|
||||
return;
|
||||
}
|
||||
|
||||
$logRecord
|
||||
->setExceptionClass(get_class($exception))
|
||||
->setFile($exception->getFile())
|
||||
->setLine($exception->getLine())
|
||||
->setCode($exception->getCode());
|
||||
}
|
||||
|
||||
private function setRequest(LogRecord $record, AppLogRecord $logRecord): void
|
||||
{
|
||||
$request = $record->context['request'] ?? null;
|
||||
|
||||
if (!$request instanceof Request) {
|
||||
return;
|
||||
}
|
||||
|
||||
$logRecord
|
||||
->setRequestMethod($request->getMethod())
|
||||
->setRequestResourcePath($request->getResourcePath());
|
||||
}
|
||||
}
|
||||
103
application/Espo/Core/Log/Handler/EspoFileHandler.php
Normal file
103
application/Espo/Core/Log/Handler/EspoFileHandler.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log\Handler;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
|
||||
use Monolog\Handler\StreamHandler as MonologStreamHandler;
|
||||
use Monolog\Level;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class EspoFileHandler extends MonologStreamHandler
|
||||
{
|
||||
protected FileManager $fileManager;
|
||||
|
||||
protected int $maxErrorMessageLength = 10000;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
string $filename,
|
||||
Level $level = Level::Debug,
|
||||
bool $bubble = true
|
||||
) {
|
||||
parent::__construct($filename, $level, $bubble);
|
||||
|
||||
$defaultPermissions = $config->get('defaultPermissions');
|
||||
|
||||
$this->fileManager = new FileManager($defaultPermissions);
|
||||
}
|
||||
|
||||
protected function write(LogRecord $record): void
|
||||
{
|
||||
if (!$this->url) {
|
||||
throw new RuntimeException("Missing a logger file path. Check logger params in config.");
|
||||
}
|
||||
|
||||
try {
|
||||
if (!is_writable($this->url)) {
|
||||
$checkFileResult = $this->fileManager->checkCreateFile($this->url);
|
||||
|
||||
if (!$checkFileResult) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->fileManager->appendContents(
|
||||
$this->url,
|
||||
$this->pruneMessage($record)
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
$msg = "Could not write file `$this->url`.";
|
||||
|
||||
if ($e->getMessage()) {
|
||||
$msg .= " Error message: " . $e->getMessage();
|
||||
}
|
||||
|
||||
throw new RuntimeException($msg);
|
||||
}
|
||||
}
|
||||
|
||||
private function pruneMessage(LogRecord $record): string
|
||||
{
|
||||
if (strlen($record->message) <= $this->maxErrorMessageLength) {
|
||||
return $record->formatted;
|
||||
}
|
||||
|
||||
$message = substr($record->message, 0, $this->maxErrorMessageLength) . '...';
|
||||
|
||||
$record = $record->with(message: $message);
|
||||
|
||||
return $this->getFormatter()->format($record);
|
||||
}
|
||||
}
|
||||
113
application/Espo/Core/Log/Handler/EspoRotatingFileHandler.php
Normal file
113
application/Espo/Core/Log/Handler/EspoRotatingFileHandler.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log\Handler;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
use Monolog\Level;
|
||||
|
||||
class EspoRotatingFileHandler extends EspoFileHandler
|
||||
{
|
||||
protected string $dateFormat = 'Y-m-d';
|
||||
protected string $filenameFormat = '{filename}-{date}';
|
||||
protected string $filename;
|
||||
protected int $maxFiles;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
string $filename,
|
||||
int $maxFiles = 0,
|
||||
Level $level = Level::Debug,
|
||||
bool $bubble = true
|
||||
) {
|
||||
$this->filename = $filename;
|
||||
$this->maxFiles = $maxFiles;
|
||||
|
||||
parent::__construct($config, $this->getTimedFilename(), $level, $bubble);
|
||||
|
||||
$this->rotate();
|
||||
}
|
||||
|
||||
protected function rotate(): void
|
||||
{
|
||||
if (0 === $this->maxFiles) {
|
||||
return; // unlimited number of files for 0
|
||||
}
|
||||
|
||||
$filePattern = $this->getFilePattern();
|
||||
$dirPath = $this->fileManager->getDirName($this->filename);
|
||||
/** @var string[] $logFiles */
|
||||
$logFiles = $this->fileManager->getFileList($dirPath, false, $filePattern, true);
|
||||
|
||||
if (!empty($logFiles) && count($logFiles) > $this->maxFiles) {
|
||||
usort($logFiles, function ($a, $b) {
|
||||
return strcmp($b, $a);
|
||||
});
|
||||
|
||||
$logFilesToBeRemoved = array_slice($logFiles, $this->maxFiles);
|
||||
|
||||
$this->fileManager->removeFile($logFilesToBeRemoved, $dirPath);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTimedFilename(): string
|
||||
{
|
||||
$fileInfo = pathinfo($this->filename);
|
||||
|
||||
$timedFilename = str_replace(
|
||||
['{filename}', '{date}'],
|
||||
[$fileInfo['filename'], date($this->dateFormat)],
|
||||
($fileInfo['dirname'] ?? '') . '/' . $this->filenameFormat
|
||||
);
|
||||
|
||||
if (!empty($fileInfo['extension'])) {
|
||||
$timedFilename .= '.' . $fileInfo['extension'];
|
||||
}
|
||||
|
||||
return $timedFilename;
|
||||
}
|
||||
|
||||
protected function getFilePattern(): string
|
||||
{
|
||||
$fileInfo = pathinfo($this->filename);
|
||||
|
||||
$glob = str_replace(
|
||||
['{filename}', '{date}'],
|
||||
[$fileInfo['filename'], '.*'],
|
||||
$this->filenameFormat
|
||||
);
|
||||
|
||||
if (!empty($fileInfo['extension'])) {
|
||||
$glob .= '\.'.$fileInfo['extension'];
|
||||
}
|
||||
|
||||
return '^' . $glob . '$';
|
||||
}
|
||||
}
|
||||
90
application/Espo/Core/Log/HandlerListLoader.php
Normal file
90
application/Espo/Core/Log/HandlerListLoader.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Monolog\Handler\FormattableHandlerInterface;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
|
||||
use Espo\Core\InjectableFactory;
|
||||
|
||||
class HandlerListLoader
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InjectableFactory $injectableFactory,
|
||||
private readonly DefaultHandlerLoader $defaultLoader
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<array<string, mixed>> $dataList
|
||||
* @return HandlerInterface[]
|
||||
*/
|
||||
public function load(array $dataList, ?string $defaultLevel = null): array
|
||||
{
|
||||
$handlerList = [];
|
||||
|
||||
foreach ($dataList as $item) {
|
||||
$handler = $this->loadHandler($item, $defaultLevel);
|
||||
|
||||
$handlerList[] = $handler;
|
||||
}
|
||||
|
||||
return $handlerList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
private function loadHandler(array $data, ?string $defaultLevel = null): HandlerInterface
|
||||
{
|
||||
$params = $data['params'] ?? [];
|
||||
$params['level'] ??= $defaultLevel;
|
||||
|
||||
/** @var ?class-string<HandlerLoader> $loaderClassName */
|
||||
$loaderClassName = $data['loaderClassName'] ?? null;
|
||||
|
||||
if ($loaderClassName) {
|
||||
$loader = $this->injectableFactory->create($loaderClassName);
|
||||
|
||||
$handler = $loader->load($params);
|
||||
|
||||
if ($handler instanceof FormattableHandlerInterface) {
|
||||
$formatter = $this->defaultLoader->loadFormatter($data);
|
||||
|
||||
if ($formatter) {
|
||||
$handler->setFormatter($formatter);
|
||||
}
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
return $this->defaultLoader->load($data, $defaultLevel);
|
||||
}
|
||||
}
|
||||
40
application/Espo/Core/Log/HandlerLoader.php
Normal file
40
application/Espo/Core/Log/HandlerLoader.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
|
||||
interface HandlerLoader
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $params
|
||||
*/
|
||||
public function load(array $params): HandlerInterface;
|
||||
}
|
||||
130
application/Espo/Core/Log/LogLoader.php
Normal file
130
application/Espo/Core/Log/LogLoader.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Log;
|
||||
|
||||
use Espo\Core\Application\ApplicationParams;
|
||||
use Espo\Core\ApplicationState;
|
||||
use Espo\Core\Log\Handler\DatabaseHandler;
|
||||
use Espo\Core\Log\Handler\EspoFileHandler;
|
||||
use Espo\Core\Log\Handler\EspoRotatingFileHandler;
|
||||
use Espo\Core\ORM\EntityManagerProxy;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Log;
|
||||
|
||||
use Monolog\ErrorHandler as MonologErrorHandler;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\Logger;
|
||||
|
||||
class LogLoader
|
||||
{
|
||||
private const PATH = 'data/logs/espo.log';
|
||||
|
||||
private const MAX_FILE_NUMBER = 30;
|
||||
private const DEFAULT_LEVEL = 'WARNING';
|
||||
|
||||
public function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly HandlerListLoader $handlerListLoader,
|
||||
private readonly EntityManagerProxy $entityManagerProxy,
|
||||
private readonly ApplicationState $applicationState,
|
||||
private readonly ApplicationParams $applicationParams,
|
||||
) {}
|
||||
|
||||
public function load(): Log
|
||||
{
|
||||
$log = new Log('Espo');
|
||||
|
||||
$handlerDataList = $this->config->get('logger.handlerList') ?? null;
|
||||
|
||||
if ($handlerDataList) {
|
||||
$level = $this->config->get('logger.level');
|
||||
|
||||
$handlerList = $this->handlerListLoader->load($handlerDataList, $level);
|
||||
} else {
|
||||
$handlerList = [$this->createDefaultHandler()];
|
||||
}
|
||||
|
||||
if ($this->config->get('logger.databaseHandler')) {
|
||||
$handlerList[] = $this->createDatabaseHandler();
|
||||
}
|
||||
|
||||
foreach ($handlerList as $handler) {
|
||||
$log->pushHandler($handler);
|
||||
}
|
||||
|
||||
if (!$this->applicationParams->noErrorHandler) {
|
||||
$errorHandler = new MonologErrorHandler($log);
|
||||
|
||||
$errorHandler->registerExceptionHandler([], false);
|
||||
$errorHandler->registerErrorHandler([], false);
|
||||
}
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
private function createDefaultHandler(): HandlerInterface
|
||||
{
|
||||
$path = $this->config->get('logger.path') ?? self::PATH;
|
||||
$level = $this->config->get('logger.level') ?? self::DEFAULT_LEVEL;
|
||||
$rotation = $this->config->get('logger.rotation') ?? true;
|
||||
|
||||
$levelCode = Logger::toMonologLevel($level);
|
||||
|
||||
if ($rotation) {
|
||||
$maxFileNumber = $this->config->get('logger.maxFileNumber') ?? self::MAX_FILE_NUMBER;
|
||||
|
||||
$handler = new EspoRotatingFileHandler($this->config, $path, $maxFileNumber, $levelCode, true);
|
||||
} else {
|
||||
$handler = new EspoFileHandler($this->config, $path, $levelCode, true);
|
||||
}
|
||||
|
||||
$formatter = new DefaultFormatter($this->printTrace());
|
||||
|
||||
$handler->setFormatter($formatter);
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
private function printTrace(): bool
|
||||
{
|
||||
return (bool) $this->config->get('logger.printTrace');
|
||||
}
|
||||
|
||||
private function createDatabaseHandler(): HandlerInterface
|
||||
{
|
||||
$rawLevel = $this->config->get('logger.databaseHandlerLevel') ??
|
||||
$this->config->get('logger.level') ??
|
||||
self::DEFAULT_LEVEL;
|
||||
|
||||
$level = Logger::toMonologLevel($rawLevel);
|
||||
|
||||
return new DatabaseHandler($level, $this->entityManagerProxy, $this->applicationState);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user