3 namespace Friendica\Util\Logger;
5 use Friendica\Util\DateTimeFormat;
6 use Friendica\Util\Introspection;
10 * A Logger instance for logging into a stream (file, stdout, stderr)
12 class StreamLogger extends AbstractFriendicaLogger
15 * The minimum loglevel at which this logger will be triggered
21 * The file URL of the stream (if needed)
27 * The stream, where the current logger is writing into
33 * The current process ID
39 * Translates LogLevel log levels to integer values
42 private $levelToInt = [
43 LogLevel::EMERGENCY => 0,
45 LogLevel::CRITICAL => 2,
47 LogLevel::WARNING => 4,
48 LogLevel::NOTICE => 5,
55 * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
56 * @param string $level The minimum loglevel at which this logger will be triggered
60 public function __construct($channel, $stream, Introspection $introspection, $level = LogLevel::DEBUG)
62 parent::__construct($channel, $introspection);
64 if (is_resource($stream)) {
65 $this->stream = $stream;
66 } elseif (is_string($stream)) {
69 throw new \InvalidArgumentException('A stream must either be a resource or a string.');
72 $this->pid = getmypid();
73 if (array_key_exists($level, $this->levelToInt)) {
74 $this->logLevel = $this->levelToInt[$level];
76 throw new \InvalidArgumentException(sprintf('The level "%s" is not valid.', $level));
80 public function close()
82 if ($this->url && is_resource($this->stream)) {
83 fclose($this->stream);
90 * Adds a new entry to the log
93 * @param string $message
94 * @param array $context
98 protected function addEntry($level, $message, $context = [])
100 if (!array_key_exists($level, $this->levelToInt)) {
101 throw new \InvalidArgumentException('The level "%s" is not valid', $level);
104 $logLevel = $this->levelToInt[$level];
106 if ($logLevel > $this->logLevel) {
110 $this->checkStream();
112 $this->stream = fopen($this->url, 'a');
113 $formattedLog = $this->formatLog($level, $message, $context);
114 fwrite($this->stream, $formattedLog);
118 * Formats a log record for the syslog output
120 * @param int $level The loglevel/priority
121 * @param string $message The message
122 * @param array $context The context of this call
124 * @return string the formatted syslog output
126 private function formatLog($level, $message, $context = [])
128 $record = $this->introspection->getRecord();
129 $record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
132 $logMessage .= DateTimeFormat::localNow() . ' ';
133 $logMessage .= $this->channel . ' ';
134 $logMessage .= '[' . strtoupper($level) . ']: ';
135 $logMessage .= $this->psrInterpolate($message, $context) . ' ';
136 $logMessage .= @json_encode($context) . ' - ';
137 $logMessage .= @json_encode($record);
138 $logMessage .= PHP_EOL;
143 private function checkStream()
145 if (is_resource($this->stream)) {
149 if (empty($this->url)) {
150 throw new \LogicException('Missing stream URL.');
154 $this->stream = fopen($this->url, 'a');
156 if (!is_resource($this->stream)) {
157 $this->stream = null;
159 throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened.', $this->url));
163 private function createDir()
166 $pos = strpos($this->url, '://');
168 $dirname = dirname($this->url);
171 if (substr($this->url, 0, 7) === 'file://') {
172 $dirname = dirname(substr($this->url, 7));
175 if (isset($dirname) && !is_dir($dirname)) {
176 $status = mkdir($dirname, 0777, true);
177 if (!$status && !is_dir($dirname)) {
178 throw new \UnexpectedValueException(sprintf('Directory "%s" cannot get created.', $dirname));