]> git.wh0rd.org Git - tt-rss.git/commitdiff
make logging configurable; add logging to syslog
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 19 Apr 2013 05:45:43 +0000 (09:45 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 19 Apr 2013 05:45:43 +0000 (09:45 +0400)
classes/logger.php
classes/logger/sql.php
classes/logger/syslog.php [new file with mode: 0644]
classes/pref/system.php
config.php-dist
include/errorhandler.php
include/sanity_config.php

index 3c501eb92cd0fc2a5263e309e1daba0313383183..4a9c1df820cba383d06cbea104831d3b6f63be43 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 class Logger {
+       private static $instance;
+       private $adapter;
 
        public static $errornames = array(
                1                       => '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;
+       }
+
 }
 ?>
index 50e5de9a6e246bc64788ab85f4eedf0ba870efeb..c0f8b4598ba7841de29371e3ed7a6fd9d331bd3a 100644 (file)
@@ -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 (file)
index 0000000..b8b5260
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+class Logger_Syslog {
+
+       function log_error($errno, $errstr, $file, $line, $context) {
+
+               switch ($errno) {
+               case E_ERROR:
+               case E_PARSE:
+               case E_CORE_ERROR:
+               case E_COMPILE_ERROR:
+               case E_USER_ERROR:
+                       $priority = LOG_ERR;
+                       break;
+               case E_WARNING:
+               case E_CORE_WARNING:
+               case E_COMPILE_WARNING:
+               case E_USER_WARNING:
+                       $priority = LOG_WARNING;
+                       break;
+               default:
+                       $priority = LOG_INFO;
+               }
+
+               $errname = Logger::$errornames[$errno] . " ($errno)";
+
+               syslog($priority, "[tt-rss] $errname ($file:$line) $errstr");
+
+       }
+
+}
+?>
index 725c337dc99ec0504dd2ab42a3b0e93e2347f146..d2b6cd7461ff8240a4cbfabd87f3670a13204b41 100644 (file)
@@ -24,46 +24,53 @@ class Pref_System extends Handler_Protected {
                print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
                print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Error Log')."\">";
 
-               $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 "<button dojoType=\"dijit.form.Button\"
-                       onclick=\"updateSystemList()\">".__('Refresh')."</button> ";
-
-               print "<p><table width=\"100%\" cellspacing=\"10\" class=\"prefErrorLog\">";
-
-               print "<tr class=\"title\">
-                       <td width='5%'>".__("Error")."</td>
-                       <td>".__("Filename")."</td>
-                       <td>".__("Message")."</td>
-                       <td width='5%'>".__("User")."</td>
-                       <td width='5%'>".__("Date")."</td>
-                       </tr>";
-
-               while ($line = $this->dbh->fetch_assoc($result)) {
-                       print "<tr class=\"errrow\">";
-
-                       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 "<button dojoType=\"dijit.form.Button\"
+                               onclick=\"updateSystemList()\">".__('Refresh')."</button> ";
+
+                       print "<p><table width=\"100%\" cellspacing=\"10\" class=\"prefErrorLog\">";
+
+                       print "<tr class=\"title\">
+                               <td width='5%'>".__("Error")."</td>
+                               <td>".__("Filename")."</td>
+                               <td>".__("Message")."</td>
+                               <td width='5%'>".__("User")."</td>
+                               <td width='5%'>".__("Date")."</td>
+                               </tr>";
+
+                       while ($line = $this->dbh->fetch_assoc($result)) {
+                               print "<tr class=\"errrow\">";
+
+                               foreach ($line as $k => $v) {
+                                       $line[$k] = htmlspecialchars($v);
+                               }
+
+                               print "<td class='errno'>" . Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")</td>";
+                               print "<td class='filename'>" . $line["filename"] . ":" . $line["lineno"] . "</td>";
+                               print "<td class='errstr'>" . $line["errstr"] . "</td>";
+                               print "<td class='login'>" . $line["login"] . "</td>";
+
+                               print "<td class='timestamp'>" .
+                                       make_local_datetime(
+                                       $line["created_at"], false) . "</td>";
+
+                               print "</tr>";
                        }
 
-                       print "<td class='errno'>" . Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")</td>";
-                       print "<td class='filename'>" . $line["filename"] . ":" . $line["lineno"] . "</td>";
-                       print "<td class='errstr'>" . $line["errstr"] . "</td>";
-                       print "<td class='login'>" . $line["login"] . "</td>";
+                       print "</table>";
+               } else {
 
-                       print "<td class='timestamp'>" .
-                               make_local_datetime(
-                               $line["created_at"], false) . "</td>";
+                       print_notice("Please set LOG_DESTINATION to 'sql' in config.php to enable database logging.");
 
-                       print "</tr>";
                }
 
-               print "</table>";
-
                print "</div>";
 
                PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
index 7cb9d9397d3b7f665945dc7537d8f4918a81188e..1c356a9aeba07c975d10bf7b89ff7572079784d9 100644 (file)
        // 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
index 2c8d35f8371c4a8b2759d014642245910b50ad97..9acef23575c624acbc21f06ef62fcc607db42582 100644 (file)
@@ -1,22 +1,12 @@
 <?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 (error_reporting() == 0 || !$errno) return false;
 
-       if (!$logger) $logger = new Logger_SQL();
-
        $file = substr(str_replace(dirname(dirname(__FILE__)), "", $file), 1);
 
-       if ($logger) {
-               return $logger->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;
index d5cfd2d7819d9c55e81e7a6a6a5ddda2f27eeb5b..7d8afe102c9e6687fee16e41fc4a5b9bd290579f 100644 (file)
@@ -1,3 +1,3 @@
-<?php # This file has been generated at:  Tue Apr 16 16:41:13 MSK 2013
+<?php # This file has been generated at:  Fri Apr 19 09:22:33 MSK 2013
 define('GENERATED_CONFIG_CHECK', 26);
-$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', 'CONFIG_VERSION'); ?>
+$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'); ?>