[jboss-svn-commits] JBoss Common SVN: r3086 - jboss-logmanager/trunk/src/main/java/org/jboss/logmanager.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Mar 16 22:02:01 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-03-16 22:02:01 -0400 (Mon, 16 Mar 2009)
New Revision: 3086

Added:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/AbstractLoggingWriter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java
Modified:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LogContext.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggingWriter.java
Log:
JBAS-2412 - separate logger output for stdout/stderr for each deployment (this solution is actually for each logging context, and does not generally solve the problem of redirecting stdout/err)

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/AbstractLoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/AbstractLoggingWriter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/AbstractLoggingWriter.java	2009-03-17 02:02:01 UTC (rev 3086)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.io.Writer;
+import java.io.IOException;
+
+/**
+ * Abstract base class for writers which log to a logger.
+ */
+public abstract class AbstractLoggingWriter extends Writer {
+
+    private final StringBuilder buffer = new StringBuilder();
+
+    /**
+     * Construct a new instance.
+     */
+    protected AbstractLoggingWriter() {
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void write(final int c) throws IOException {
+        final java.util.logging.Logger logger = getLogger();
+        if (logger == null) {
+            return;
+        }
+        synchronized (buffer) {
+            if (c == '\n') {
+                logger.log(getLevel(), buffer.toString());
+                buffer.setLength(0);
+            } else {
+                buffer.append((char) c);
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void write(final char[] cbuf, final int off, final int len) throws IOException {
+        final java.util.logging.Logger logger = getLogger();
+        if (logger == null) {
+            return;
+        }
+        synchronized (buffer) {
+            int mark = 0;
+            int i;
+            for (i = 0; i < len; i++) {
+                final char c = cbuf[off + i];
+                if (c == '\n') {
+                    buffer.append(cbuf, mark + off, i - mark);
+                    logger.log(getLevel(), buffer.toString());
+                    buffer.setLength(0);
+                    mark = i + 1;
+                }
+            }
+            buffer.append(cbuf, mark + off, i - mark);
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void flush() throws IOException {
+        final java.util.logging.Logger logger = getLogger();
+        if (logger == null) {
+            return;
+        }
+        synchronized (buffer) {
+            if (buffer.length() > 0) {
+                buffer.append(" >>> FLUSH");
+                logger.log(getLevel(), buffer.toString());
+                buffer.setLength(0);
+            }
+        }
+    }
+
+    /**
+     * Get the logger to use.
+     *
+     * @return the logger
+     */
+    protected abstract java.util.logging.Logger getLogger();
+
+    /**
+     * Get the level at which to log.
+     *
+     * @return the level
+     */
+    protected abstract java.util.logging.Level getLevel();
+
+    /** {@inheritDoc} */
+    public void close() throws IOException {
+        // ignore
+    }
+}

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LogContext.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LogContext.java	2009-03-16 23:13:28 UTC (rev 3085)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LogContext.java	2009-03-17 02:02:01 UTC (rev 3086)
@@ -41,8 +41,14 @@
     @SuppressWarnings({ "ThisEscapedInObjectConstruction" })
     private final LoggerNode rootLogger = new LoggerNode(this);
 
+    /**
+     * This lock is taken any time a change is made which affects multiple nodes in the hierarchy.
+     */
     final Lock treeLock = new ReentrantLock(false);
 
+    private volatile Logger stdoutLogger;
+    private volatile Logger stderrLogger;
+
     LogContext() {
     }
 
@@ -115,4 +121,40 @@
             sm.checkPermission(CONTROL_PERMISSION);
         }
     }
+
+    /**
+     * Get the logger for standard output.
+     *
+     * @return the current logger, or {@code null} if none is configured
+     */
+    public Logger getStdoutLogger() {
+        return stdoutLogger;
+    }
+
+    /**
+     * Set the logger for standard output, or null to suppress for this context.
+     *
+     * @param stdoutLogger the new logger
+     */
+    public void setStdoutLogger(final Logger stdoutLogger) {
+        this.stdoutLogger = stdoutLogger;
+    }
+
+    /**
+     * Get the logger for standard error.
+     *
+     * @return the current logger, or {@code null} if none is configured
+     */
+    public Logger getStderrLogger() {
+        return stderrLogger;
+    }
+
+    /**
+     * Set the logger for standard error, or null to suppress for this context.
+     *
+     * @param stderrLogger the new logger
+     */
+    public void setStderrLogger(final Logger stderrLogger) {
+        this.stderrLogger = stderrLogger;
+    }
 }

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggingWriter.java	2009-03-16 23:13:28 UTC (rev 3085)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/LoggingWriter.java	2009-03-17 02:02:01 UTC (rev 3086)
@@ -22,20 +22,17 @@
 
 package org.jboss.logmanager;
 
-import java.io.Writer;
-import java.io.IOException;
-
 import java.util.logging.Logger;
+import java.util.logging.Level;
 
 /**
  * A writer which sends its data to a logger.
  */
-public final class LoggingWriter extends Writer {
+public final class LoggingWriter extends AbstractLoggingWriter {
 
     @SuppressWarnings({ "NonConstantLogger" })
     private final Logger log;
-    private final java.util.logging.Level level;
-    private final StringBuilder buffer = new StringBuilder();
+    private final Level level;
 
     /**
      * Construct a new instance.
@@ -43,55 +40,18 @@
      * @param category the log category to use
      * @param level the level at which to log messages
      */
-    public LoggingWriter(final String category, final java.util.logging.Level level) {
+    public LoggingWriter(final String category, final Level level) {
         this.level = level;
         log = Logger.getLogger(category);
     }
 
     /** {@inheritDoc} */
-    @Override
-    public void write(final int c) throws IOException {
-        synchronized (buffer) {
-            if (c == '\n') {
-                log.log(level, buffer.toString());
-                buffer.setLength(0);
-            } else {
-                buffer.append((char) c);
-            }
-        }
+    protected Logger getLogger() {
+        return log;
     }
 
     /** {@inheritDoc} */
-    public void write(final char[] cbuf, final int off, final int len) throws IOException {
-        synchronized (buffer) {
-            int mark = 0;
-            int i;
-            for (i = 0; i < len; i++) {
-                final char c = cbuf[off + i];
-                if (c == '\n') {
-                    buffer.append(cbuf, mark + off, i - mark);
-                    log.log(level, buffer.toString());
-                    buffer.setLength(0);
-                    mark = i + 1;
-                }
-            }
-            buffer.append(cbuf, mark + off, i - mark);
-        }
+    protected Level getLevel() {
+        return level;
     }
-
-    /** {@inheritDoc} */
-    public void flush() throws IOException {
-        synchronized (buffer) {
-            if (buffer.length() > 0) {
-                buffer.append(" >>> FLUSH");
-                log.log(level, buffer.toString());
-                buffer.setLength(0);
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    public void close() throws IOException {
-        // ignore
-    }
 }

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemErrLoggingWriter.java	2009-03-17 02:02:01 UTC (rev 3086)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ * A writer which writes to the current logging context's standard error logger.
+ */
+public final class SystemErrLoggingWriter extends AbstractLoggingWriter {
+
+    /**
+     * Construct a new instance.
+     */
+    public SystemErrLoggingWriter() {
+    }
+
+    /** {@inheritDoc} */
+    protected Logger getLogger() {
+        return LogContext.getLogContext().getStderrLogger();
+    }
+
+    /** {@inheritDoc} */
+    protected Level getLevel() {
+        return Level.WARNING;
+    }
+}
\ No newline at end of file

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/SystemOutLoggingWriter.java	2009-03-17 02:02:01 UTC (rev 3086)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager;
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ * A writer which writes to the current logging context's standard output logger.
+ */
+public final class SystemOutLoggingWriter extends AbstractLoggingWriter {
+
+    /**
+     * Construct a new instance.
+     */
+    public SystemOutLoggingWriter() {
+    }
+
+    /** {@inheritDoc} */
+    protected Logger getLogger() {
+        return LogContext.getLogContext().getStdoutLogger();
+    }
+
+    /** {@inheritDoc} */
+    protected Level getLevel() {
+        return Level.INFO;
+    }
+}




More information about the jboss-svn-commits mailing list