friendica/src/Core/Logger/Type/StreamLogger.php

212 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* 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/>.
*
*/
2021-10-23 12:22:27 +02:00
namespace Friendica\Core\Logger\Type;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hooks\Capabilities\IAmAStrategy;
2023-01-17 20:11:48 +01:00
use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
2021-10-23 12:22:27 +02:00
use Friendica\Core\Logger\Exception\LoggerArgumentException;
use Friendica\Core\Logger\Exception\LoggerException;
2021-10-29 08:03:59 +02:00
use Friendica\Core\Logger\Exception\LogLevelException;
2019-03-03 20:32:27 +01:00
use Friendica\Util\DateTimeFormat;
use Friendica\Util\FileSystem;
2019-03-03 20:32:27 +01:00
use Psr\Log\LogLevel;
/**
2019-02-28 09:48:55 +01:00
* A Logger instance for logging into a stream (file, stdout, stderr)
*/
class StreamLogger extends AbstractLogger implements IAmAStrategy
{
2019-02-28 09:48:55 +01:00
/**
* The minimum loglevel at which this logger will be triggered
* @var string
*/
private $logLevel;
2019-03-03 20:32:27 +01:00
/**
* The file URL of the stream (if needed)
* @var string
*/
private $url;
/**
* The stream, where the current logger is writing into
* @var resource
*/
private $stream;
/**
* The current process ID
* @var int
*/
private $pid;
2019-03-03 21:25:18 +01:00
/**
* @var FileSystem
2019-03-03 21:25:18 +01:00
*/
private $fileSystem;
2019-03-03 21:25:18 +01:00
2019-03-03 20:32:27 +01:00
/**
* Translates LogLevel log levels to integer values
* @var array
*/
private $levelToInt = [
LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1,
LogLevel::CRITICAL => 2,
LogLevel::ERROR => 3,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 5,
LogLevel::INFO => 6,
LogLevel::DEBUG => 7,
];
/**
* {@inheritdoc}
* @param string $level The minimum loglevel at which this logger will be triggered
*
2021-10-23 12:22:27 +02:00
* @throws LoggerArgumentException
2021-10-29 08:03:59 +02:00
* @throws LogLevelException
2019-03-03 20:32:27 +01:00
*/
2023-01-17 20:11:48 +01:00
public function __construct(string $channel, IManageConfigValues $config, IHaveCallIntrospections $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
2019-03-03 20:32:27 +01:00
{
$this->fileSystem = $fileSystem;
$stream = $this->logfile ?? $config->get('system', 'logfile');
if ((file_exists($stream) && !is_writable($stream)) || is_writable(basename($stream))) {
throw new LoggerArgumentException(sprintf('%s is not a valid logfile', $stream));
}
2019-03-03 20:32:27 +01:00
parent::__construct($channel, $introspection);
if (is_resource($stream)) {
$this->stream = $stream;
} elseif (is_string($stream)) {
$this->url = $stream;
} else {
2021-10-23 12:22:27 +02:00
throw new LoggerArgumentException('A stream must either be a resource or a string.');
2019-03-03 20:32:27 +01:00
}
$this->pid = getmypid();
if (array_key_exists($level, $this->levelToInt)) {
$this->logLevel = $this->levelToInt[$level];
} else {
2021-10-29 08:03:59 +02:00
throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
2019-03-03 20:32:27 +01:00
}
$this->checkStream();
2019-03-03 20:32:27 +01:00
}
public function close()
{
2019-03-03 20:32:27 +01:00
if ($this->url && is_resource($this->stream)) {
fclose($this->stream);
}
$this->stream = null;
}
/**
2019-02-28 09:41:31 +01:00
* Adds a new entry to the log
*
2021-10-23 12:22:27 +02:00
* @param mixed $level
* @param string $message
2021-10-23 12:22:27 +02:00
* @param array $context
*
2019-02-28 09:41:31 +01:00
* @return void
2021-10-23 12:22:27 +02:00
*
* @throws LoggerException
2021-10-29 08:03:59 +02:00
* @throws LogLevelException
*/
2021-10-23 12:22:27 +02:00
protected function addEntry($level, string $message, array $context = [])
{
2019-03-03 20:32:27 +01:00
if (!array_key_exists($level, $this->levelToInt)) {
2021-10-29 08:03:59 +02:00
throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
2019-03-03 20:32:27 +01:00
}
$logLevel = $this->levelToInt[$level];
if ($logLevel > $this->logLevel) {
return;
}
$this->checkStream();
$formattedLog = $this->formatLog($level, $message, $context);
fwrite($this->stream, $formattedLog);
}
/**
* Formats a log record for the syslog output
*
2021-10-23 12:22:27 +02:00
* @param mixed $level The loglevel/priority
2019-03-03 20:32:27 +01:00
* @param string $message The message
* @param array $context The context of this call
*
* @return string the formatted syslog output
2021-10-23 12:22:27 +02:00
*
* @throws LoggerException
2019-03-03 20:32:27 +01:00
*/
2021-10-23 12:22:27 +02:00
private function formatLog($level, string $message, array $context = []): string
2019-03-03 20:32:27 +01:00
{
$record = $this->introspection->getRecord();
$record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
2021-10-23 12:22:27 +02:00
try {
$logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
} catch (\Exception $exception) {
throw new LoggerException('Cannot get current datetime.', $exception);
}
2019-03-03 20:32:27 +01:00
$logMessage .= $this->channel . ' ';
$logMessage .= '[' . strtoupper($level) . ']: ';
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
2021-03-28 21:50:32 +02:00
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
$logMessage .= $this->jsonEncodeArray($record);
2019-03-03 20:32:27 +01:00
$logMessage .= PHP_EOL;
return $logMessage;
}
2021-10-23 12:22:27 +02:00
/**
* Checks the current stream
*
* @throws LoggerException
* @throws LoggerArgumentException
*/
2019-03-03 20:32:27 +01:00
private function checkStream()
{
if (is_resource($this->stream)) {
return;
}
if (empty($this->url)) {
2021-10-23 12:22:27 +02:00
throw new LoggerArgumentException('Missing stream URL.');
2019-03-03 20:32:27 +01:00
}
2021-10-23 12:22:27 +02:00
try {
$this->stream = $this->fileSystem->createStream($this->url);
} catch (\UnexpectedValueException $exception) {
throw new LoggerException('Cannot create stream.', $exception);
}
2019-03-03 21:25:18 +01:00
}
}