001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.genesis.logging; 021 022 import java.io.Serializable; 023 import java.lang.reflect.Constructor; 024 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.impl.SimpleLog; 027 028 /** 029 * Jakarta Commons Logging Log which delegates to another Log type. 030 * 031 * @version $Rev: 470159 $ $Date: 2006-11-01 17:19:16 -0800 (Wed, 01 Nov 2006) $ 032 */ 033 public class DelegatingLog 034 implements Log, Serializable 035 { 036 private static Constructor factory; 037 038 public static void setDelegateType(final Class type) { 039 assert type != null; 040 041 try { 042 factory = type.getConstructor(new Class[] { String.class }); 043 } 044 catch (NoSuchMethodException e) { 045 throw new RuntimeException("Failed to lookup (String) constructor for type: " + type, e); 046 } 047 } 048 049 public static Log getDelegate(final String name) { 050 // If no factory is set, then use a SimpleLog... so logging always works 051 if (factory == null) { 052 return new SimpleLog(name); 053 } 054 055 try { 056 return (Log) factory.newInstance(new Object[] { name }); 057 } 058 catch (Exception e) { 059 throw new RuntimeException("Failed to construct delegate logger using factory: " + factory, e); 060 } 061 } 062 063 private String name; 064 065 public DelegatingLog(final String name) { 066 assert name != null; 067 068 this.name = name; 069 } 070 071 private Log getLog() { 072 return getDelegate(name); 073 } 074 075 public boolean isDebugEnabled() { 076 return getLog().isDebugEnabled(); 077 } 078 079 public boolean isErrorEnabled() { 080 return getLog().isErrorEnabled(); 081 } 082 083 public boolean isFatalEnabled() { 084 return getLog().isErrorEnabled(); 085 } 086 087 public boolean isInfoEnabled() { 088 return getLog().isInfoEnabled(); 089 } 090 091 public boolean isTraceEnabled() { 092 return getLog().isTraceEnabled(); 093 } 094 095 public boolean isWarnEnabled() { 096 return getLog().isWarnEnabled(); 097 } 098 099 private String createMessage(final Object object) { 100 return String.valueOf(object); 101 } 102 103 public void trace(final Object object) { 104 getLog().trace(object); 105 } 106 107 public void trace(final Object object, final Throwable throwable) { 108 getLog().trace(object, throwable); 109 } 110 111 public void debug(final Object object) { 112 getLog().debug(createMessage(object)); 113 } 114 115 public void debug(final Object object, final Throwable throwable) { 116 getLog().debug(createMessage(object), throwable); 117 } 118 119 public void info(final Object object) { 120 getLog().info(createMessage(object)); 121 } 122 123 public void info(final Object object, final Throwable throwable) { 124 getLog().info(createMessage(object), throwable); 125 } 126 127 public void warn(final Object object) { 128 getLog().warn(createMessage(object)); 129 } 130 131 public void warn(final Object object, final Throwable throwable) { 132 getLog().warn(createMessage(object), throwable); 133 } 134 135 public void error(final Object object) { 136 getLog().error(createMessage(object)); 137 } 138 139 public void error(final Object object, final Throwable throwable) { 140 getLog().error(createMessage(object), throwable); 141 } 142 143 public void fatal(final Object object) { 144 error(object); 145 } 146 147 public void fatal(final Object object, final Throwable throwable) { 148 error(object, throwable); 149 } 150 }