From 889a5f9f195309df5842f142986b3166212d8a58 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 16 Apr 2013 19:41:31 +0400 Subject: experimental SQL-based error logger --- classes/logger.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 classes/logger.php (limited to 'classes/logger.php') diff --git a/classes/logger.php b/classes/logger.php new file mode 100644 index 000000000..6370e1425 --- /dev/null +++ b/classes/logger.php @@ -0,0 +1,24 @@ + 'E_WARNING', + 8 => 'E_NOTICE', + 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', + 32767 => 'E_ALL'); + + function log_error($errno, $errstr, $file, $line, $context) { + return false; + } + + function log($string) { + return false; + } +} +?> -- cgit v1.2.3-54-g00ecf From 4e53956addb597d99a76d10d302ab56faad88bf8 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 16 Apr 2013 20:16:15 +0400 Subject: implement error log viewer --- classes/logger.php | 3 ++- classes/logger/sql.php | 3 ++- classes/pref/prefs.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ include/errorhandler.php | 4 ++-- prefs.css | 7 +++++++ 5 files changed, 58 insertions(+), 4 deletions(-) (limited to 'classes/logger.php') diff --git a/classes/logger.php b/classes/logger.php index 6370e1425..e0ca37363 100644 --- a/classes/logger.php +++ b/classes/logger.php @@ -1,7 +1,8 @@ 'E_ERROR', 2 => 'E_WARNING', 8 => 'E_NOTICE', 256 => 'E_USER_ERROR', diff --git a/classes/logger/sql.php b/classes/logger/sql.php index 7ee22844e..a478e8928 100644 --- a/classes/logger/sql.php +++ b/classes/logger/sql.php @@ -16,7 +16,8 @@ class Logger_SQL { $errstr = db_escape_string($this->link, $errstr); $file = db_escape_string($this->link, $file); $line = db_escape_string($this->link, $line); - $context = db_escape_string($this->link, json_encode($context)); + $context = ''; // backtrace is a lot of data which is not really critical to store + //$context = db_escape_string($this->link, serialize($context)); $owner_uid = $_SESSION["uid"] ? $_SESSION["uid"] : "NULL"; diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 29541e04d..c6d41c15b 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -723,6 +723,51 @@ class Pref_Prefs extends Handler_Protected { print ""; #pane + if ($_SESSION["access_level"] == 10) { + + print "
"; + print "

".__("Error Log")."

"; + + $result = db_query($this->link, "SELECT errno, errstr, filename, lineno, + created_at, login FROM ttrss_error_log + LEFT JOIN ttrss_users ON (owner_uid = ttrss_users.id) + ORDER BY ttrss_error_log.id DESC + LIMIT 100"); + + print "

"; + + print " + + + + + + "; + + while ($line = db_fetch_assoc($result)) { + print ""; + + foreach ($line as $k => $v) { + $line[$k] = htmlspecialchars($v); + } + + print ""; + print ""; + print ""; + print ""; + + print ""; + + print ""; + } + + print "
".__("Error")."".__("Filename")."".__("Message")."".__("User")."".__("Date")."
" . Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")" . $line["filename"] . ":" . $line["lineno"] . "" . $line["errstr"] . "" . + make_local_datetime($this->link, + $line["created_at"], false) . "
"; + + print "

"; + } + print "
"; print "

".__("Plugins")."

