]> git.mxchange.org Git - friendica.git/blob - src/Util/Logger/AbstractLogger.php
wrapping up 2019.12
[friendica.git] / src / Util / Logger / AbstractLogger.php
1 <?php
2
3 namespace Friendica\Util\Logger;
4
5 use Friendica\Util\Introspection;
6 use Friendica\Util\Strings;
7 use Psr\Log\LoggerInterface;
8 use Psr\Log\LogLevel;
9
10 /**
11  * This class contains all necessary dependencies and calls for Friendica
12  * Every new Logger should extend this class and define, how addEntry() works
13  *
14  * Additional information for each Logger, who extends this class:
15  * - Introspection
16  * - UID for each call
17  * - Channel of the current call (i.e. index, worker, daemon, ...)
18  */
19 abstract class AbstractLogger implements LoggerInterface
20 {
21         /**
22          * The output channel of this logger
23          * @var string
24          */
25         protected $channel;
26
27         /**
28          * The Introspection for the current call
29          * @var Introspection
30          */
31         protected $introspection;
32
33         /**
34          * The UID of the current call
35          * @var string
36          */
37         protected $logUid;
38
39         /**
40          * Adds a new entry to the log
41          *
42          * @param int    $level
43          * @param string $message
44          * @param array  $context
45          *
46          * @return void
47          */
48         abstract protected function addEntry($level, $message, $context = []);
49
50         /**
51          * @param string        $channel       The output channel
52          * @param Introspection $introspection The introspection of the current call
53          *
54          * @throws \Exception
55          */
56         public function __construct($channel, Introspection $introspection)
57         {
58                 $this->channel       = $channel;
59                 $this->introspection = $introspection;
60                 $this->logUid        = Strings::getRandomHex(6);
61         }
62
63         /**
64          * Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
65          * @see https://www.php-fig.org/psr/psr-3/#12-message
66          *
67          * @param string $message
68          * @param array  $context
69          *
70          * @return string the interpolated message
71          */
72         protected function psrInterpolate($message, array $context = array())
73         {
74                 $replace = [];
75                 foreach ($context as $key => $value) {
76                         // check that the value can be casted to string
77                         if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
78                                 $replace['{' . $key . '}'] = $value;
79                         } elseif (is_array($value)) {
80                                 $replace['{' . $key . '}'] = @json_encode($value);
81                         }
82                 }
83
84                 return strtr($message, $replace);
85         }
86
87         /**
88          * {@inheritdoc}
89          */
90         public function emergency($message, array $context = array())
91         {
92                 $this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
93         }
94
95         /**
96          * {@inheritdoc}
97          */
98         public function alert($message, array $context = array())
99         {
100                 $this->addEntry(LogLevel::ALERT, (string) $message, $context);
101         }
102
103         /**
104          * {@inheritdoc}
105          */
106         public function critical($message, array $context = array())
107         {
108                 $this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
109         }
110
111         /**
112          * {@inheritdoc}
113          */
114         public function error($message, array $context = array())
115         {
116                 $this->addEntry(LogLevel::ERROR, (string) $message, $context);
117         }
118
119         /**
120          * {@inheritdoc}
121          */
122         public function warning($message, array $context = array())
123         {
124                 $this->addEntry(LogLevel::WARNING, (string) $message, $context);
125         }
126
127         /**
128          * {@inheritdoc}
129          */
130         public function notice($message, array $context = array())
131         {
132                 $this->addEntry(LogLevel::NOTICE, (string) $message, $context);
133         }
134
135         /**
136          * {@inheritdoc}
137          */
138         public function info($message, array $context = array())
139         {
140                 $this->addEntry(LogLevel::INFO, (string) $message, $context);
141         }
142
143         /**
144          * {@inheritdoc}
145          */
146         public function debug($message, array $context = array())
147         {
148                 $this->addEntry(LogLevel::DEBUG, (string) $message, $context);
149         }
150
151         /**
152          * {@inheritdoc}
153          */
154         public function log($level, $message, array $context = array())
155         {
156                 $this->addEntry($level, (string) $message, $context);
157         }
158 }