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
45 * Translates LogLevel log levels to integer values
48 private $levelToInt = [
49 LogLevel::EMERGENCY => 0,
51 LogLevel::CRITICAL => 2,
53 LogLevel::WARNING => 4,
54 LogLevel::NOTICE => 5,
61 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
62 * @param string $level The minimum loglevel at which this logger will be triggered
66 public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, $level = LogLevel::DEBUG)
68 $this->fileSystem = $fileSystem;
70 parent::__construct($channel, $introspection);
72 if (is_resource($stream)) {
73 $this->stream = $stream;
74 } elseif (is_string($stream)) {
77 throw new \InvalidArgumentException('A stream must either be a resource or a string.');
80 $this->pid = getmypid();
81 if (array_key_exists($level, $this->levelToInt)) {
82 $this->logLevel = $this->levelToInt[$level];
84 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
90 public function close()
92 if ($this->url && is_resource($this->stream)) {
93 fclose($this->stream);
100 * Adds a new entry to the log
103 * @param string $message
104 * @param array $context
108 protected function addEntry($level, $message, $context = [])
110 if (!array_key_exists($level, $this->levelToInt)) {
111 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
114 $logLevel = $this->levelToInt[$level];
116 if ($logLevel > $this->logLevel) {
120 $this->checkStream();
122 $formattedLog = $this->formatLog($level, $message, $context);
123 fwrite($this->stream, $formattedLog);
127 * Formats a log record for the syslog output
129 * @param int $level The loglevel/priority
130 * @param string $message The message
131 * @param array $context The context of this call
133 * @return string the formatted syslog output
135 private function formatLog($level, $message, $context = [])
137 $record = $this->introspection->getRecord();
138 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
141 $logMessage .= DateTimeFormat::utcNow() . ' ';
142 $logMessage .= $this->channel . ' ';
143 $logMessage .= '[' . strtoupper($level) . ']: ';
144 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
145 $logMessage .= @json_encode($context) . ' - ';
146 $logMessage .= @json_encode($record);
147 $logMessage .= PHP_EOL;
152 private function checkStream()
154 if (is_resource($this->stream)) {
158 if (empty($this->url)) {
159 throw new \LogicException('Missing stream URL.');
162 $this->stream = $this->fileSystem->createStream($this->url);