001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.logging.impl; 019 020import java.io.Serializable; 021 022import org.apache.commons.logging.Log; 023import org.apache.log.Hierarchy; 024import org.apache.log.Logger; 025 026/** 027 * Implementation of {@code org.apache.commons.logging.Log} 028 * that wraps the <a href="https://avalon.apache.org/logkit/">avalon-logkit</a> 029 * logging system. Configuration of {@code LogKit} is left to the user. 030 * <p> 031 * {@code LogKit} accepts only {@code String} messages. 032 * Therefore, this implementation converts object messages into strings 033 * by called their {@code toString()} method before logging them. 034 * 035 * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued. 036 */ 037@Deprecated 038public class LogKitLogger implements Log, Serializable { 039 040 /** Serializable version identifier. */ 041 private static final long serialVersionUID = 3768538055836059519L; 042 043 /** Logging goes to this {@code LogKit} logger */ 044 protected transient volatile Logger logger; 045 046 /** Name of this logger */ 047 protected String name; 048 049 /** 050 * Constructs {@code LogKitLogger} which wraps the {@code LogKit} 051 * logger with given name. 052 * 053 * @param name log name 054 */ 055 public LogKitLogger(final String name) { 056 this.name = name; 057 this.logger = getLogger(); 058 } 059 060 /** 061 * Logs a message with {@code org.apache.log.Priority.DEBUG}. 062 * 063 * @param message to log 064 * @see org.apache.commons.logging.Log#debug(Object) 065 */ 066 @Override 067 public void debug(final Object message) { 068 if (message != null) { 069 getLogger().debug(String.valueOf(message)); 070 } 071 } 072 073 /** 074 * Logs a message with {@code org.apache.log.Priority.DEBUG}. 075 * 076 * @param message to log 077 * @param t log this cause 078 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 079 */ 080 @Override 081 public void debug(final Object message, final Throwable t) { 082 if (message != null) { 083 getLogger().debug(String.valueOf(message), t); 084 } 085 } 086 087 /** 088 * Logs a message with {@code org.apache.log.Priority.ERROR}. 089 * 090 * @param message to log 091 * @see org.apache.commons.logging.Log#error(Object) 092 */ 093 @Override 094 public void error(final Object message) { 095 if (message != null) { 096 getLogger().error(String.valueOf(message)); 097 } 098 } 099 100 /** 101 * Logs a message with {@code org.apache.log.Priority.ERROR}. 102 * 103 * @param message to log 104 * @param t log this cause 105 * @see org.apache.commons.logging.Log#error(Object, Throwable) 106 */ 107 @Override 108 public void error(final Object message, final Throwable t) { 109 if (message != null) { 110 getLogger().error(String.valueOf(message), t); 111 } 112 } 113 114 /** 115 * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}. 116 * 117 * @param message to log 118 * @see org.apache.commons.logging.Log#fatal(Object) 119 */ 120 @Override 121 public void fatal(final Object message) { 122 if (message != null) { 123 getLogger().fatalError(String.valueOf(message)); 124 } 125 } 126 127 /** 128 * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}. 129 * 130 * @param message to log 131 * @param t log this cause 132 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 133 */ 134 @Override 135 public void fatal(final Object message, final Throwable t) { 136 if (message != null) { 137 getLogger().fatalError(String.valueOf(message), t); 138 } 139 } 140 141 /** 142 * Gets the underlying Logger we are using. 143 * 144 * @return the underlying Logger we are using. 145 */ 146 public Logger getLogger() { 147 Logger result = logger; 148 if (result == null) { 149 synchronized(this) { 150 result = logger; 151 if (result == null) { 152 logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name); 153 } 154 } 155 } 156 return result; 157 } 158 159 /** 160 * Logs a message with {@code org.apache.log.Priority.INFO}. 161 * 162 * @param message to log 163 * @see org.apache.commons.logging.Log#info(Object) 164 */ 165 @Override 166 public void info(final Object message) { 167 if (message != null) { 168 getLogger().info(String.valueOf(message)); 169 } 170 } 171 172 /** 173 * Logs a message with {@code org.apache.log.Priority.INFO}. 174 * 175 * @param message to log 176 * @param t log this cause 177 * @see org.apache.commons.logging.Log#info(Object, Throwable) 178 */ 179 @Override 180 public void info(final Object message, final Throwable t) { 181 if (message != null) { 182 getLogger().info(String.valueOf(message), t); 183 } 184 } 185 186 /** 187 * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}. 188 */ 189 @Override 190 public boolean isDebugEnabled() { 191 return getLogger().isDebugEnabled(); 192 } 193 194 /** 195 * Checks whether the {@code LogKit} logger will log messages of priority {@code ERROR}. 196 */ 197 @Override 198 public boolean isErrorEnabled() { 199 return getLogger().isErrorEnabled(); 200 } 201 202 /** 203 * Checks whether the {@code LogKit} logger will log messages of priority {@code FATAL_ERROR}. 204 */ 205 @Override 206 public boolean isFatalEnabled() { 207 return getLogger().isFatalErrorEnabled(); 208 } 209 210 /** 211 * Checks whether the {@code LogKit} logger will log messages of priority {@code INFO}. 212 */ 213 @Override 214 public boolean isInfoEnabled() { 215 return getLogger().isInfoEnabled(); 216 } 217 218 /** 219 * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}. 220 */ 221 @Override 222 public boolean isTraceEnabled() { 223 return getLogger().isDebugEnabled(); 224 } 225 226 /** 227 * Checks whether the {@code LogKit} logger will log messages of priority {@code WARN}. 228 */ 229 @Override 230 public boolean isWarnEnabled() { 231 return getLogger().isWarnEnabled(); 232 } 233 234 /** 235 * Logs a message with {@code org.apache.log.Priority.DEBUG}. 236 * 237 * @param message to log 238 * @see org.apache.commons.logging.Log#trace(Object) 239 */ 240 @Override 241 public void trace(final Object message) { 242 debug(message); 243 } 244 245 /** 246 * Logs a message with {@code org.apache.log.Priority.DEBUG}. 247 * 248 * @param message to log 249 * @param t log this cause 250 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 251 */ 252 @Override 253 public void trace(final Object message, final Throwable t) { 254 debug(message, t); 255 } 256 257 /** 258 * Logs a message with {@code org.apache.log.Priority.WARN}. 259 * 260 * @param message to log 261 * @see org.apache.commons.logging.Log#warn(Object) 262 */ 263 @Override 264 public void warn(final Object message) { 265 if (message != null) { 266 getLogger().warn(String.valueOf(message)); 267 } 268 } 269 270 /** 271 * Logs a message with {@code org.apache.log.Priority.WARN}. 272 * 273 * @param message to log 274 * @param t log this cause 275 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 276 */ 277 @Override 278 public void warn(final Object message, final Throwable t) { 279 if (message != null) { 280 getLogger().warn(String.valueOf(message), t); 281 } 282 } 283}