Author: gnodet
Date: Tue Sep 18 12:28:06 2007
New Revision: 577021
URL: http://svn.apache.org/viewvc?rev=577021&view=rev
Log:
CXF-1033: Allow other logging framework to be used instead of java.util.logging
Added:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/Log4jLogger.java
Modified:
incubator/cxf/trunk/common/common/pom.xml
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
Modified: incubator/cxf/trunk/common/common/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/pom.xml?rev=577021&r1=577020&r2=577021&view=diff
==============================================================================
--- incubator/cxf/trunk/common/common/pom.xml (original)
+++ incubator/cxf/trunk/common/common/pom.xml Tue Sep 18 12:28:06 2007
@@ -113,6 +113,13 @@
</dependency>
<dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.14</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<scope>test</scope>
Added:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java?rev=577021&view=auto
==============================================================================
---
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
(added)
+++
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
Tue Sep 18 12:28:06 2007
@@ -0,0 +1,391 @@
+/**
+ * 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.cxf.common.logging;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+import java.util.logging.Filter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+/**
+ * java.util.logging.Logger implementation delegating to another framework.
+ * All methods can be used except:
+ * setLevel
+ * addHandler / getHandlers
+ * setParent / getParent
+ * setUseParentHandlers / getUseParentHandlers
+ *
+ * @author gnodet
+ */
+public abstract class AbstractDelegatingLogger extends Logger {
+
+ protected AbstractDelegatingLogger(String name, String resourceBundleName)
{
+ super(name, resourceBundleName);
+ }
+
+ public void log(LogRecord record) {
+ if (isLoggable(record.getLevel())) {
+ internalLog(record);
+ }
+ }
+
+ public void log(Level level, String msg) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ doLog(lr);
+ }
+ }
+
+ public void log(Level level, String msg, Object param1) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ Object params[] = {param1 };
+ lr.setParameters(params);
+ doLog(lr);
+ }
+ }
+
+ public void log(Level level, String msg, Object params[]) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setParameters(params);
+ doLog(lr);
+ }
+ }
+
+ public void log(Level level, String msg, Throwable thrown) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setThrown(thrown);
+ doLog(lr);
+ }
+ }
+
+ public void logp(Level level, String sourceClass, String sourceMethod,
String msg) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ doLog(lr);
+ }
+ }
+
+ public void logp(Level level, String sourceClass, String sourceMethod,
String msg, Object param1) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ Object params[] = {param1 };
+ lr.setParameters(params);
+ doLog(lr);
+ }
+ }
+
+ public void logp(Level level, String sourceClass, String sourceMethod,
String msg, Object params[]) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ lr.setParameters(params);
+ doLog(lr);
+ }
+ }
+
+ public void logp(Level level, String sourceClass, String sourceMethod,
String msg, Throwable thrown) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ lr.setThrown(thrown);
+ doLog(lr);
+ }
+ }
+
+ public void logrb(Level level, String sourceClass, String sourceMethod,
String bundleName, String msg) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ doLog(lr, bundleName);
+ }
+ }
+
+ public void logrb(Level level, String sourceClass, String sourceMethod,
+ String bundleName, String msg, Object param1) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ Object params[] = {param1 };
+ lr.setParameters(params);
+ doLog(lr, bundleName);
+ }
+ }
+
+ public void logrb(Level level, String sourceClass, String sourceMethod,
+ String bundleName, String msg, Object params[]) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ lr.setParameters(params);
+ doLog(lr, bundleName);
+ }
+ }
+
+ public void logrb(Level level, String sourceClass, String sourceMethod,
+ String bundleName, String msg, Throwable thrown) {
+ if (isLoggable(level)) {
+ LogRecord lr = new LogRecord(level, msg);
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ lr.setThrown(thrown);
+ doLog(lr, bundleName);
+ }
+ }
+
+ public void entering(String sourceClass, String sourceMethod) {
+ if (isLoggable(Level.FINER)) {
+ logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
+ }
+ }
+
+ public void entering(String sourceClass, String sourceMethod, Object
param1) {
+ if (isLoggable(Level.FINER)) {
+ Object params[] = {param1 };
+ logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
+ }
+ }
+
+ public void entering(String sourceClass, String sourceMethod, Object
params[]) {
+ if (isLoggable(Level.FINER)) {
+ String msg = "ENTRY";
+ if (params == null) {
+ logp(Level.FINER, sourceClass, sourceMethod, msg);
+ return;
+ }
+ for (int i = 0; i < params.length; i++) {
+ msg = msg + " {" + i + "}";
+ }
+ logp(Level.FINER, sourceClass, sourceMethod, msg, params);
+ }
+ }
+
+ public void exiting(String sourceClass, String sourceMethod) {
+ if (isLoggable(Level.FINER)) {
+ logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
+ }
+ }
+
+ public void exiting(String sourceClass, String sourceMethod, Object
result) {
+ if (isLoggable(Level.FINER)) {
+ Object params[] = {result };
+ logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", params);
+ }
+ }
+
+ public void throwing(String sourceClass, String sourceMethod, Throwable
thrown) {
+ if (isLoggable(Level.FINER)) {
+ LogRecord lr = new LogRecord(Level.FINER, "THROW");
+ lr.setSourceClassName(sourceClass);
+ lr.setSourceMethodName(sourceMethod);
+ lr.setThrown(thrown);
+ doLog(lr);
+ }
+ }
+
+ public void severe(String msg) {
+ if (isLoggable(Level.SEVERE)) {
+ LogRecord lr = new LogRecord(Level.SEVERE, msg);
+ doLog(lr);
+ }
+ }
+
+ public void warning(String msg) {
+ if (isLoggable(Level.WARNING)) {
+ LogRecord lr = new LogRecord(Level.WARNING, msg);
+ doLog(lr);
+ }
+ }
+
+ public void info(String msg) {
+ if (isLoggable(Level.INFO)) {
+ LogRecord lr = new LogRecord(Level.INFO, msg);
+ doLog(lr);
+ }
+ }
+
+ public void config(String msg) {
+ if (isLoggable(Level.CONFIG)) {
+ LogRecord lr = new LogRecord(Level.CONFIG, msg);
+ doLog(lr);
+ }
+ }
+
+ public void fine(String msg) {
+ if (isLoggable(Level.FINE)) {
+ LogRecord lr = new LogRecord(Level.FINE, msg);
+ doLog(lr);
+ }
+ }
+
+ public void finer(String msg) {
+ if (isLoggable(Level.FINER)) {
+ LogRecord lr = new LogRecord(Level.FINER, msg);
+ doLog(lr);
+ }
+ }
+
+ public void finest(String msg) {
+ if (isLoggable(Level.FINEST)) {
+ LogRecord lr = new LogRecord(Level.FINEST, msg);
+ doLog(lr);
+ }
+ }
+
+ public void setLevel(Level newLevel) throws SecurityException {
+ throw new UnsupportedOperationException();
+ }
+
+ public abstract Level getLevel();
+
+ public boolean isLoggable(Level level) {
+ Level l = getLevel();
+ return level.intValue() >= l.intValue() && l != Level.OFF;
+ }
+
+ public synchronized void addHandler(Handler handler) throws
SecurityException {
+ throw new UnsupportedOperationException();
+ }
+
+ public synchronized void removeHandler(Handler handler) throws
SecurityException {
+ throw new UnsupportedOperationException();
+ }
+
+ public synchronized Handler[] getHandlers() {
+ throw new UnsupportedOperationException();
+ }
+
+ public synchronized void setUseParentHandlers(boolean useParentHandlers) {
+ throw new UnsupportedOperationException();
+ }
+
+ public synchronized boolean getUseParentHandlers() {
+ throw new UnsupportedOperationException();
+ }
+
+ public Logger getParent() {
+ return null;
+ }
+
+ public void setParent(Logger parent) {
+ throw new UnsupportedOperationException();
+ }
+
+ protected void doLog(LogRecord lr) {
+ lr.setLoggerName(getName());
+ String rbname = getResourceBundleName();
+ if (rbname != null) {
+ lr.setResourceBundleName(rbname);
+ lr.setResourceBundle(getResourceBundle());
+ }
+ internalLog(lr);
+ }
+
+ protected void doLog(LogRecord lr, String rbname) {
+ lr.setLoggerName(getName());
+ if (rbname != null) {
+ lr.setResourceBundleName(rbname);
+ lr.setResourceBundle(loadResourceBundle(rbname));
+ }
+ internalLog(lr);
+ }
+
+ protected void internalLog(LogRecord record) {
+ Filter filter = getFilter();
+ if (filter != null && !filter.isLoggable(record)) {
+ return;
+ }
+ String msg = formatMessage(record);
+ internalLogFormatted(msg, record);
+ }
+
+ protected abstract void internalLogFormatted(String msg, LogRecord record);
+
+ protected String formatMessage(LogRecord record) {
+ String format = record.getMessage();
+ ResourceBundle catalog = record.getResourceBundle();
+ if (catalog != null) {
+ try {
+ format = catalog.getString(record.getMessage());
+ } catch (MissingResourceException ex) {
+ format = record.getMessage();
+ }
+ }
+ try {
+ Object parameters[] = record.getParameters();
+ if (parameters == null || parameters.length == 0) {
+ return format;
+ }
+ if (format.indexOf("{0") >= 0 || format.indexOf("{1") >= 0
+ || format.indexOf("{2") >= 0 || format.indexOf("{3")
>= 0) {
+ return java.text.MessageFormat.format(format, parameters);
+ }
+ return format;
+ } catch (Exception ex) {
+ return format;
+ }
+ }
+
+ /**
+ * Load the specified resource bundle
+ *
+ * @param resourceBundleName
+ * the name of the resource bundle to load, cannot be null
+ * @return the loaded resource bundle.
+ * @throws java.util.MissingResourceException
+ * If the specified resource bundle can not be loaded.
+ */
+ static ResourceBundle loadResourceBundle(String resourceBundleName) {
+ // try context class loader to load the resource
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ if (null != cl) {
+ try {
+ return ResourceBundle.getBundle(resourceBundleName,
Locale.getDefault(), cl);
+ } catch (MissingResourceException e) {
+ // Failed to load using context classloader, ignore
+ }
+ }
+ // try system class loader to load the resource
+ cl = ClassLoader.getSystemClassLoader();
+ if (null != cl) {
+ try {
+ return ResourceBundle.getBundle(resourceBundleName,
Locale.getDefault(), cl);
+ } catch (MissingResourceException e) {
+ // Failed to load using system classloader, ignore
+ }
+ }
+ return null;
+ }
+
+}
\ No newline at end of file
Added:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/Log4jLogger.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/Log4jLogger.java?rev=577021&view=auto
==============================================================================
---
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/Log4jLogger.java
(added)
+++
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/Log4jLogger.java
Tue Sep 18 12:28:06 2007
@@ -0,0 +1,87 @@
+/**
+ * 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.cxf.common.logging;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * java.util.logging.Logger implementation delegating to Log4j.
+ * All methods can be used except:
+ * setLevel
+ * addHandler / getHandlers
+ * setParent / getParent
+ * setUseParentHandlers / getUseParentHandlers
+ *
+ * @author gnodet
+ */
+public class Log4jLogger extends AbstractDelegatingLogger {
+
+ private static final Map<Level, org.apache.log4j.Level> TO_LOG4J =
+ new HashMap<Level,
org.apache.log4j.Level>();
+ private static final Map<org.apache.log4j.Level, Level> FROM_LOG4J =
+ new
HashMap<org.apache.log4j.Level, Level>();
+
+ private org.apache.log4j.Logger log;
+
+
+ static {
+ TO_LOG4J.put(Level.ALL, org.apache.log4j.Level.ALL);
+ TO_LOG4J.put(Level.SEVERE, org.apache.log4j.Level.ERROR);
+ TO_LOG4J.put(Level.WARNING, org.apache.log4j.Level.WARN);
+ TO_LOG4J.put(Level.INFO, org.apache.log4j.Level.INFO);
+ TO_LOG4J.put(Level.CONFIG, org.apache.log4j.Level.DEBUG);
+ TO_LOG4J.put(Level.FINE, org.apache.log4j.Level.DEBUG);
+ TO_LOG4J.put(Level.FINER, org.apache.log4j.Level.TRACE);
+ TO_LOG4J.put(Level.FINEST, org.apache.log4j.Level.TRACE);
+ TO_LOG4J.put(Level.OFF, org.apache.log4j.Level.OFF);
+ FROM_LOG4J.put(org.apache.log4j.Level.ALL, Level.ALL);
+ FROM_LOG4J.put(org.apache.log4j.Level.ERROR, Level.SEVERE);
+ FROM_LOG4J.put(org.apache.log4j.Level.WARN, Level.WARNING);
+ FROM_LOG4J.put(org.apache.log4j.Level.INFO, Level.INFO);
+ FROM_LOG4J.put(org.apache.log4j.Level.DEBUG, Level.FINE);
+ FROM_LOG4J.put(org.apache.log4j.Level.TRACE, Level.FINEST);
+ FROM_LOG4J.put(org.apache.log4j.Level.OFF, Level.OFF);
+ }
+
+ public Log4jLogger(String name, String resourceBundleName) {
+ super(name, resourceBundleName);
+ log = org.apache.log4j.LogManager.getLogger(name);
+ }
+
+ public Level getLevel() {
+ for (org.apache.log4j.Category c = log; c != null; c = c.getParent()) {
+ org.apache.log4j.Level l = c.getLevel();
+ if (l != null) {
+ return FROM_LOG4J.get(l);
+ }
+ }
+ return null;
+ }
+
+ protected void internalLogFormatted(String msg, LogRecord record) {
+ log.log(AbstractDelegatingLogger.class.getName(),
+ TO_LOG4J.get(record.getLevel()),
+ msg,
+ record.getThrown());
+ }
+
+}
Modified:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java?rev=577021&r1=577020&r2=577021&view=diff
==============================================================================
---
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
(original)
+++
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
Tue Sep 18 12:28:06 2007
@@ -19,6 +19,7 @@
package org.apache.cxf.common.logging;
+import java.lang.reflect.Constructor;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
@@ -35,6 +36,8 @@
public final class LogUtils {
private static final Object[] NO_PARAMETERS = new Object[0];
+
+ private static Class<?> loggerClass;
/**
* Prevents instantiation.
@@ -43,17 +46,20 @@
}
/**
+ * Enable users to use their own logger implementation.
+ */
+ public static void setLoggerClass(Class<?> cls) {
+ loggerClass = cls;
+ }
+
+ /**
* Get a Logger with the associated default resource bundle for the class.
*
* @param cls the Class to contain the Logger
* @return an appropriate Logger
*/
public static Logger getL7dLogger(Class<?> cls) {
- try {
- return Logger.getLogger(cls.getName(),
BundleUtils.getBundleName(cls));
- } catch (MissingResourceException rex) {
- return Logger.getLogger(cls.getName());
- }
+ return createLogger(cls, null);
}
/**
@@ -64,7 +70,38 @@
* @return an appropriate Logger
*/
public static Logger getL7dLogger(Class<?> cls, String name) {
- return Logger.getLogger(cls.getName(), BundleUtils.getBundleName(cls,
name));
+ return createLogger(cls, name);
+ }
+
+ /**
+ * Create a logger
+ */
+ protected static Logger createLogger(Class<?> cls, String name) {
+ if (loggerClass != null) {
+ try {
+ Constructor cns = loggerClass.getConstructor(String.class,
String.class);
+ if (name == null) {
+ try {
+ return (Logger) cns.newInstance(cls.getName(),
BundleUtils.getBundleName(cls));
+ } catch (MissingResourceException rex) {
+ return (Logger) cns.newInstance(cls.getName(), null);
+ }
+ } else {
+ return (Logger) cns.newInstance(cls.getName(),
BundleUtils.getBundleName(cls, name));
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ if (name == null) {
+ try {
+ return Logger.getLogger(cls.getName(),
BundleUtils.getBundleName(cls));
+ } catch (MissingResourceException rex) {
+ return Logger.getLogger(cls.getName(), null);
+ }
+ } else {
+ return Logger.getLogger(cls.getName(),
BundleUtils.getBundleName(cls, name));
+ }
}
/**