[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [ernie/master 1/2] Write INFO and WARNING logs of last run to website.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Tue, 13 Apr 2010 16:37:51 +0200
Subject: Write INFO and WARNING logs of last run to website.
Commit: 6061c2ebb7cf4f94074239cbdec0c334b71c4110

---
 logging.properties            |   23 -----
 run.sh                        |    2 +-
 src/LoggingConfiguration.java |  188 +++++++++++++++++++++++++++++++++++++++++
 src/Main.java                 |    3 +
 4 files changed, 192 insertions(+), 24 deletions(-)
 delete mode 100644 logging.properties
 create mode 100644 src/LoggingConfiguration.java

diff --git a/logging.properties b/logging.properties
deleted file mode 100644
index e05327e..0000000
--- a/logging.properties
+++ /dev/null
@@ -1,23 +0,0 @@
-# Log levels used by ERNIE:
-# - SEVERE: An event made it impossible to continue program execution.
-# - WARNING: A potential problem occured that requires the operator to
-#   look after the otherwise unattended setup
-# - INFO: Messages on INFO level are meant to help the operator in making
-#   sure that operation works as expected.
-# - FINE: Debug messages that are used to identify prolems and which are
-#   turned on by default.
-# - FINER: More detailed debug messages to investigate problems in more
-#   detail. Not turned on by default. Increase log file limit when using
-#   FINER.
-# - FINEST: Most detailed debug messages. Not used.
-handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
-.level=FINE
-sun.level=OFF
-java.util.logging.ConsoleHandler.level=WARNING
-java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
-java.util.logging.FileHandler.level=FINE
-java.util.logging.FileHandler.pattern=log
-java.util.logging.FileHandler.limit=5000000
-java.util.logging.FileHandler.count=5
-java.util.logging.FileHandler.append=true
-java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
diff --git a/run.sh b/run.sh
index ddd5554..3fef684 100755
--- a/run.sh
+++ b/run.sh
@@ -1,3 +1,3 @@
 #!/bin/sh
-java -Xms128m -Xmx1024m -cp bin/:lib/commons-codec-1.4.jar:lib/commons-compress-1.0.jar:lib/postgresql-8.4-701.jdbc4.jar -Djava.util.logging.config.file=logging.properties Main
+java -Xms128m -Xmx1024m -cp bin/:lib/commons-codec-1.4.jar:lib/commons-compress-1.0.jar:lib/postgresql-8.4-701.jdbc4.jar Main
 
diff --git a/src/LoggingConfiguration.java b/src/LoggingConfiguration.java
new file mode 100644
index 0000000..3a2126c
--- /dev/null
+++ b/src/LoggingConfiguration.java
@@ -0,0 +1,188 @@
+import java.io.*;
+import java.text.*;
+import java.util.Date;
+import java.util.logging.*;
+/**
+ * Initialize logging configuration.
+ *
+ * Log levels used by ERNIE:
+ *
+ * - SEVERE: An event made it impossible to continue program execution.
+ * - WARNING: A potential problem occurred that requires the operator to
+ *   look after the otherwise unattended setup
+ * - INFO: Messages on INFO level are meant to help the operator in making
+ *   sure that operation works as expected.
+ * - FINE: Debug messages that are used to identify problems and which are
+ *   turned on by default.
+ * - FINER: More detailed debug messages to investigate problems in more
+ *   detail. Not turned on by default. Increase log file limit when using
+ *   FINER.
+ * - FINEST: Most detailed debug messages. Not used.
+ */
+public class LoggingConfiguration {
+  public LoggingConfiguration() {
+
+    /* Remove default console handler. */
+    for (Handler h : Logger.getLogger("").getHandlers()) {
+      Logger.getLogger("").removeHandler(h);
+    }
+
+    /* Disable logging of internal Sun classes. */
+    Logger.getLogger("sun").setLevel(Level.OFF);
+
+    /* Create console logger to write messages on WARNING or higher to the
+     * console. */
+    final SimpleDateFormat dateTimeFormat =
+        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    Formatter cf = new Formatter() {
+      public String format(LogRecord record) {
+        return dateTimeFormat.format(new Date(record.getMillis())) + " "
+            + record.getMessage() + "\n";
+      }
+    };
+    Handler ch = new ConsoleHandler();
+    ch.setFormatter(cf);
+    ch.setLevel(Level.WARNING);
+    Logger.getLogger("").addHandler(ch);
+
+    /* Initialize own logger for this class. */
+    Logger logger = Logger.getLogger(
+        LoggingConfiguration.class.getName());
+
+    /* Create file logger that writes all messages on FINE or higher to a
+     * local file. */
+    Formatter ff = new Formatter() {
+      public String format(LogRecord record) {
+        return dateTimeFormat.format(new Date(record.getMillis())) + " "
+            + record.getLevel() + " " + record.getSourceClassName() + " "
+            + record.getSourceMethodName() + " " + record.getMessage()
+            + "\n";
+      }
+    };
+    try {
+      FileHandler fh = new FileHandler("log", 5000000, 5, true);
+      fh.setFormatter(ff);
+      fh.setLevel(Level.FINE);
+      Logger.getLogger("").addHandler(fh);
+    } catch (SecurityException e) {
+      logger.log(Level.WARNING, "No permission to create log file. "
+          + "Logging to file is disabled.", e);
+    } catch (IOException e) {
+      logger.log(Level.WARNING, "Could not write to log file. Logging to "
+          + "file is disabled.", e);
+    }
+
+    /* Initialize website logger that writes all message on INFO or higher
+     * to a website. */
+    Formatter wf = new Formatter() {
+      private StringBuilder infos = new StringBuilder();
+      private StringBuilder warnings = new StringBuilder();
+      public String format(LogRecord record) {
+        String msg = "          <tr>\n"
+            + "            <td>"
+            + dateTimeFormat.format(new Date(record.getMillis()))
+            + "</td>\n"
+            + "            <td>" + record.getMessage() + "</td>\n"
+            + "          </tr>\n";
+        if (record.getLevel().equals(Level.WARNING)) {
+          this.warnings.append(msg);
+        } else {
+          this.infos.append(msg);
+        }
+        return "";
+      }
+      public String getTail(Handler h) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
+              + "Transitional//EN\">\n"
+            + "<html>\n"
+            + "  <head>\n"
+            + "    <title>Tor Metrics Portal: Last execution "
+              + "logs</title>\n"
+            + "    <meta http-equiv=Content-Type content=\"text/html; "
+              + "charset=iso-8859-1\">\n"
+            + "    <link href=\"http://www.torproject.org/stylesheet-ltr.";
+              + "css\" type=text/css rel=stylesheet>\n"
+            + "    <link href=\"http://www.torproject.org/favicon.ico\"; "
+              + "type=image/x-icon rel=\"shortcut icon\">\n"
+            + "  </head>\n"
+            + "  <body>\n"
+            + "    <div class=\"center\">\n"
+            + "      <table class=\"banner\" border=\"0\" "
+              + "cellpadding=\"0\" cellspacing=\"0\" summary=\"\">\n"
+            + "        <tr>\n"
+            + "          <td class=\"banner-left\"><a href=\"https://www.";
+              + "torproject.org/\"><img src=\"http://www.torproject.org/i";
+              + "mages/top-left.png\" alt=\"Click to go to home page\" "
+              + "width=\"193\" height=\"79\"></a></td>\n"
+            + "          <td class=\"banner-middle\">\n"
+            + "            <a href=\"/\">Home</a>\n"
+            + "            <a href=\"graphs.html\">Graphs</a>\n"
+            + "            <a href=\"reports.html\">Reports</a>\n"
+            + "            <a href=\"papers.html\">Papers</a>\n"
+            + "            <a href=\"data.html\">Data</a>\n"
+            + "            <a href=\"tools.html\">Tools</a>\n"
+            + "          </td>\n"
+            + "          <td class=\"banner-right\"></td>\n"
+            + "        </tr>\n"
+            + "      </table>\n"
+            + "      <div class=\"main-column\">\n"
+            + "        <h2>Tor Metrics Portal: Last execution logs</h2>\n"
+            + "        <br/>\n"
+            + "        <h3>Warnings</h3>\n"
+            + "        <br/>\n");
+        if (this.warnings.length() < 1) {
+          sb.append("        <p>(No messages.)</p>\n");
+        } else {
+          sb.append("        <table border=\"0\" cellpadding=\"0\" "
+              + "cellspacing=\"0\" summary=\"\">\n"
+              + "          <colgroup>\n"
+              + "            <col width=\"160\">\n"
+              + "            <col width=\"640\">\n"
+              + "          </colgroup>\n");
+          sb.append(warnings.toString());
+          sb.append("        </table>\n");
+        }
+
+        sb.append("        <br/>\n"
+            + "        <h3>Infos</h3>\n"
+            + "        <br/>\n");
+        if (this.infos.length() < 1) {
+          sb.append("<p>(No messages.)</p>\n");
+        } else {
+          sb.append("        <table border=\"0\" cellpadding=\"0\" "
+              + "cellspacing=\"0\" summary=\"\">\n"
+            + "          <colgroup>\n"
+            + "            <col width=\"160\">\n"
+            + "            <col width=\"640\">\n"
+            + "          </colgroup>\n");
+          sb.append(this.infos.toString());
+          sb.append("        </table>\n"
+              + "      </div>\n"
+              + "    </div>\n"
+              + "    <div class=\"bottom\" id=\"bottom\">\n"
+              + "      <p>\"Tor\" and the \"Onion Logo\" are <a "
+                + "href=\"https://www.torproject.org/trademark-faq.html.";
+                + "en\">"
+              + "registered trademarks</a> of The Tor Project, Inc.</p>\n"
+              + "    </div>\n"
+              + "  </body>\n"
+              + "</html>");
+        }
+        return sb.toString();
+      }
+    };
+    try {
+      FileHandler wh = new FileHandler("website/log.html");
+      wh.setFormatter(wf);
+      wh.setLevel(Level.INFO);
+      Logger.getLogger("").addHandler(wh);
+    } catch (SecurityException e) {
+      logger.log(Level.WARNING, "No permission to create website log "
+          + "file. Logging to website is disabled.", e);
+    } catch (IOException e) {
+      logger.log(Level.WARNING, "Could not write to website log file. "
+          + "Logging to website is disabled.", e);
+    }
+  }
+}
diff --git a/src/Main.java b/src/Main.java
index 03bede7..d1e4c6a 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -8,6 +8,9 @@ import java.util.logging.*;
 public class Main {
   public static void main(String[] args) {
 
+    /* Initialize logging configuration. */
+    new LoggingConfiguration();
+
     Logger logger = Logger.getLogger(Main.class.getName());
     logger.info("Starting ERNIE...");
 
-- 
1.6.5