| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Slf4jLog |
|
| 5.0;5 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * Slf4jLog.java - mongodb-async-driver - Allanbank Consulting, Inc. | |
| 4 | * %% | |
| 5 | * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. | |
| 6 | * %% | |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 8 | * you may not use this file except in compliance with the License. | |
| 9 | * You may obtain a copy of the License at | |
| 10 | * | |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 12 | * | |
| 13 | * Unless required by applicable law or agreed to in writing, software | |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 16 | * See the License for the specific language governing permissions and | |
| 17 | * limitations under the License. | |
| 18 | * #L% | |
| 19 | */ | |
| 20 | package com.allanbank.mongodb.util.log; | |
| 21 | ||
| 22 | import java.lang.reflect.InvocationTargetException; | |
| 23 | import java.lang.reflect.Method; | |
| 24 | import java.util.logging.Level; | |
| 25 | import java.util.logging.LogRecord; | |
| 26 | import java.util.logging.Logger; | |
| 27 | ||
| 28 | /** | |
| 29 | * Slf4jLog is the simplified logging implementation for SLF4J logging facade. | |
| 30 | * | |
| 31 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
| 32 | * mutated in incompatible ways between any two releases of the driver. | |
| 33 | * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved | |
| 34 | */ | |
| 35 | public class Slf4jLog extends AbstractLog { | |
| 36 | ||
| 37 | /** The delegate for the log to a SLF4J <code>LocationAwareLogger</code>. */ | |
| 38 | private final Object myDelegate; | |
| 39 | ||
| 40 | /** | |
| 41 | * The <code>log(Marker, String, int, String, Object[])</code> method from | |
| 42 | * the <code>LocationAwareLogger</code> interface. | |
| 43 | */ | |
| 44 | private final Method myLogMethod; | |
| 45 | ||
| 46 | /** | |
| 47 | * Creates a new {@link Slf4jLog}. | |
| 48 | * | |
| 49 | * @param logMethod | |
| 50 | * The <code>log(Marker, String, int, String, Object[])</code> | |
| 51 | * method from the <code>LocationAwareLogger</code> interface. | |
| 52 | * @param logger | |
| 53 | * The SLF4J Logger. | |
| 54 | */ | |
| 55 | 235 | protected Slf4jLog(final Method logMethod, final Object logger) { |
| 56 | 235 | myDelegate = logger; |
| 57 | 235 | myLogMethod = logMethod; |
| 58 | 235 | } |
| 59 | ||
| 60 | /** | |
| 61 | * {@inheritDoc} | |
| 62 | * <p> | |
| 63 | * Overridden to create a {@link LogRecord} based on the log information. | |
| 64 | * </p> | |
| 65 | * | |
| 66 | * @see AbstractLog#doLog(Level, Throwable, String, Object...) | |
| 67 | */ | |
| 68 | @Override | |
| 69 | protected final void doLog(final Level level, final Throwable thrown, | |
| 70 | final String template, final Object... args) { | |
| 71 | // Note the name of the class is the AbstractLog which is where all of | |
| 72 | // the public log methods are implemented. | |
| 73 | try { | |
| 74 | 324 | myLogMethod.invoke(myDelegate, new Object[] { null, CLASS_NAME, |
| 75 | toInt(level), template, args, thrown }); | |
| 76 | } | |
| 77 | 0 | catch (final IllegalArgumentException e) { |
| 78 | 0 | Logger.getLogger(Slf4jLog.class.getName()).log(Level.WARNING, |
| 79 | "Failed to log a message: " + e.getMessage(), e); | |
| 80 | } | |
| 81 | 0 | catch (final IllegalAccessException e) { |
| 82 | 0 | Logger.getLogger(Slf4jLog.class.getName()).log(Level.WARNING, |
| 83 | "Failed to log a message: " + e.getMessage(), e); | |
| 84 | } | |
| 85 | 0 | catch (final InvocationTargetException e) { |
| 86 | 0 | Logger.getLogger(Slf4jLog.class.getName()).log(Level.WARNING, |
| 87 | "Failed to log a message: " + e.getMessage(), e); | |
| 88 | 324 | } |
| 89 | 324 | } |
| 90 | ||
| 91 | /** | |
| 92 | * Returns the integer level for the {@link Level} as defined in the | |
| 93 | * LocationAwareLogger interface. | |
| 94 | * | |
| 95 | * @param level | |
| 96 | * The level to convert. | |
| 97 | * @return The integer level. | |
| 98 | */ | |
| 99 | private Integer toInt(final Level level) { | |
| 100 | 324 | final int levelIntValue = (level != null) ? level.intValue() : 0; |
| 101 | ||
| 102 | // Note: Return values are from the <code>LocationAwareLogger</code> | |
| 103 | // interface. | |
| 104 | 324 | if (levelIntValue == 0) { |
| 105 | 1 | return 0; |
| 106 | } | |
| 107 | 323 | else if (levelIntValue <= Level.FINE.intValue()) { |
| 108 | 177 | return 10; |
| 109 | } | |
| 110 | 146 | else if (levelIntValue <= Level.INFO.intValue()) { |
| 111 | 83 | return 20; |
| 112 | } | |
| 113 | 63 | else if (levelIntValue <= Level.WARNING.intValue()) { |
| 114 | 56 | return 30; |
| 115 | } | |
| 116 | else { | |
| 117 | 7 | return 40; |
| 118 | } | |
| 119 | } | |
| 120 | } |