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

153 lines
3.9 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;
2023-07-23 03:21:41 +02:00
use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
2021-10-23 12:22:27 +02:00
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 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
{
2023-07-26 23:02:09 +02:00
const NAME = 'stream';
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 stream, where the current logger is writing into
* @var resource
*/
private $stream;
/**
* The current process ID
* @var int
*/
private $pid;
/**
* Translates LogLevel log levels to integer values
* @var array
*/
public const levelToInt = [
2019-03-03 20:32:27 +01:00
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
*
* @throws LoggerException
2019-03-03 20:32:27 +01:00
*/
public function __construct(string $channel, IHaveCallIntrospections $introspection, $stream, int $logLevel, int $pid)
2019-03-03 20:32:27 +01:00
{
parent::__construct($channel, $introspection);
$this->stream = $stream;
$this->pid = $pid;
$this->logLevel = $logLevel;
2019-03-03 20:32:27 +01:00
}
public function close()
{
if (is_resource($this->stream)) {
2019-03-03 20:32:27 +01:00
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 = [])
{
if (!array_key_exists($level, static::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 = static::levelToInt[$level];
2019-03-03 20:32:27 +01:00
if ($logLevel > $this->logLevel) {
return;
}
$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;
}
}