Elgg  Version 4.x
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Cli\Application as CliApplication;
19 
25 class Logger extends \Monolog\Logger {
26 
27  const CHANNEL = 'ELGG';
28 
29  const OFF = 600; // use highest log level for OFF
30 
35  protected static $elgg_levels = [
36  0 => false,
37  100 => LogLevel::DEBUG,
38  200 => LogLevel::INFO,
39  250 => LogLevel::NOTICE,
40  300 => LogLevel::WARNING,
41  400 => LogLevel::ERROR,
42  500 => LogLevel::CRITICAL,
43  550 => LogLevel::ALERT,
44  600 => LogLevel::EMERGENCY,
45  ];
46 
51  protected static $legacy_levels = [
52  'OFF' => false,
53  'INFO' => LogLevel::INFO,
54  'NOTICE' => LogLevel::NOTICE,
55  'WARNING' => LogLevel::WARNING,
56  'ERROR' => LogLevel::ERROR,
57  ];
58 
62  protected $level;
63 
67  protected $disabled_stack = [];
68 
77  public static function factory(InputInterface $input = null, OutputInterface $output = null) {
78  $logger = new static(self::CHANNEL);
79 
80  if (\Elgg\Application::isCli()) {
81  if (is_null($input) || is_null($output)) {
84 
85  $app = new CliApplication();
86  $app->setup($input, $output);
87  }
88 
89  $handler = new ErrorHandler(
90  $output,
91  \Elgg\Application::getStdErr(),
92  true
93  );
94 
95  $formatter = new ErrorFormatter();
96  $formatter->allowInlineLineBreaks();
97  $formatter->ignoreEmptyContextAndExtra();
98 
99  $handler->setFormatter($formatter);
100 
101  $handler->pushProcessor(new BacktraceProcessor(self::ERROR));
102  } else {
103  $handler = new ErrorLogHandler();
104 
105  $handler->pushProcessor(new WebProcessor());
106 
107  $formatter = new ElggLogFormatter();
108  $formatter->allowInlineLineBreaks();
109  $formatter->ignoreEmptyContextAndExtra();
110 
111  $handler->setFormatter($formatter);
112 
113  $handler->pushProcessor(new MemoryUsageProcessor());
114  $handler->pushProcessor(new MemoryPeakUsageProcessor());
115  $handler->pushProcessor(new ProcessIdProcessor());
116  $handler->pushProcessor(new BacktraceProcessor(self::WARNING));
117  }
118 
119  $handler->pushProcessor(new PsrLogMessageProcessor());
120 
121  $logger->pushHandler($handler);
122 
123  $logger->setLevel();
124 
125  return $logger;
126  }
127 
135  protected function normalizeLevel($level = null) {
136  if (!$level) {
137  return false;
138  }
139 
140  if (array_key_exists($level, self::$legacy_levels)) {
141  $level = self::$legacy_levels[$level];
142  if ($level === false) {
143  // can't array_key_exists for false
144  return 0;
145  }
146  }
147 
148  if (array_key_exists($level, self::$elgg_levels)) {
149  $level = self::$elgg_levels[$level];
150  }
151 
152  if (!in_array($level, self::$elgg_levels)) {
153  $level = false;
154  }
155 
156  return $level;
157  }
158 
167  public function setLevel($level = null) {
168  if (!isset($level)) {
169  $php_error_level = error_reporting();
170 
171  $level = false;
172 
173  if (($php_error_level & E_NOTICE) == E_NOTICE) {
174  $level = LogLevel::NOTICE;
175  } else if (($php_error_level & E_WARNING) == E_WARNING) {
176  $level = LogLevel::WARNING;
177  } else if (($php_error_level & E_ERROR) == E_ERROR) {
178  $level = LogLevel::ERROR;
179  }
180  }
181 
182  $this->level = $this->normalizeLevel($level);
183  }
184 
193  public function getLevel($severity = true) {
194  if ($severity) {
195  return array_search($this->level, self::$elgg_levels);
196  }
197 
198  return $this->level;
199  }
200 
208  public function isLoggable($level) {
209  $level = $this->normalizeLevel($level);
210 
211  $severity = array_search($level, self::$elgg_levels);
212  if (!$this->getLevel() || $severity < $this->getLevel()) {
213  return false;
214  }
215 
216  return true;
217  }
218 
222  public function log($level, $message, array $context = []): void {
223 
224  $level = $this->normalizeLevel($level);
225 
226  if (!empty($this->disabled_stack)) {
227  // capture to top of stack
228  end($this->disabled_stack);
229  $key = key($this->disabled_stack);
230  $this->disabled_stack[$key][] = [
231  'message' => $message,
232  'level' => $level,
233  ];
234  }
235 
236  if (!$this->isLoggable($level)) {
237  return;
238  }
239 
240  // when capturing, still use consistent return value
241  if (!empty($this->disabled_stack)) {
242  return;
243  }
244 
245  parent::log($level, $message, $context);
246  }
247 
251  public function emergency($message, array $context = []): void {
252  $this->log(LogLevel::EMERGENCY, $message, $context);
253  }
254 
258  public function alert($message, array $context = []): void {
259  $this->log(LogLevel::ALERT, $message, $context);
260  }
261 
265  public function critical($message, array $context = []): void {
266  $this->log(LogLevel::CRITICAL, $message, $context);
267  }
268 
272  public function error($message, array $context = []): void {
273  $this->log(LogLevel::ERROR, $message, $context);
274  }
275 
279  public function warning($message, array $context = []): void {
280  $this->log(LogLevel::WARNING, $message, $context);
281  }
282 
286  public function notice($message, array $context = []): void {
287  $this->log(LogLevel::NOTICE, $message, $context);
288  }
289 
293  public function info($message, array $context = []): void {
294  $this->log(LogLevel::INFO, $message, $context);
295  }
296 
300  public function debug($message, array $context = []): void {
301  $this->log(LogLevel::DEBUG, $message, $context);
302  }
303 
311  public function dump($data) {
312  $this->log(LogLevel::ERROR, $data);
313  }
314 
327  public function disable() {
328  $this->disabled_stack[] = [];
329  }
330 
338  public function enable() {
339  return array_pop($this->disabled_stack);
340  }
341 }
disable()
Temporarily disable logging and capture logs (before tests)
Definition: Logger.php:327
$context
Definition: add.php:8
Custom log formatter.
info($message, array $context=[])
{}
Definition: Logger.php:293
getLevel($severity=true)
Get the current logging level severity.
Definition: Logger.php:193
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
emergency($message, array $context=[])
{}
Definition: Logger.php:251
static getStdOut()
Load console output interface.
static factory(InputInterface $input=null, OutputInterface $output=null)
Build a new logger.
Definition: Logger.php:77
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
alert($message, array $context=[])
{}
Definition: Logger.php:258
Wrapper for console application.
Definition: Application.php:11
debug($message, array $context=[])
{}
Definition: Logger.php:300
Inject backtrace stack into the record.
setLevel($level=null)
Set the logging level.
Definition: Logger.php:167
warning($message, array $context=[])
{}
Definition: Logger.php:279
log($level, $message, array $context=[])
{}
Definition: Logger.php:222
Logger.
Definition: Logger.php:25
enable()
Restore logging and get record of log calls (after tests)
Definition: Logger.php:338
error($message, array $context=[])
{}
Definition: Logger.php:272
Handle system and PHP errors.
critical($message, array $context=[])
{}
Definition: Logger.php:265
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
static getStdIn()
Load console input interface.
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
$input
Form field view.
Definition: field.php:13
dump($data)
Dump data to log.
Definition: Logger.php:311
$handler
Definition: add.php:7
isLoggable($level)
Check if a level is loggable under current logging level.
Definition: Logger.php:208
Format errors for console output.
normalizeLevel($level=null)
Normalizes legacy string or numeric representation of the level to LogLevel strings.
Definition: Logger.php:135
$output
Definition: download.php:9
notice($message, array $context=[])
{}
Definition: Logger.php:286