experimental SQL-based error logger

This commit is contained in:
Andrew Dolgov 2013-04-16 19:41:31 +04:00
parent 4f032700db
commit 889a5f9f19
9 changed files with 177 additions and 5 deletions

24
classes/logger.php Normal file
View file

@ -0,0 +1,24 @@
<?php
class Logger {
protected $errornames = array(
2 => '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;
}
}
?>

35
classes/logger/sql.php Normal file
View file

@ -0,0 +1,35 @@
<?php
class Logger_SQL {
private $link;
function __construct() {
$this->link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
function log_error($errno, $errstr, $file, $line, $context) {
if ($errno == E_NOTICE) return false;
if ($this->link) {
$errno = db_escape_string($this->link, $errno);
$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));
$owner_uid = $_SESSION["uid"] ? $_SESSION["uid"] : "NULL";
$result = db_query($this->link,
"INSERT INTO ttrss_error_log
(errno, errstr, filename, lineno, context, owner_uid, created_at) VALUES
($errno, '$errstr', '$file', '$line', '$context', $owner_uid, NOW())");
return db_affected_rows($this->link, $result) != 0;
}
return false;
}
}
?>

50
include/errorhandler.php Normal file
View file

@ -0,0 +1,50 @@
<?php
// TODO: make configurable
require_once "classes/logger.php";
require_once "classes/logger/sql.php";
function ttrss_error_handler($errno, $errstr, $file, $line, $context) {
global $logger;
if (!$logger) $logger = new Logger_SQL();
$errfile = str_replace(dirname(dirname(__FILE__)), "", $errfile);
if ($logger) {
return $logger->log_error($errno, $errstr, $file, $line, $context);
}
return false;
}
function ttrss_fatal_handler() {
global $logger;
$file = "UNKNOWN FILE";
$errstr = "UNKNOWN";
$errno = E_CORE_ERROR;
$line = -1;
$error = error_get_last();
if ($error !== NULL) {
$errno = $error["type"];
$file = $error["file"];
$line = $error["line"];
$errstr = $error["message"];
$context = debug_backtrace();
$file = str_replace(dirname(dirname(__FILE__)) . "/", "", $file);
if (!$logger) $logger = new Logger_SQL();
if ($logger) {
$logger->log_error($errno, $errstr, $file, $line, $context);
}
}
}
register_shutdown_function('ttrss_fatal_handler');
set_error_handler('ttrss_error_handler');
?>

View file

@ -1,6 +1,6 @@
<?php
define('EXPECTED_CONFIG_VERSION', 26);
define('SCHEMA_VERSION', 117);
define('SCHEMA_VERSION', 118);
define('LABEL_BASE_INDEX', -1024);
define('PLUGIN_FEED_BASE_INDEX', -128);
@ -3369,9 +3369,8 @@
return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
}
function init_connection($link) {
function init_connection_only($link) {
if ($link) {
if (DB_TYPE == "pgsql") {
pg_query($link, "set client_encoding = 'UTF-8'");
pg_set_client_encoding("UNICODE");
@ -3385,6 +3384,16 @@
}
}
return true;
}
return false;
}
function init_connection($link) {
if ($link) {
init_connection_only($link);
global $pluginhost;
$pluginhost = new PluginHost($link);

View file

@ -3,6 +3,7 @@
require_once "config.php";
require_once "db.php";
require_once "errorhandler.php";
require_once "lib/accept-to-gettext.php";
require_once "lib/gettext/gettext.inc";
require_once "version.php";

View file

@ -1,6 +1,7 @@
SET NAMES utf8;
SET CHARACTER SET utf8;
drop table if exists ttrss_error_log;
drop table if exists ttrss_plugin_storage;
drop table if exists ttrss_linked_feeds;
drop table if exists ttrss_linked_instances;
@ -299,7 +300,7 @@ create table ttrss_tags (id integer primary key auto_increment,
create table ttrss_version (schema_version int not null) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
insert into ttrss_version values (117);
insert into ttrss_version values (118);
create table ttrss_enclosures (id integer primary key auto_increment,
content_url text not null,
@ -478,5 +479,15 @@ create table ttrss_plugin_storage (
content longtext not null,
foreign key (owner_uid) references ttrss_users(id) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
create table ttrss_error_log(
id integer not null auto_increment primary key,
owner_uid integer,
errno integer not null,
errstr text not null,
filename text not null,
lineno integer not null,
context text not null,
created_at datetime not null,
foreign key (owner_uid) references ttrss_users(id) ON DELETE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
commit;

View file

@ -1,3 +1,4 @@
drop table if exists ttrss_error_log;
drop table if exists ttrss_plugin_storage;
drop table if exists ttrss_linked_feeds;
drop table if exists ttrss_linked_instances;
@ -257,7 +258,7 @@ create index ttrss_tags_post_int_id_idx on ttrss_tags(post_int_id);
create table ttrss_version (schema_version int not null);
insert into ttrss_version values (117);
insert into ttrss_version values (118);
create table ttrss_enclosures (id serial not null primary key,
content_url text not null,
@ -421,4 +422,14 @@ create table ttrss_plugin_storage (
owner_uid integer not null references ttrss_users(id) ON DELETE CASCADE,
content text not null);
create table ttrss_error_log(
id serial not null primary key,
owner_uid integer references ttrss_users(id) ON DELETE SET NULL,
errno integer not null,
errstr text not null,
filename text not null,
lineno integer not null,
context text not null,
created_at timestamp not null);
commit;

View file

@ -0,0 +1,16 @@
begin;
create table ttrss_error_log(
id integer not null auto_increment primary key,
owner_uid integer,
errno integer not null,
errstr text not null,
filename text not null,
lineno integer not null,
context text not null,
created_at datetime not null,
foreign key (owner_uid) references ttrss_users(id) ON DELETE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
update ttrss_version set schema_version = 118;
commit;

View file

@ -0,0 +1,15 @@
begin;
create table ttrss_error_log(
id serial not null primary key,
owner_uid integer references ttrss_users(id) ON DELETE SET NULL,
errno integer not null,
errstr text not null,
filename text not null,
lineno integer not null,
context text not null,
created_at timestamp not null);
update ttrss_version set schema_version = 118;
commit;