3 namespace Friendica\Util\Logger;
5 use Friendica\Util\DateTimeFormat;
6 use Friendica\Util\FileSystem;
7 use Friendica\Util\Introspection;
11 * A Logger instance for logging into a stream (file, stdout, stderr)
13 class StreamLogger extends AbstractLogger
16 * The minimum loglevel at which this logger will be triggered
22 * The file URL of the stream (if needed)
28 * The stream, where the current logger is writing into
34 * The current process ID
46 * Translates LogLevel log levels to integer values
49 private $levelToInt = [
50 LogLevel::EMERGENCY => 0,
52 LogLevel::CRITICAL => 2,
54 LogLevel::WARNING => 4,
55 LogLevel::NOTICE => 5,
62 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
63 * @param string $level The minimum loglevel at which this logger will be triggered
67 public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, $level = LogLevel::DEBUG)
69 $this->fileSystem = $fileSystem;
71 parent::__construct($channel, $introspection);
73 if (is_resource($stream)) {
74 $this->stream = $stream;
75 } elseif (is_string($stream)) {
78 throw new \InvalidArgumentException('A stream must either be a resource or a string.');
81 $this->pid = getmypid();
82 if (array_key_exists($level, $this->levelToInt)) {
83 $this->logLevel = $this->levelToInt[$level];
85 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
91 public function close()
93 if ($this->url && is_resource($this->stream)) {
94 fclose($this->stream);
101 * Adds a new entry to the log
104 * @param string $message
105 * @param array $context
109 protected function addEntry($level, $message, $context = [])
111 if (!array_key_exists($level, $this->levelToInt)) {
112 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
115 $logLevel = $this->levelToInt[$level];
117 if ($logLevel > $this->logLevel) {
121 $this->checkStream();
123 $formattedLog = $this->formatLog($level, $message, $context);
124 fwrite($this->stream, $formattedLog);
128 * Formats a log record for the syslog output
130 * @param int $level The loglevel/priority
131 * @param string $message The message
132 * @param array $context The context of this call
134 * @return string the formatted syslog output
136 private function formatLog($level, $message, $context = [])
138 $record = $this->introspection->getRecord();
139 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
142 $logMessage .= DateTimeFormat::utcNow() . ' ';
143 $logMessage .= $this->channel . ' ';
144 $logMessage .= '[' . strtoupper($level) . ']: ';
145 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
146 $logMessage .= @json_encode($context) . ' - ';
147 $logMessage .= @json_encode($record);
148 $logMessage .= PHP_EOL;
153 private function checkStream()
155 if (is_resource($this->stream)) {
159 if (empty($this->url)) {
160 throw new \LogicException('Missing stream URL.');
163 $this->stream = $this->fileSystem->createStream($this->url);