tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r722064 - in /tomcat/trunk: java/org/apache/juli/OneLineFormatter.java webapps/docs/changelog.xml
Date Mon, 01 Dec 2008 13:15:39 GMT
Author: markt
Date: Mon Dec  1 05:15:37 2008
New Revision: 722064

URL: http://svn.apache.org/viewvc?rev=722064&view=rev
Log:
Add log formatter that outputs on a single line (except stack traces)

Added:
    tomcat/trunk/java/org/apache/juli/OneLineFormatter.java   (with props)
Modified:
    tomcat/trunk/webapps/docs/changelog.xml

Added: tomcat/trunk/java/org/apache/juli/OneLineFormatter.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/juli/OneLineFormatter.java?rev=722064&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/juli/OneLineFormatter.java (added)
+++ tomcat/trunk/java/org/apache/juli/OneLineFormatter.java Mon Dec  1 05:15:37 2008
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.juli;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.logging.Formatter;
+import java.util.logging.LogRecord;
+
+/**
+ * Provides same information as default log format but on a single line to make
+ * it easier to grep the logs. The only excpetion is stacktraces which are
+ * always preceeded by whitespace to make it simple to skip them.
+ */
+/*
+ * Date processing based on AccessLogValve.
+ */
+public class OneLineFormatter extends Formatter {
+
+    /**
+     * The set of month abbreviations for log messages.
+     */
+    private static final String months[] = {"Jan", "Feb", "Mar", "Apr", "May",
+        "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+    private static final String LINE_SEP = System.getProperty("line.separator");
+    private static final String ST_SEP = LINE_SEP + " ";
+
+    private final SimpleDateFormat dayFormatter = new SimpleDateFormat("dd");
+    private final SimpleDateFormat monthFormatter = new SimpleDateFormat("MM");
+    private final SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
+    private final SimpleDateFormat timeFormatter =
+        new SimpleDateFormat("hh:mm:ss");
+    
+    private Date currentDate;
+    private String currentDateString;
+
+    @Override
+    public String format(LogRecord record) {
+        StringBuffer sb = new StringBuffer();
+        
+        // Timestamp
+        addTimestamp(sb, new Date(record.getMillis()));
+        
+        // Severity
+        sb.append(' ');
+        sb.append(record.getLevel());
+        
+        // Source
+        sb.append(' ');
+        sb.append(record.getSourceClassName());
+        sb.append('.');
+        sb.append(record.getSourceMethodName());
+        
+        // Message
+        sb.append(' ');
+        sb.append(record.getMessage());
+        
+        // Stack trace
+        if (record.getThrown() != null) {
+            sb.append(ST_SEP);
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            record.getThrown().printStackTrace(pw);
+            pw.close();
+            sb.append(sw.getBuffer());
+        }
+
+        // New line for next record
+        sb.append(LINE_SEP);
+
+        return sb.toString();
+    }
+
+    public void addTimestamp(StringBuffer buf, Date date) {
+        if (currentDate != date) {
+            synchronized (this) {
+                if (currentDate != date) {
+                    StringBuffer current = new StringBuffer(32);
+                    current.append(dayFormatter.format(date)); // Day
+                    current.append('-');
+                    current.append(lookup(monthFormatter.format(date))); // Month
+                    current.append('-');
+                    current.append(yearFormatter.format(date)); // Year
+                    current.append(' ');
+                    current.append(timeFormatter.format(date)); // Time
+                    currentDateString = current.toString();
+                    currentDate = date;
+                }
+            }
+        }
+        buf.append(currentDateString);
+    }
+
+    /**
+     * Return the month abbreviation for the specified month, which must
+     * be a two-digit String.
+     *
+     * @param month Month number ("01" .. "12").
+     */
+    private String lookup(String month) {
+        int index;
+        try {
+            index = Integer.parseInt(month) - 1;
+        } catch (Throwable t) {
+            index = 0;  // Can not happen, in theory
+        }
+        return (months[index]);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/juli/OneLineFormatter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=722064&r1=722063&r2=722064&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Dec  1 05:15:37 2008
@@ -38,7 +38,7 @@
         David Jecks with modifications by Remy. (remm/fhanik)
       </update>
       <update>
-        <rev>620845</rev> and <rev>669119</rev>. Make shutdwn address
+        <rev>620845</rev> and <rev>669119</rev>. Make shutdown address
         configurable. (jfclere)
       </update>
       <fix>
@@ -90,6 +90,10 @@
       <add>
         Expose thisAccessedTime via Session interface. (rjung)
       </add>
+      <add>
+        Provide a log format for JULI that provides the same information as the
+        default but on a single line. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message