001package org.apache.commons.jcs3.log; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache license, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the license for the specific language governing permissions and 017 * limitations under the license. 018 */ 019 020import java.util.function.Supplier; 021 022import org.apache.logging.log4j.Level; 023import org.apache.logging.log4j.Logger; 024 025/** 026 * This is a wrapper around the <code>org.apache.logging.log4j.Logger</code> implementing our own 027 * <code>Log</code> interface. 028 */ 029public class Log4j2LogAdapter implements Log 030{ 031 private final Logger logger; 032 033 /** 034 * Construct a Log4j Logger wrapper 035 * 036 * @param logger the log4j Logger 037 */ 038 public Log4j2LogAdapter(final Logger logger) 039 { 040 this.logger = logger; 041 } 042 043 private void log(final Level level, final String message, final Supplier<?>... paramSuppliers) 044 { 045 if (logger.isEnabled(level)) 046 { 047 if (paramSuppliers == null) 048 { 049 logger.log(level, message); 050 } 051 else 052 { 053 switch (paramSuppliers.length) 054 { 055 case 1: logger.log(level, message, paramSuppliers[0].get()); 056 break; 057 case 2: logger.log(level, message, paramSuppliers[0].get(), 058 paramSuppliers[1].get()); 059 break; 060 case 3: logger.log(level, message, paramSuppliers[0].get(), 061 paramSuppliers[1].get(), paramSuppliers[2].get()); 062 break; 063 case 4: logger.log(level, message, paramSuppliers[0].get(), 064 paramSuppliers[1].get(), paramSuppliers[2].get(), 065 paramSuppliers[3].get()); 066 break; 067 case 5: logger.log(level, message, paramSuppliers[0].get(), 068 paramSuppliers[1].get(), paramSuppliers[2].get(), 069 paramSuppliers[3].get(), paramSuppliers[4].get()); 070 break; 071 default: logger.log(level, message, paramSuppliers[0].get(), 072 paramSuppliers[1].get(), paramSuppliers[2].get(), 073 paramSuppliers[3].get(), paramSuppliers[4].get(), 074 paramSuppliers[5].get()); 075 break; 076 } 077 } 078 } 079 } 080 081 /** 082 * Logs a message object with the DEBUG level. 083 * 084 * @param message the message string to log. 085 */ 086 @Override 087 public void debug(final String message) 088 { 089 logger.debug(message); 090 } 091 092 /** 093 * Logs a message object with the DEBUG level. 094 * 095 * @param message the message object to log. 096 */ 097 @Override 098 public void debug(final Object message) 099 { 100 logger.debug(message); 101 } 102 103 /** 104 * Logs a message with parameters at the DEBUG level. 105 * 106 * @param message the message to log; the format depends on the message factory. 107 * @param params parameters to the message. 108 */ 109 @Override 110 public void debug(final String message, final Object... params) 111 { 112 logger.debug(message, params); 113 } 114 115 /** 116 * Logs a message with parameters which are only to be constructed if the 117 * logging level is the DEBUG level. 118 * 119 * @param message the message to log; the format depends on the message factory. 120 * @param paramSuppliers An array of functions, which when called, produce 121 * the desired log message parameters. 122 */ 123 @Override 124 public void debug(final String message, final Supplier<?>... paramSuppliers) 125 { 126 log(Level.DEBUG, message, paramSuppliers); 127 } 128 129 /** 130 * Logs a message at the DEBUG level including the stack trace of the {@link Throwable} 131 * <code>t</code> passed as parameter. 132 * 133 * @param message the message to log. 134 * @param t the exception to log, including its stack trace. 135 */ 136 @Override 137 public void debug(final String message, final Throwable t) 138 { 139 logger.debug(message, t); 140 } 141 142 /** 143 * Logs a message object with the ERROR level. 144 * 145 * @param message the message string to log. 146 */ 147 @Override 148 public void error(final String message) 149 { 150 logger.error(message); 151 } 152 153 /** 154 * Logs a message object with the ERROR level. 155 * 156 * @param message the message object to log. 157 */ 158 @Override 159 public void error(final Object message) 160 { 161 logger.error(message); 162 } 163 164 /** 165 * Logs a message with parameters at the ERROR level. 166 * 167 * @param message the message to log; the format depends on the message factory. 168 * @param params parameters to the message. 169 */ 170 @Override 171 public void error(final String message, final Object... params) 172 { 173 logger.error(message, params); 174 } 175 176 /** 177 * Logs a message with parameters which are only to be constructed if the 178 * logging level is the ERROR level. 179 * 180 * @param message the message to log; the format depends on the message factory. 181 * @param paramSuppliers An array of functions, which when called, produce 182 * the desired log message parameters. 183 */ 184 @Override 185 public void error(final String message, final Supplier<?>... paramSuppliers) 186 { 187 log(Level.ERROR, message, paramSuppliers); 188 } 189 190 /** 191 * Logs a message at the ERROR level including the stack trace of the {@link Throwable} 192 * <code>t</code> passed as parameter. 193 * 194 * @param message the message object to log. 195 * @param t the exception to log, including its stack trace. 196 */ 197 @Override 198 public void error(final String message, final Throwable t) 199 { 200 logger.error(message, t); 201 } 202 203 /** 204 * Logs a message object with the FATAL level. 205 * 206 * @param message the message string to log. 207 */ 208 @Override 209 public void fatal(final String message) 210 { 211 logger.fatal(message); 212 } 213 214 /** 215 * Logs a message object with the FATAL level. 216 * 217 * @param message the message object to log. 218 */ 219 @Override 220 public void fatal(final Object message) 221 { 222 logger.fatal(message); 223 } 224 225 /** 226 * Logs a message with parameters at the FATAL level. 227 * 228 * @param message the message to log; the format depends on the message factory. 229 * @param params parameters to the message. 230 */ 231 @Override 232 public void fatal(final String message, final Object... params) 233 { 234 logger.fatal(message, params); 235 } 236 237 /** 238 * Logs a message with parameters which are only to be constructed if the 239 * logging level is the FATAL level. 240 * 241 * @param message the message to log; the format depends on the message factory. 242 * @param paramSuppliers An array of functions, which when called, produce 243 * the desired log message parameters. 244 */ 245 @Override 246 public void fatal(final String message, final Supplier<?>... paramSuppliers) 247 { 248 log(Level.FATAL, message, paramSuppliers); 249 } 250 251 /** 252 * Logs a message at the FATAL level including the stack trace of the {@link Throwable} 253 * <code>t</code> passed as parameter. 254 * 255 * @param message the message object to log. 256 * @param t the exception to log, including its stack trace. 257 */ 258 @Override 259 public void fatal(final String message, final Throwable t) 260 { 261 logger.fatal(message, t); 262 } 263 264 /** 265 * Gets the logger name. 266 * 267 * @return the logger name. 268 */ 269 @Override 270 public String getName() 271 { 272 return logger.getName(); 273 } 274 275 /** 276 * Logs a message object with the INFO level. 277 * 278 * @param message the message string to log. 279 */ 280 @Override 281 public void info(final String message) 282 { 283 logger.info(message); 284 } 285 286 /** 287 * Logs a message object with the INFO level. 288 * 289 * @param message the message object to log. 290 */ 291 @Override 292 public void info(final Object message) 293 { 294 logger.info(message); 295 } 296 297 /** 298 * Logs a message with parameters at the INFO level. 299 * 300 * @param message the message to log; the format depends on the message factory. 301 * @param params parameters to the message. 302 */ 303 @Override 304 public void info(final String message, final Object... params) 305 { 306 logger.info(message, params); 307 } 308 309 /** 310 * Logs a message with parameters which are only to be constructed if the 311 * logging level is the INFO level. 312 * 313 * @param message the message to log; the format depends on the message factory. 314 * @param paramSuppliers An array of functions, which when called, produce 315 * the desired log message parameters. 316 */ 317 @Override 318 public void info(final String message, final Supplier<?>... paramSuppliers) 319 { 320 log(Level.INFO, message, paramSuppliers); 321 } 322 323 /** 324 * Logs a message at the INFO level including the stack trace of the {@link Throwable} 325 * <code>t</code> passed as parameter. 326 * 327 * @param message the message object to log. 328 * @param t the exception to log, including its stack trace. 329 */ 330 @Override 331 public void info(final String message, final Throwable t) 332 { 333 logger.info(message, t); 334 } 335 336 /** 337 * Checks whether this Logger is enabled for the DEBUG Level. 338 * 339 * @return boolean - {@code true} if this Logger is enabled for level DEBUG, {@code false} 340 * otherwise. 341 */ 342 @Override 343 public boolean isDebugEnabled() 344 { 345 return logger.isDebugEnabled(); 346 } 347 348 /** 349 * Checks whether this Logger is enabled for the ERROR Level. 350 * 351 * @return boolean - {@code true} if this Logger is enabled for level ERROR, {@code false} 352 * otherwise. 353 */ 354 @Override 355 public boolean isErrorEnabled() 356 { 357 return logger.isErrorEnabled(); 358 } 359 360 /** 361 * Checks whether this Logger is enabled for the FATAL Level. 362 * 363 * @return boolean - {@code true} if this Logger is enabled for level FATAL, {@code false} 364 * otherwise. 365 */ 366 @Override 367 public boolean isFatalEnabled() 368 { 369 return logger.isFatalEnabled(); 370 } 371 372 /** 373 * Checks whether this Logger is enabled for the INFO Level. 374 * 375 * @return boolean - {@code true} if this Logger is enabled for level INFO, {@code false} 376 * otherwise. 377 */ 378 @Override 379 public boolean isInfoEnabled() 380 { 381 return logger.isInfoEnabled(); 382 } 383 384 /** 385 * Checks whether this Logger is enabled for the TRACE level. 386 * 387 * @return boolean - {@code true} if this Logger is enabled for level TRACE, {@code false} 388 * otherwise. 389 */ 390 @Override 391 public boolean isTraceEnabled() 392 { 393 return logger.isTraceEnabled(); 394 } 395 396 /** 397 * Checks whether this Logger is enabled for the WARN Level. 398 * 399 * @return boolean - {@code true} if this Logger is enabled for level WARN, {@code false} 400 * otherwise. 401 */ 402 @Override 403 public boolean isWarnEnabled() 404 { 405 return logger.isWarnEnabled(); 406 } 407 408 /** 409 * Logs a message object with the TRACE level. 410 * 411 * @param message the message string to log. 412 */ 413 @Override 414 public void trace(final String message) 415 { 416 logger.trace(message); 417 } 418 419 /** 420 * Logs a message object with the TRACE level. 421 * 422 * @param message the message object to log. 423 */ 424 @Override 425 public void trace(final Object message) 426 { 427 logger.trace(message); 428 } 429 430 /** 431 * Logs a message with parameters at the TRACE level. 432 * 433 * @param message the message to log; the format depends on the message factory. 434 * @param params parameters to the message. 435 */ 436 @Override 437 public void trace(final String message, final Object... params) 438 { 439 logger.trace(message, params); 440 } 441 442 /** 443 * Logs a message with parameters which are only to be constructed if the 444 * logging level is the TRACE level. 445 * 446 * @param message the message to log; the format depends on the message factory. 447 * @param paramSuppliers An array of functions, which when called, produce 448 * the desired log message parameters. 449 */ 450 @Override 451 public void trace(final String message, final Supplier<?>... paramSuppliers) 452 { 453 log(Level.TRACE, message, paramSuppliers); 454 } 455 456 /** 457 * Logs a message at the TRACE level including the stack trace of the {@link Throwable} 458 * <code>t</code> passed as parameter. 459 * 460 * @param message the message object to log. 461 * @param t the exception to log, including its stack trace. 462 * @see #debug(String) 463 */ 464 @Override 465 public void trace(final String message, final Throwable t) 466 { 467 logger.trace(message, t); 468 } 469 470 /** 471 * Logs a message object with the WARN level. 472 * 473 * @param message the message string to log. 474 */ 475 @Override 476 public void warn(final String message) 477 { 478 logger.warn(message); 479 } 480 481 /** 482 * Logs a message object with the WARN level. 483 * 484 * @param message the message object to log. 485 */ 486 @Override 487 public void warn(final Object message) 488 { 489 logger.warn(message); 490 } 491 492 /** 493 * Logs a message with parameters at the WARN level. 494 * 495 * @param message the message to log; the format depends on the message factory. 496 * @param params parameters to the message. 497 */ 498 @Override 499 public void warn(final String message, final Object... params) 500 { 501 logger.warn(message, params); 502 } 503 504 /** 505 * Logs a message with parameters which are only to be constructed if the 506 * logging level is the WARN level. 507 * 508 * @param message the message to log; the format depends on the message factory. 509 * @param paramSuppliers An array of functions, which when called, produce 510 * the desired log message parameters. 511 */ 512 @Override 513 public void warn(final String message, final Supplier<?>... paramSuppliers) 514 { 515 log(Level.WARN, message, paramSuppliers); 516 } 517 518 /** 519 * Logs a message at the WARN level including the stack trace of the {@link Throwable} 520 * <code>t</code> passed as parameter. 521 * 522 * @param message the message object to log. 523 * @param t the exception to log, including its stack trace. 524 */ 525 @Override 526 public void warn(final String message, final Throwable t) 527 { 528 logger.warn(message, t); 529 } 530}