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

[or-cvs] [ernie/master] Don't delete last logs on website before writing new ones.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Tue, 13 Apr 2010 22:22:13 +0200
Subject: Don't delete last logs on website before writing new ones.
Commit: 750c6766519a72fd8bd9a3c68acd2a80fba93a33

---
 src/LoggingConfiguration.java |  216 ++++++++++++++++++++++-------------------
 1 files changed, 118 insertions(+), 98 deletions(-)

diff --git a/src/LoggingConfiguration.java b/src/LoggingConfiguration.java
index 88359f4..e4758e9 100644
--- a/src/LoggingConfiguration.java
+++ b/src/LoggingConfiguration.java
@@ -1,6 +1,7 @@
 import java.io.*;
 import java.text.*;
 import java.util.Date;
+import java.util.TimeZone;
 import java.util.logging.*;
 /**
  * Initialize logging configuration.
@@ -30,10 +31,11 @@ public class LoggingConfiguration {
     /* Disable logging of internal Sun classes. */
     Logger.getLogger("sun").setLevel(Level.OFF);
 
-    /* Create console logger to write messages on WARNING or higher to the
+    /* Create log handler that writes messages on WARNING or higher to the
      * console. */
     final SimpleDateFormat dateTimeFormat =
         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
     Formatter cf = new Formatter() {
       public String format(LogRecord record) {
         return dateTimeFormat.format(new Date(record.getMillis())) + " "
@@ -49,7 +51,7 @@ public class LoggingConfiguration {
     Logger logger = Logger.getLogger(
         LoggingConfiguration.class.getName());
 
-    /* Create file logger that writes all messages on FINE or higher to a
+    /* Create log handler that writes all messages on FINE or higher to a
      * local file. */
     Formatter ff = new Formatter() {
       public String format(LogRecord record) {
@@ -72,119 +74,137 @@ public class LoggingConfiguration {
           + "file is disabled.", e);
     }
 
-    /* Initialize website logger that writes all message on INFO or higher
-     * to a website. */
-    Formatter wf = new Formatter() {
+    /* Create log handler that writes messages on INFO or higher to a
+     * local HTML file for display on the website. */
+    Handler wh = new Handler() {
       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().replaceAll("\n", "<br/>")
-            + "</td>\n"
-            + "          </tr>\n";
-        if (record.getLevel().equals(Level.INFO)) {
-          this.infos.append(msg);
-        } else {
-          this.warnings.append(msg);
+      public void close() {
+        if (this.infos == null || this.warnings == null) {
+          return;
         }
-        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\" "
+        try {
+          BufferedWriter bw = new BufferedWriter(
+              new FileWriter("website/log.html"));
+          bw.write("<!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/images/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"
+              + "        <p>This page shows the warnings and info logs "
+                + "of the last program execution. All timestamps are in "
+                + "UTC.</p>\n"
+              + "        <br/>\n"
+              + "        <h3>Warnings</h3>\n"
+              + "        <br/>\n"
+              + "        <p><i>Warning messages indicate that a "
+                + "potential problem has occurred that requires the "
+                + "operator to look after the otherwise unattended "
+                + "setup.</i></p>\n"
+              + "        <br/>\n"
+              + "        <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"
+          if (this.warnings.length() < 1) {
+            bw.write("          <tr><td>(No messages.)</td><td/></tr>\n");
+          } else {
+            bw.write(warnings.toString());
+          }
+          bw.write("        </table>\n"
+              + "        <br/>\n"
+              + "        <br/>\n"
+              + "        <h3>Infos</h3>\n"
+              + "        <br/>\n"
+              + "        <p><i>Info messages are meant to help the "
+                + "operator in making sure that operation works as "
+                + "expected.</i></p>\n"
+              + "        <br/>\n"
+              + "        <table border=\"0\" cellpadding=\"0\" "
+                + "cellspacing=\"0\" summary=\"\">\n"
+              + "          <colgroup>\n"
+              + "            <col width=\"160\">\n"
+              + "            <col width=\"640\">\n"
+              + "          </colgroup>\n");
+          if (this.infos.length() < 1) {
+            bw.write("          <tr><td>(No messages.)</td><td/></tr>\n");
+          } else {
+            bw.write(this.infos.toString());
+          }
+          bw.write("        </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"
+                + "href=\"https://www.torproject.org/trademark-faq.html";
+                + ".en\">"
+              + "registered trademarks</a> of The Tor Project, "
+                + "Inc.</p>\n"
               + "    </div>\n"
               + "  </body>\n"
               + "</html>");
+          bw.close();
+          this.infos = null;
+          this.warnings = null;
+        } catch (IOException e) {
+        }
+      }
+      public void flush() {
+      }
+      public void publish(LogRecord record) {
+        if (this.infos == null || this.warnings == null) {
+          return;
+        }
+        String logMessage = "          <tr>\n"
+            + "            <td>"
+            + dateTimeFormat.format(new Date(record.getMillis()))
+            + "</td>\n"
+            + "            <td>"
+            + record.getMessage().replaceAll("\n", "<br/>")
+            + "</td>\n"
+            + "          </tr>\n";
+        if (record.getLevel().equals(Level.INFO)) {
+          this.infos.append(logMessage);
+        } else {
+          this.warnings.append(logMessage);
         }
-        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);
-    }
+    wh.setLevel(Level.INFO);
+    Logger.getLogger("").addHandler(wh);
   }
 }
-- 
1.6.5