"; diff --git a/include/errorhandler.php b/include/errorhandler.php index 13eed0e30..bb60592b7 100644 --- a/include/errorhandler.php +++ b/include/errorhandler.php @@ -8,7 +8,7 @@ function ttrss_error_handler($errno, $errstr, $file, $line, $context) { if (!$logger) $logger = new Logger_SQL(); - $errfile = str_replace(dirname(dirname(__FILE__)), "", $errfile); + $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1); if ($logger) { return $logger->log_error($errno, $errstr, $file, $line, $context); @@ -35,7 +35,7 @@ function ttrss_fatal_handler() { $context = debug_backtrace(); - $file = str_replace(dirname(dirname(__FILE__)) . "/", "", $file); + $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1); if (!$logger) $logger = new Logger_SQL(); diff --git a/prefs.css b/prefs.css index 353fbe80b..dc527ca1e 100644 --- a/prefs.css +++ b/prefs.css @@ -125,4 +125,11 @@ ul.userFeedList { padding : 0px; } +table.prefErrorLog tr.errrow td { + font-size : 10px; +} +table.prefErrorLog tr.errrow td.errno { + font-style : italic; + white-space : nowrap; +} -- cgit v1.2.3-54-g00ecf From e2261e177bc9f5308185d91783ca128000c6fd87 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 16 Apr 2013 20:34:20 +0400 Subject: implement old log entry purging in update_daemon_common; add some more error types; tweak error log display --- classes/logger.php | 5 +++++ include/rssfuncs.php | 14 ++++++++++++++ prefs.css | 6 ++++++ 3 files changed, 25 insertions(+) (limited to 'classes/logger.php') diff --git a/classes/logger.php b/classes/logger.php index e0ca37363..3c501eb92 100644 --- a/classes/logger.php +++ b/classes/logger.php @@ -4,7 +4,12 @@ class Logger { public static $errornames = array( 1 => 'E_ERROR', 2 => 'E_WARNING', + 4 => 'E_PARSE', 8 => 'E_NOTICE', + 16 => 'E_CORE_ERROR', + 32 => 'E_CORE_WARNING', + 64 => 'E_COMPILE_ERROR', + 128 => 'E_COMPILE_WARNING', 256 => 'E_USER_ERROR', 512 => 'E_USER_WARNING', 1024 => 'E_USER_NOTICE', diff --git a/include/rssfuncs.php b/include/rssfuncs.php index b3bf25849..0a20f5d8e 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -148,6 +148,7 @@ expire_cached_files($debug); expire_lock_files($debug); + expire_error_log($link, $debug); $nf = 0; @@ -1197,6 +1198,19 @@ return $doc->saveXML($node); } + function expire_error_log($link, $debug) { + if ($debug) _debug("Removing old error log entries..."); + + if (DB_TYPE == "pgsql") { + db_query($link, "DELETE FROM ttrss_error_log + WHERE created_at < NOW() - INTERVAL '7 days'"); + } else { + db_query($link, "DELETE FROM ttrss_error_log + WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"); + } + + } + function expire_lock_files($debug) { if ($debug) _debug("Removing old lock files..."); diff --git a/prefs.css b/prefs.css index dc527ca1e..254de4f34 100644 --- a/prefs.css +++ b/prefs.css @@ -131,5 +131,11 @@ table.prefErrorLog tr.errrow td { table.prefErrorLog tr.errrow td.errno { font-style : italic; + font-weight : bold; white-space : nowrap; } + +table.prefErrorLog td.filename, table.prefErrorLog td.login, table.prefErrorLog td.timestamp { + color : gray; +} + -- cgit v1.2.3-54-g00ecf From b367c951b990b38677e67f1a1756cd4d1eaee50b Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 19 Apr 2013 09:45:43 +0400 Subject: make logging configurable; add logging to syslog --- classes/logger.php | 39 ++++++++++++++++++++++-- classes/logger/sql.php | 3 -- classes/logger/syslog.php | 31 ++++++++++++++++++++ classes/pref/system.php | 75 ++++++++++++++++++++++++++--------------------- config.php-dist | 6 ++++ include/errorhandler.php | 21 ++----------- include/sanity_config.php | 4 +-- 7 files changed, 119 insertions(+), 60 deletions(-) create mode 100644 classes/logger/syslog.php (limited to 'classes/logger.php') diff --git a/classes/logger.php b/classes/logger.php index 3c501eb92..4a9c1df82 100644 --- a/classes/logger.php +++ b/classes/logger.php @@ -1,5 +1,7 @@ 'E_ERROR', @@ -20,11 +22,44 @@ class Logger { 32767 => 'E_ALL'); function log_error($errno, $errstr, $file, $line, $context) { - return false; + if ($errno == E_NOTICE) return false; + + if ($this->adapter) + return $this->adapter->log_error($errno, $errstr, $file, $line, $context); + else + return false; } function log($string) { - return false; + if ($this->adapter) + return $this->adapter->log($string); + else + return false; + } + + private function __clone() { + // + } + + function __construct() { + switch (LOG_DESTINATION) { + case "sql": + $this->adapter = new Logger_SQL(); + break; + case "syslog": + $this->adapter = new Logger_Syslog(); + break; + default: + $this->adapter = false; + } } + + public static function get() { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; + } + } ?> diff --git a/classes/logger/sql.php b/classes/logger/sql.php index 50e5de9a6..c0f8b4598 100644 --- a/classes/logger/sql.php +++ b/classes/logger/sql.php @@ -2,9 +2,6 @@ class Logger_SQL { function log_error($errno, $errstr, $file, $line, $context) { - - if ($errno == E_NOTICE) return false; - if (Db::get() && get_schema_version() > 117) { $errno = Db::get()->escape_string($errno); diff --git a/classes/logger/syslog.php b/classes/logger/syslog.php new file mode 100644 index 000000000..b8b5260a0 --- /dev/null +++ b/classes/logger/syslog.php @@ -0,0 +1,31 @@ + diff --git a/classes/pref/system.php b/classes/pref/system.php index 725c337dc..d2b6cd746 100644 --- a/classes/pref/system.php +++ b/classes/pref/system.php @@ -24,46 +24,53 @@ class Pref_System extends Handler_Protected { print "
"; print "
"; - $result = $this->dbh->query("SELECT errno, errstr, filename, lineno, - created_at, login FROM ttrss_error_log - LEFT JOIN ttrss_users ON (owner_uid = ttrss_users.id) - ORDER BY ttrss_error_log.id DESC - LIMIT 100"); - - print " "; - - print "

