001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.log; 018 019import java.util.function.Supplier; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024/** 025 * A convenient accessor for Commons Logging, providing not only 026 * {@code CharSequence} based log methods but also {@code Supplier} 027 * based variants for use with Java 8 lambda expressions. 028 * 029 * @author Juergen Hoeller 030 * @since 5.2 031 */ 032public class LogAccessor { 033 034 private final Log log; 035 036 037 /** 038 * Create a new accessor for the given Commons Log. 039 * @see LogFactory#getLog(Class) 040 * @see LogFactory#getLog(String) 041 */ 042 public LogAccessor(Log log) { 043 this.log = log; 044 } 045 046 /** 047 * Create a new accessor for the specified Commons Log category. 048 * @see LogFactory#getLog(Class) 049 */ 050 public LogAccessor(Class<?> logCategory) { 051 this.log = LogFactory.getLog(logCategory); 052 } 053 054 /** 055 * Create a new accessor for the specified Commons Log category. 056 * @see LogFactory#getLog(String) 057 */ 058 public LogAccessor(String logCategory) { 059 this.log = LogFactory.getLog(logCategory); 060 } 061 062 063 /** 064 * Return the target Commons Log. 065 */ 066 public final Log getLog() { 067 return this.log; 068 } 069 070 071 // Log level checks 072 073 /** 074 * Is fatal logging currently enabled? 075 */ 076 public boolean isFatalEnabled() { 077 return this.log.isFatalEnabled(); 078 } 079 080 /** 081 * Is error logging currently enabled? 082 */ 083 public boolean isErrorEnabled() { 084 return this.log.isErrorEnabled(); 085 } 086 087 /** 088 * Is warn logging currently enabled? 089 */ 090 public boolean isWarnEnabled() { 091 return this.log.isWarnEnabled(); 092 } 093 094 /** 095 * Is info logging currently enabled? 096 */ 097 public boolean isInfoEnabled() { 098 return this.log.isInfoEnabled(); 099 } 100 101 /** 102 * Is debug logging currently enabled? 103 */ 104 public boolean isDebugEnabled() { 105 return this.log.isDebugEnabled(); 106 } 107 108 /** 109 * Is trace logging currently enabled? 110 */ 111 public boolean isTraceEnabled() { 112 return this.log.isTraceEnabled(); 113 } 114 115 116 // Plain log methods 117 118 /** 119 * Log a message with fatal log level. 120 * @param message the message to log 121 */ 122 public void fatal(CharSequence message) { 123 this.log.fatal(message); 124 } 125 126 /** 127 * Log an error with fatal log level. 128 * @param cause the exception to log 129 * @param message the message to log 130 */ 131 public void fatal(Throwable cause, CharSequence message) { 132 this.log.fatal(message, cause); 133 } 134 135 /** 136 * Log a message with error log level. 137 * @param message the message to log 138 */ 139 public void error(CharSequence message) { 140 this.log.error(message); 141 } 142 143 /** 144 * Log an error with error log level. 145 * @param cause the exception to log 146 * @param message the message to log 147 */ 148 public void error(Throwable cause, CharSequence message) { 149 this.log.error(message, cause); 150 } 151 152 /** 153 * Log a message with warn log level. 154 * @param message the message to log 155 */ 156 public void warn(CharSequence message) { 157 this.log.warn(message); 158 } 159 160 /** 161 * Log an error with warn log level. 162 * @param cause the exception to log 163 * @param message the message to log 164 */ 165 public void warn(Throwable cause, CharSequence message) { 166 this.log.warn(message, cause); 167 } 168 169 /** 170 * Log a message with info log level. 171 * @param message the message to log 172 */ 173 public void info(CharSequence message) { 174 this.log.info(message); 175 } 176 177 /** 178 * Log an error with info log level. 179 * @param cause the exception to log 180 * @param message the message to log 181 */ 182 public void info(Throwable cause, CharSequence message) { 183 this.log.info(message, cause); 184 } 185 186 /** 187 * Log a message with debug log level. 188 * @param message the message to log 189 */ 190 public void debug(CharSequence message) { 191 this.log.debug(message); 192 } 193 194 /** 195 * Log an error with debug log level. 196 * @param cause the exception to log 197 * @param message the message to log 198 */ 199 public void debug(Throwable cause, CharSequence message) { 200 this.log.debug(message, cause); 201 } 202 203 /** 204 * Log a message with trace log level. 205 * @param message the message to log 206 */ 207 public void trace(CharSequence message) { 208 this.log.trace(message); 209 } 210 211 /** 212 * Log an error with trace log level. 213 * @param cause the exception to log 214 * @param message the message to log 215 */ 216 public void trace(Throwable cause, CharSequence message) { 217 this.log.trace(message, cause); 218 } 219 220 221 // Supplier-based log methods 222 223 /** 224 * Log a message with fatal log level. 225 * @param messageSupplier a lazy supplier for the message to log 226 */ 227 public void fatal(Supplier<? extends CharSequence> messageSupplier) { 228 if (this.log.isFatalEnabled()) { 229 this.log.fatal(LogMessage.of(messageSupplier)); 230 } 231 } 232 233 /** 234 * Log an error with fatal log level. 235 * @param cause the exception to log 236 * @param messageSupplier a lazy supplier for the message to log 237 */ 238 public void fatal(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 239 if (this.log.isFatalEnabled()) { 240 this.log.fatal(LogMessage.of(messageSupplier), cause); 241 } 242 } 243 244 /** 245 * Log a message with error log level. 246 * @param messageSupplier a lazy supplier for the message to log 247 */ 248 public void error(Supplier<? extends CharSequence> messageSupplier) { 249 if (this.log.isErrorEnabled()) { 250 this.log.error(LogMessage.of(messageSupplier)); 251 } 252 } 253 254 /** 255 * Log an error with error log level. 256 * @param cause the exception to log 257 * @param messageSupplier a lazy supplier for the message to log 258 */ 259 public void error(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 260 if (this.log.isErrorEnabled()) { 261 this.log.error(LogMessage.of(messageSupplier), cause); 262 } 263 } 264 265 /** 266 * Log a message with warn log level. 267 * @param messageSupplier a lazy supplier for the message to log 268 */ 269 public void warn(Supplier<? extends CharSequence> messageSupplier) { 270 if (this.log.isWarnEnabled()) { 271 this.log.warn(LogMessage.of(messageSupplier)); 272 } 273 } 274 275 /** 276 * Log an error with warn log level. 277 * @param cause the exception to log 278 * @param messageSupplier a lazy supplier for the message to log 279 */ 280 public void warn(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 281 if (this.log.isWarnEnabled()) { 282 this.log.warn(LogMessage.of(messageSupplier), cause); 283 } 284 } 285 286 /** 287 * Log a message with info log level. 288 * @param messageSupplier a lazy supplier for the message to log 289 */ 290 public void info(Supplier<? extends CharSequence> messageSupplier) { 291 if (this.log.isInfoEnabled()) { 292 this.log.info(LogMessage.of(messageSupplier)); 293 } 294 } 295 296 /** 297 * Log an error with info log level. 298 * @param cause the exception to log 299 * @param messageSupplier a lazy supplier for the message to log 300 */ 301 public void info(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 302 if (this.log.isInfoEnabled()) { 303 this.log.info(LogMessage.of(messageSupplier), cause); 304 } 305 } 306 307 /** 308 * Log a message with debug log level. 309 * @param messageSupplier a lazy supplier for the message to log 310 */ 311 public void debug(Supplier<? extends CharSequence> messageSupplier) { 312 if (this.log.isDebugEnabled()) { 313 this.log.debug(LogMessage.of(messageSupplier)); 314 } 315 } 316 317 /** 318 * Log an error with debug log level. 319 * @param cause the exception to log 320 * @param messageSupplier a lazy supplier for the message to log 321 */ 322 public void debug(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 323 if (this.log.isDebugEnabled()) { 324 this.log.debug(LogMessage.of(messageSupplier), cause); 325 } 326 } 327 328 /** 329 * Log a message with trace log level. 330 * @param messageSupplier a lazy supplier for the message to log 331 */ 332 public void trace(Supplier<? extends CharSequence> messageSupplier) { 333 if (this.log.isTraceEnabled()) { 334 this.log.trace(LogMessage.of(messageSupplier)); 335 } 336 } 337 338 /** 339 * Log an error with trace log level. 340 * @param cause the exception to log 341 * @param messageSupplier a lazy supplier for the message to log 342 */ 343 public void trace(Throwable cause, Supplier<? extends CharSequence> messageSupplier) { 344 if (this.log.isTraceEnabled()) { 345 this.log.trace(LogMessage.of(messageSupplier), cause); 346 } 347 } 348 349}