ttrss/classes/logger.php

87 lines
2.3 KiB
PHP
Raw Normal View History

2013-04-16 15:41:31 +00:00
<?php
class Logger {
private static $instance;
private $adapter;
2013-04-16 15:41:31 +00:00
const LOG_DEST_SQL = "sql";
const LOG_DEST_STDOUT = "stdout";
const LOG_DEST_SYSLOG = "syslog";
2021-02-25 12:49:30 +00:00
const ERROR_NAMES = [
2013-04-16 16:16:15 +00:00
1 => 'E_ERROR',
2013-04-16 15:41:31 +00:00
2 => 'E_WARNING',
4 => 'E_PARSE',
2013-04-16 15:41:31 +00:00
8 => 'E_NOTICE',
16 => 'E_CORE_ERROR',
32 => 'E_CORE_WARNING',
64 => 'E_COMPILE_ERROR',
128 => 'E_COMPILE_WARNING',
2013-04-16 15:41:31 +00:00
256 => 'E_USER_ERROR',
512 => 'E_USER_WARNING',
1024 => 'E_USER_NOTICE',
2048 => 'E_STRICT',
4096 => 'E_RECOVERABLE_ERROR',
8192 => 'E_DEPRECATED',
16384 => 'E_USER_DEPRECATED',
2021-02-25 12:49:30 +00:00
32767 => 'E_ALL'];
2013-04-16 15:41:31 +00:00
2021-02-25 12:49:30 +00:00
static function log_error(int $errno, string $errstr, string $file, int $line, $context) {
return self::get_instance()->_log_error($errno, $errstr, $file, $line, $context);
}
private function _log_error($errno, $errstr, $file, $line, $context) {
//if ($errno == E_NOTICE) return false;
if ($this->adapter)
return $this->adapter->log_error($errno, $errstr, $file, $line, $context);
else
return false;
2013-04-16 15:41:31 +00:00
}
2021-02-25 12:49:30 +00:00
static function log(int $errno, string $errstr, $context = "") {
return self::get_instance()->_log($errno, $errstr, $context);
}
private function _log(int $errno, string $errstr, $context = "") {
if ($this->adapter)
return $this->adapter->log_error($errno, $errstr, '', 0, $context);
else
return user_error($errstr, $errno);
}
private function __clone() {
//
}
function __construct() {
2021-02-22 19:35:27 +00:00
switch (Config::get(Config::LOG_DESTINATION)) {
case self::LOG_DEST_SQL:
$this->adapter = new Logger_SQL();
break;
case self::LOG_DEST_SYSLOG:
$this->adapter = new Logger_Syslog();
break;
case self::LOG_DEST_STDOUT:
2018-08-15 12:08:31 +00:00
$this->adapter = new Logger_Stdout();
break;
default:
$this->adapter = false;
}
if ($this->adapter && !implements_interface($this->adapter, "Logger_Adapter"))
user_error("Adapter for LOG_DESTINATION: " . Config::LOG_DESTINATION . " does not implement required interface.", E_USER_ERROR);
2013-04-16 15:41:31 +00:00
}
2021-02-25 12:49:30 +00:00
private static function get_instance() : Logger {
if (self::$instance == null)
self::$instance = new self();
return self::$instance;
}
2021-02-25 12:49:30 +00:00
static function get() : Logger {
user_error("Please don't use Logger::get(), call Logger::log(...) instead.", E_USER_DEPRECATED);
return self::get_instance();
}
2018-08-15 12:08:31 +00:00
}