"; - - print " - - - - - - "; - - while ($line = $this->dbh->fetch_assoc($result)) { - print ""; - - foreach ($line as $k => $v) { - $line[$k] = htmlspecialchars($v); + if (LOG_DESTINATION == "sql") { + + $result = $this->dbh->query("SELECT errno, errstr, filename, lineno, + created_at, login FROM ttrss_error_log + LEFT JOIN ttrss_users ON (owner_uid = ttrss_users.id) + ORDER BY ttrss_error_log.id DESC + LIMIT 100"); + + print " "; + + print "

".__("Error")."".__("Filename")."".__("Message")."".__("User")."".__("Date")."
"; + + print " + + + + + + "; + + while ($line = $this->dbh->fetch_assoc($result)) { + print ""; + + foreach ($line as $k => $v) { + $line[$k] = htmlspecialchars($v); + } + + print ""; + print ""; + print ""; + print ""; + + print ""; + + print ""; } - print ""; - print ""; - print ""; - print ""; + print "
".__("Error")."".__("Filename")."".__("Message")."".__("User")."".__("Date")."
" . Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")" . $line["filename"] . ":" . $line["lineno"] . "" . $line["errstr"] . "" . + make_local_datetime( + $line["created_at"], false) . "
" . Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")" . $line["filename"] . ":" . $line["lineno"] . "" . $line["errstr"] . "
"; + } else { - print "" . - make_local_datetime( - $line["created_at"], false) . ""; + print_notice("Please set LOG_DESTINATION to 'sql' in config.php to enable database logging."); - print ""; } - print ""; - print "

"; PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB, diff --git a/config.php-dist b/config.php-dist index 7cb9d9397..1c356a9ae 100644 --- a/config.php-dist +++ b/config.php-dist @@ -192,6 +192,12 @@ // authentication plugin here (auth_*). // Users may enable other user plugins from Preferences/Plugins but may not // disable plugins specified in this list. + + define('LOG_DESTINATION', 'sql'); + // Log destination to use. Possible values: sql (uses internal logging + // you can read in Preferences -> System), syslog - logs to system log. + // Setting this to blank uses PHP logging (usually to http server + // error.log). define('CONFIG_VERSION', 26); // Expected config version. Please update this option in config.php diff --git a/include/errorhandler.php b/include/errorhandler.php index 2c8d35f83..9acef2357 100644 --- a/include/errorhandler.php +++ b/include/errorhandler.php @@ -1,22 +1,12 @@ log_error($errno, $errstr, $file, $line, $context); - } - - return false; + return Logger::get()->log_error($errno, $errstr, $file, $line, $context); } function ttrss_fatal_handler() { @@ -36,14 +26,7 @@ function ttrss_fatal_handler() { $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1); - if (!$logger) $logger = new Logger_SQL(); - - if ($logger) { - if ($logger->log_error($errno, $errstr, $file, $line, $context)) { - return true; - } - } - return false; + return Logger::get()->log_error($errno, $errstr, $file, $line, $context); } return false; diff --git a/include/sanity_config.php b/include/sanity_config.php index d5cfd2d78..7d8afe102 100644 --- a/include/sanity_config.php +++ b/include/sanity_config.php @@ -1,3 +1,3 @@ - +$requred_defines = array( 'DB_TYPE', 'DB_HOST', 'DB_USER', 'DB_NAME', 'DB_PASS', 'MYSQL_CHARSET', 'SELF_URL_PATH', 'FEED_CRYPT_KEY', 'SINGLE_USER_MODE', 'SIMPLE_UPDATE_MODE', 'PHP_EXECUTABLE', 'LOCK_DIRECTORY', 'CACHE_DIR', 'ICONS_DIR', 'ICONS_URL', 'AUTH_AUTO_CREATE', 'AUTH_AUTO_LOGIN', 'FORCE_ARTICLE_PURGE', 'PUBSUBHUBBUB_HUB', 'PUBSUBHUBBUB_ENABLED', 'SPHINX_ENABLED', 'SPHINX_SERVER', 'SPHINX_INDEX', 'ENABLE_REGISTRATION', 'REG_NOTIFY_ADDRESS', 'REG_MAX_USERS', 'SESSION_COOKIE_LIFETIME', 'SESSION_CHECK_ADDRESS', 'SMTP_FROM_NAME', 'SMTP_FROM_ADDRESS', 'DIGEST_SUBJECT', 'SMTP_SERVER', 'SMTP_LOGIN', 'SMTP_PASSWORD', 'CHECK_FOR_NEW_VERSION', 'ENABLE_GZIP_OUTPUT', 'PLUGINS', 'LOG_DESTINATION', 'CONFIG_VERSION'); ?> -- cgit v1.2.3-54-g00ecf