From 40ce9194885103488f5da3961bcfcae466039b4a Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 19 Jun 2009 09:31:38 +0000 Subject: [PATCH] * Added InternalLogLevel * Added InternalLogger.log() and isEnabled() --- .../netty/logging/AbstractInternalLogger.java | 95 +++++++++++++++++++ .../jboss/netty/logging/CommonsLogger.java | 2 +- .../jboss/netty/logging/InternalLogLevel.java | 49 ++++++++++ .../jboss/netty/logging/InternalLogger.java | 15 +++ .../netty/logging/InternalLoggerFactory.java | 13 +++ .../org/jboss/netty/logging/JBossLogger.java | 2 +- .../org/jboss/netty/logging/JdkLogger.java | 2 +- .../org/jboss/netty/logging/Log4JLogger.java | 2 +- .../org/jboss/netty/logging/OsgiLogger.java | 2 +- .../org/jboss/netty/logging/Slf4JLogger.java | 2 +- 10 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/jboss/netty/logging/AbstractInternalLogger.java create mode 100644 src/main/java/org/jboss/netty/logging/InternalLogLevel.java diff --git a/src/main/java/org/jboss/netty/logging/AbstractInternalLogger.java b/src/main/java/org/jboss/netty/logging/AbstractInternalLogger.java new file mode 100644 index 0000000000..b27b8394e0 --- /dev/null +++ b/src/main/java/org/jboss/netty/logging/AbstractInternalLogger.java @@ -0,0 +1,95 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt 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.netty.logging; + +/** + * A skeletal implementation of {@link InternalLogger}. This class implements + * all methods that have a {@link InternalLogLevel} parameter by default to call + * specific logger methods such as {@link #info(String)} or {@link #isInfoEnabled()}. + * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public abstract class AbstractInternalLogger implements InternalLogger { + + /** + * Creates a new instance. + */ + protected AbstractInternalLogger() { + super(); + } + + public boolean isEnabled(InternalLogLevel level) { + switch (level) { + case DEBUG: + return isDebugEnabled(); + case INFO: + return isInfoEnabled(); + case WARN: + return isWarnEnabled(); + case ERROR: + return isErrorEnabled(); + default: + throw new Error(); + } + } + + public void log(InternalLogLevel level, String msg, Throwable cause) { + switch (level) { + case DEBUG: + debug(msg, cause); + break; + case INFO: + info(msg, cause); + break; + case WARN: + warn(msg, cause); + break; + case ERROR: + error(msg, cause); + break; + default: + throw new Error(); + } + } + + public void log(InternalLogLevel level, String msg) { + switch (level) { + case DEBUG: + debug(msg); + break; + case INFO: + info(msg); + break; + case WARN: + warn(msg); + break; + case ERROR: + error(msg); + break; + default: + throw new Error(); + } + } +} diff --git a/src/main/java/org/jboss/netty/logging/CommonsLogger.java b/src/main/java/org/jboss/netty/logging/CommonsLogger.java index 4b42cc0d12..819d16aa3d 100644 --- a/src/main/java/org/jboss/netty/logging/CommonsLogger.java +++ b/src/main/java/org/jboss/netty/logging/CommonsLogger.java @@ -34,7 +34,7 @@ import org.apache.commons.logging.Log; * @version $Rev$, $Date$ * */ -class CommonsLogger implements InternalLogger { +class CommonsLogger extends AbstractInternalLogger { private final Log logger; private final String loggerName; diff --git a/src/main/java/org/jboss/netty/logging/InternalLogLevel.java b/src/main/java/org/jboss/netty/logging/InternalLogLevel.java new file mode 100644 index 0000000000..6d97776693 --- /dev/null +++ b/src/main/java/org/jboss/netty/logging/InternalLogLevel.java @@ -0,0 +1,49 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt 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.netty.logging; + +/** + * Represents the log level that {@link InternalLogger} can log at. + * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public enum InternalLogLevel { + /** + * 'DEBUG' log level. + */ + DEBUG, + /** + * 'INFO' log level. + */ + INFO, + /** + * 'WARN' log level. + */ + WARN, + /** + * 'ERROR' log level. + */ + ERROR; +} diff --git a/src/main/java/org/jboss/netty/logging/InternalLogger.java b/src/main/java/org/jboss/netty/logging/InternalLogger.java index 9b3742bd24..ee748c728a 100644 --- a/src/main/java/org/jboss/netty/logging/InternalLogger.java +++ b/src/main/java/org/jboss/netty/logging/InternalLogger.java @@ -52,6 +52,11 @@ public interface InternalLogger { */ boolean isErrorEnabled(); + /** + * Returns {@code true} if the specified log level message is logged. + */ + boolean isEnabled(InternalLogLevel level); + /** * Logs a DEBUG level message. */ @@ -91,4 +96,14 @@ public interface InternalLogger { * Logs an ERROR level message. */ void error(String msg, Throwable cause); + + /** + * Logs a message. + */ + void log(InternalLogLevel level, String msg); + + /** + * Logs a message. + */ + void log(InternalLogLevel level, String msg, Throwable cause); } diff --git a/src/main/java/org/jboss/netty/logging/InternalLoggerFactory.java b/src/main/java/org/jboss/netty/logging/InternalLoggerFactory.java index ca2655e13a..19796303ee 100644 --- a/src/main/java/org/jboss/netty/logging/InternalLoggerFactory.java +++ b/src/main/java/org/jboss/netty/logging/InternalLoggerFactory.java @@ -139,6 +139,19 @@ public abstract class InternalLoggerFactory { StackTraceSimplifier.simplify(cause); logger.warn(msg, cause); } + + public boolean isEnabled(InternalLogLevel level) { + return logger.isEnabled(level); + } + + public void log(InternalLogLevel level, String msg) { + logger.log(level, msg); + } + + public void log(InternalLogLevel level, String msg, Throwable cause) { + StackTraceSimplifier.simplify(cause); + logger.log(level, msg, cause); + } }; } diff --git a/src/main/java/org/jboss/netty/logging/JBossLogger.java b/src/main/java/org/jboss/netty/logging/JBossLogger.java index a49b36ca31..536c989e30 100644 --- a/src/main/java/org/jboss/netty/logging/JBossLogger.java +++ b/src/main/java/org/jboss/netty/logging/JBossLogger.java @@ -34,7 +34,7 @@ import org.jboss.logging.Logger; * @version $Rev$, $Date$ * */ -class JBossLogger implements InternalLogger { +class JBossLogger extends AbstractInternalLogger { private final Logger logger; diff --git a/src/main/java/org/jboss/netty/logging/JdkLogger.java b/src/main/java/org/jboss/netty/logging/JdkLogger.java index f53ac4c156..131b8fe9d0 100644 --- a/src/main/java/org/jboss/netty/logging/JdkLogger.java +++ b/src/main/java/org/jboss/netty/logging/JdkLogger.java @@ -35,7 +35,7 @@ import java.util.logging.Logger; * @version $Rev$, $Date$ * */ -class JdkLogger implements InternalLogger { +class JdkLogger extends AbstractInternalLogger { private final Logger logger; private final String loggerName; diff --git a/src/main/java/org/jboss/netty/logging/Log4JLogger.java b/src/main/java/org/jboss/netty/logging/Log4JLogger.java index 8e8fc5a4a0..e076c8e68d 100644 --- a/src/main/java/org/jboss/netty/logging/Log4JLogger.java +++ b/src/main/java/org/jboss/netty/logging/Log4JLogger.java @@ -34,7 +34,7 @@ import org.apache.log4j.Logger; * @version $Rev$, $Date$ * */ -class Log4JLogger implements InternalLogger { +class Log4JLogger extends AbstractInternalLogger { private final Logger logger; diff --git a/src/main/java/org/jboss/netty/logging/OsgiLogger.java b/src/main/java/org/jboss/netty/logging/OsgiLogger.java index ad418b5bfd..47badbaf54 100644 --- a/src/main/java/org/jboss/netty/logging/OsgiLogger.java +++ b/src/main/java/org/jboss/netty/logging/OsgiLogger.java @@ -33,7 +33,7 @@ import org.osgi.service.log.LogService; * @version $Rev$, $Date$ * */ -class OsgiLogger implements InternalLogger { +class OsgiLogger extends AbstractInternalLogger { private final OsgiLoggerFactory parent; private final InternalLogger fallback; diff --git a/src/main/java/org/jboss/netty/logging/Slf4JLogger.java b/src/main/java/org/jboss/netty/logging/Slf4JLogger.java index 2675b79155..fa1bf0dff4 100644 --- a/src/main/java/org/jboss/netty/logging/Slf4JLogger.java +++ b/src/main/java/org/jboss/netty/logging/Slf4JLogger.java @@ -33,7 +33,7 @@ import org.slf4j.Logger; * @version $Rev$, $Date$ * */ -class Slf4JLogger implements InternalLogger { +class Slf4JLogger extends AbstractInternalLogger { private final Logger logger;