You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2012/10/16 09:46:49 UTC

svn commit: r1398686 - /sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java

Author: fmeschbe
Date: Tue Oct 16 07:46:48 2012
New Revision: 1398686

URL: http://svn.apache.org/viewvc?rev=1398686&view=rev
Log:
SLING-2622 Control framework logging sending the output through Sling's MainDelegate output formatting instead of leaving it up to the framework to format.
Also make sure log messages are not overlapping in the output if logging is called concurrently.

Modified:
    sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java

Modified: sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java?rev=1398686&r1=1398685&r2=1398686&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java (original)
+++ sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/app/MainDelegate.java Tue Oct 16 07:46:48 2012
@@ -33,7 +33,9 @@ import org.apache.sling.launchpad.base.i
 import org.apache.sling.launchpad.base.shared.Launcher;
 import org.apache.sling.launchpad.base.shared.Notifiable;
 import org.apache.sling.launchpad.base.shared.SharedConstants;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
 
 /**
  * The <code>Main</code> class is a simple Java Application which interprests
@@ -156,7 +158,7 @@ public class MainDelegate implements Lau
                 DEFAULT_LOG_LEVEL);
             commandLine.put(LOG_LEVEL_PROP, String.valueOf(logLevel));
         }
-        final Logger logger = new Logger();
+        final Logger logger = new SlingLogger();
 
         // Display port number on console, in case HttpService doesn't
         info("HTTP server port: " + commandLine.get(PROP_PORT), null);
@@ -320,11 +322,21 @@ public class MainDelegate implements Lau
         System.exit(code);
     }
 
+    // emit an debugging message to standard out
+    static void debug(String message, Throwable t) {
+        log(System.out, "*DEBUG*", message, t);
+    }
+
     // emit an informational message to standard out
     static void info(String message, Throwable t) {
         log(System.out, "*INFO *", message, t);
     }
 
+    // emit an warning message to standard out
+    static void warn(String message, Throwable t) {
+        log(System.out, "*WARN *", message, t);
+    }
+
     // emit an error message to standard err
     static void error(String message, Throwable t) {
         log(System.err, "*ERROR*", message, t);
@@ -347,19 +359,55 @@ public class MainDelegate implements Lau
         linePrefixBuilder.append("] ");
         final String linePrefix = linePrefixBuilder.toString();
 
-        out.print(linePrefix);
-        out.println(message);
-        if (t != null) {
-            t.printStackTrace(new PrintStream(out) {
-                @Override
-                public void println(String x) {
-                    synchronized (this) {
-                        print(linePrefix);
-                        super.println(x);
-                        flush();
+        synchronized (out) {
+            out.print(linePrefix);
+            out.println(message);
+            if (t != null) {
+                t.printStackTrace(new PrintStream(out) {
+                    @Override
+                    public void println(String x) {
+                        synchronized (this) {
+                            print(linePrefix);
+                            super.println(x);
+                            flush();
+                        }
                     }
-                }
-            });
+                });
+            }
+        }
+    }
+
+    private static class SlingLogger extends Logger {
+
+        @Override
+        protected void doLog(Bundle bundle, ServiceReference sr, int level, String msg, Throwable throwable) {
+
+            // unwind throwable if it is a BundleException
+            if ((throwable instanceof BundleException) && (((BundleException) throwable).getNestedException() != null)) {
+                throwable = ((BundleException) throwable).getNestedException();
+            }
+
+            String s = (sr == null) ? null : "SvcRef " + sr;
+            s = (s == null) ? null : s + " Bundle '" + bundle.getBundleId() + "'";
+            s = (s == null) ? msg : s + " " + msg;
+            s = (throwable == null) ? s : s + " (" + throwable + ")";
+
+            switch (level) {
+                case LOG_DEBUG:
+                    debug("DEBUG: " + s, null);
+                    break;
+                case LOG_INFO:
+                    info("INFO: " + s, null);
+                    break;
+                case LOG_WARNING:
+                    warn("WARNING: " + s, null);
+                    break;
+                case LOG_ERROR:
+                    error("ERROR: " + s, throwable);
+                    break;
+                default:
+                    warn("UNKNOWN[" + level + "]: " + s, null);
+            }
         }
     }
 }