Elgg  Version master
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Cli\Application as CliApplication;
11 use Monolog\Level;
20 
26 class Logger extends \Monolog\Logger {
27 
28  const CHANNEL = 'ELGG';
29 
30  const OFF = 600; // use highest log level for OFF
31 
36  protected static $elgg_levels = [
37  0 => false,
38  100 => LogLevel::DEBUG,
39  200 => LogLevel::INFO,
40  250 => LogLevel::NOTICE,
41  300 => LogLevel::WARNING,
42  400 => LogLevel::ERROR,
43  500 => LogLevel::CRITICAL,
44  550 => LogLevel::ALERT,
45  600 => LogLevel::EMERGENCY,
46  ];
47 
52  protected static $legacy_levels = [
53  'OFF' => false,
54  'INFO' => LogLevel::INFO,
55  'NOTICE' => LogLevel::NOTICE,
56  'WARNING' => LogLevel::WARNING,
57  'ERROR' => LogLevel::ERROR,
58  ];
59 
63  protected $level;
64 
68  protected $disabled_stack = [];
69 
78  public static function factory(InputInterface $input = null, OutputInterface $output = null) {
79  $logger = new static(self::CHANNEL);
80 
81  if (\Elgg\Application::isCli()) {
82  if (is_null($input) || is_null($output)) {
85 
86  $app = new CliApplication();
87  $app->setup($input, $output);
88  }
89 
90  $handler = new ErrorHandler(
91  $output,
92  \Elgg\Application::getStdErr(),
93  true
94  );
95 
96  $formatter = new ErrorFormatter();
97  $formatter->allowInlineLineBreaks();
98  $formatter->ignoreEmptyContextAndExtra();
99 
100  $handler->setFormatter($formatter);
101 
102  $handler->pushProcessor(new BacktraceProcessor(Level::Error));
103  } else {
104  $handler = new ErrorLogHandler();
105 
106  $handler->pushProcessor(new WebProcessor());
107 
108  $formatter = new ElggLogFormatter();
109  $formatter->allowInlineLineBreaks();
110  $formatter->ignoreEmptyContextAndExtra();
111 
112  $handler->setFormatter($formatter);
113 
114  $handler->pushProcessor(new MemoryUsageProcessor());
115  $handler->pushProcessor(new MemoryPeakUsageProcessor());
116  $handler->pushProcessor(new ProcessIdProcessor());
117  $handler->pushProcessor(new BacktraceProcessor(Level::Warning));
118  }
119 
120  $handler->pushProcessor(new PsrLogMessageProcessor());
121 
122  $logger->pushHandler($handler);
123 
124  $logger->setLevel();
125 
126  return $logger;
127  }
128 
136  protected function normalizeLevel($level = null) {
137  if (!$level) {
138  return false;
139  }
140 
141  if (array_key_exists($level, self::$legacy_levels)) {
142  $level = self::$legacy_levels[$level];
143  if ($level === false) {
144  // can't array_key_exists for false
145  return 0;
146  }
147  }
148 
149  if (array_key_exists($level, self::$elgg_levels)) {
150  $level = self::$elgg_levels[$level];
151  }
152 
153  if (!in_array($level, self::$elgg_levels)) {
154  $level = false;
155  }
156 
157  return $level;
158  }
159 
168  public function setLevel($level = null) {
169  if (!isset($level)) {
170  $php_error_level = error_reporting();
171 
172  $level = false;
173 
174  if (($php_error_level & E_NOTICE) == E_NOTICE) {
175  $level = LogLevel::NOTICE;
176  } else if (($php_error_level & E_WARNING) == E_WARNING) {
177  $level = LogLevel::WARNING;
178  } else if (($php_error_level & E_ERROR) == E_ERROR) {
179  $level = LogLevel::ERROR;
180  }
181  }
182 
183  $this->level = $this->normalizeLevel($level);
184  }
185 
194  public function getLevel($severity = true) {
195  if ($severity) {
196  return array_search($this->level, self::$elgg_levels);
197  }
198 
199  return $this->level;
200  }
201 
209  public function isLoggable($level) {
210  $level = $this->normalizeLevel($level);
211 
212  $severity = array_search($level, self::$elgg_levels);
213  if (!$this->getLevel() || $severity < $this->getLevel()) {
214  return false;
215  }
216 
217  return true;
218  }
219 
223  public function log($level, $message, array $context = []): void {
224 
225  $level = $this->normalizeLevel($level);
226 
227  if (!empty($this->disabled_stack)) {
228  // capture to top of stack
229  end($this->disabled_stack);
230  $key = key($this->disabled_stack);
231  $this->disabled_stack[$key][] = [
232  'message' => $message,
233  'level' => $level,
234  ];
235  }
236 
237  if (!$this->isLoggable($level)) {
238  return;
239  }
240 
241  // when capturing, still use consistent return value
242  if (!empty($this->disabled_stack)) {
243  return;
244  }
245 
246  parent::log($level, $message, $context);
247  }
248 
252  public function emergency($message, array $context = []): void {
253  $this->log(LogLevel::EMERGENCY, $message, $context);
254  }
255 
259  public function alert($message, array $context = []): void {
260  $this->log(LogLevel::ALERT, $message, $context);
261  }
262 
266  public function critical($message, array $context = []): void {
267  $this->log(LogLevel::CRITICAL, $message, $context);
268  }
269 
273  public function error($message, array $context = []): void {
274  $this->log(LogLevel::ERROR, $message, $context);
275  }
276 
280  public function warning($message, array $context = []): void {
281  $this->log(LogLevel::WARNING, $message, $context);
282  }
283 
287  public function notice($message, array $context = []): void {
288  $this->log(LogLevel::NOTICE, $message, $context);
289  }
290 
294  public function info($message, array $context = []): void {
295  $this->log(LogLevel::INFO, $message, $context);
296  }
297 
301  public function debug($message, array $context = []): void {
302  $this->log(LogLevel::DEBUG, $message, $context);
303  }
304 
312  public function dump($data) {
313  $this->log(LogLevel::ERROR, $data);
314  }
315 
328  public function disable() {
329  $this->disabled_stack[] = [];
330  }
331 
339  public function enable() {
340  return array_pop($this->disabled_stack);
341  }
342 }
disable()
Temporarily disable logging and capture logs (before tests)
Definition: Logger.php:328
$context
Definition: add.php:8
Custom log formatter.
info($message, array $context=[])
{}
Definition: Logger.php:294
getLevel($severity=true)
Get the current logging level severity.
Definition: Logger.php:194
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:252
static getStdOut()
Load console output interface.
static factory(InputInterface $input=null, OutputInterface $output=null)
Build a new logger.
Definition: Logger.php:78
alert($message, array $context=[])
{}
Definition: Logger.php:259
Wrapper for console application.
Definition: Application.php:11
debug($message, array $context=[])
{}
Definition: Logger.php:301
Inject backtrace stack into the record.
setLevel($level=null)
Set the logging level.
Definition: Logger.php:168
warning($message, array $context=[])
{}
Definition: Logger.php:280
log($level, $message, array $context=[])
{}
Definition: Logger.php:223
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
Logger.
Definition: Logger.php:26
enable()
Restore logging and get record of log calls (after tests)
Definition: Logger.php:339
error($message, array $context=[])
{}
Definition: Logger.php:273
Handle system and PHP errors.
critical($message, array $context=[])
{}
Definition: Logger.php:266
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:312
$handler
Definition: add.php:7
isLoggable($level)
Check if a level is loggable under current logging level.
Definition: Logger.php:209
Format errors for console output.
normalizeLevel($level=null)
Normalizes legacy string or numeric representation of the level to LogLevel strings.
Definition: Logger.php:136
$output
Definition: download.php:9
notice($message, array $context=[])
{}
Definition: Logger.php:287