| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LoggerFactory |
|
| 1.625;1,625 |
| 1 | /* | |
| 2 | * This file is part of AceLogger. | |
| 3 | * | |
| 4 | * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU | |
| 5 | * Lesser General Public License as published by the Free Software Foundation, either version 3 of | |
| 6 | * the License, or (at your option) any later version. | |
| 7 | * | |
| 8 | * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | |
| 9 | * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 10 | * Lesser General Public License for more details. | |
| 11 | * | |
| 12 | * You should have received a copy of the GNU Lesser General Public License along with AceLogger. | |
| 13 | * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. | |
| 14 | */ | |
| 15 | package net.sourceforge.acelogger.factory; | |
| 16 | ||
| 17 | import java.util.ArrayList; | |
| 18 | import java.util.HashMap; | |
| 19 | import java.util.List; | |
| 20 | import java.util.Map; | |
| 21 | ||
| 22 | import net.sourceforge.acelogger.Logger; | |
| 23 | import net.sourceforge.acelogger.constants.LoggerConstants; | |
| 24 | import net.sourceforge.acelogger.execution.LogController; | |
| 25 | import net.sourceforge.acelogger.location.resolver.CodeFrameResolver; | |
| 26 | import net.sourceforge.acelogger.location.resolver.InternalCodeFrameResolver; | |
| 27 | ||
| 28 | /** | |
| 29 | * Factory for managing instances of configured loggers. | |
| 30 | * | |
| 31 | * @author Zardi (https://sourceforge.net/users/daniel_zardi) | |
| 32 | * @version 1.0.0 | |
| 33 | * @since 1.0.0 | |
| 34 | */ | |
| 35 | public final class LoggerFactory { | |
| 36 | ||
| 37 | /** | |
| 38 | * A instance of this class (the only one). | |
| 39 | */ | |
| 40 | 1 | private static final LoggerFactory INSTANCE = new LoggerFactory(); |
| 41 | ||
| 42 | /** | |
| 43 | * Used when no configured logger is found for a given identifier. | |
| 44 | */ | |
| 45 | private Logger defaultLogger; | |
| 46 | ||
| 47 | /** | |
| 48 | * Maps each logger manager to a unique identifier. | |
| 49 | */ | |
| 50 | private final Map<String, Logger> registeredLoggers; | |
| 51 | ||
| 52 | /** | |
| 53 | * The resolver used to determine which class invoked the factory method. | |
| 54 | */ | |
| 55 | private CodeFrameResolver resolver; | |
| 56 | ||
| 57 | /** | |
| 58 | * Creates a new LoggerFactory (This class is a singleton). | |
| 59 | * | |
| 60 | * @since 1.0.0 | |
| 61 | */ | |
| 62 | 1 | private LoggerFactory() { |
| 63 | 1 | registeredLoggers = new HashMap<String, Logger>(); |
| 64 | 1 | resolver = new InternalCodeFrameResolver(1); |
| 65 | 1 | defaultLogger = LoggerConstants.EMPTY; |
| 66 | 1 | } |
| 67 | ||
| 68 | /** | |
| 69 | * Obtains a properly configured logger for the given identifier. | |
| 70 | * | |
| 71 | * @param identifier | |
| 72 | * The identifier of the intended logger. | |
| 73 | * @return A properly configured logger for the given identifier. If the identifier is not | |
| 74 | * found, then the default logger is returned. | |
| 75 | * @see LoggerFactory#getDefaultLogger() | |
| 76 | * @since 1.0.0 | |
| 77 | */ | |
| 78 | public static Logger getLogger(String identifier) { | |
| 79 | 0 | LogController.ensureInitialization(); |
| 80 | 0 | Logger intendedLogger = INSTANCE.registeredLoggers.get(identifier); |
| 81 | 0 | if (intendedLogger == null) { |
| 82 | 0 | int lastDotIndex = identifier.lastIndexOf('.'); |
| 83 | 0 | if (lastDotIndex > 0) { |
| 84 | 0 | intendedLogger = getLogger(identifier.substring(0, lastDotIndex)); |
| 85 | } else { | |
| 86 | 0 | intendedLogger = INSTANCE.defaultLogger; |
| 87 | } | |
| 88 | } | |
| 89 | 0 | return intendedLogger; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Obtains a properly configured logger for the given class. | |
| 94 | * | |
| 95 | * @param clazz | |
| 96 | * The class that will generate the logger identifier. | |
| 97 | * @return A properly configured logger for the given identifier. If the identifier is not | |
| 98 | * found, then the default logger is returned. | |
| 99 | * @see LoggerFactory#getDefaultLogger() | |
| 100 | * @since 1.0.0 | |
| 101 | */ | |
| 102 | public static Logger getLogger(Class<?> clazz) { | |
| 103 | 0 | return getLogger(clazz.getName()); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Obtains a properly configured logger based in the callers class information. | |
| 108 | * | |
| 109 | * @return A properly configured logger for the given identifier. If the identifier is not | |
| 110 | * found, then the default logger is returned. | |
| 111 | * @see LoggerFactory#getDefaultLogger() | |
| 112 | * @see CodeFrameResolver#getCodeFrame() | |
| 113 | * @since 1.0.0 | |
| 114 | */ | |
| 115 | public static Logger getLogger() { | |
| 116 | 0 | String codeFrameQualifiedClassName = INSTANCE.resolver.getCodeFrame().getClassName(); |
| 117 | 0 | return getLogger(codeFrameQualifiedClassName); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Sets the default logger of this factory, the one used when no logger is found for a given | |
| 122 | * identifier. | |
| 123 | * | |
| 124 | * @param defaultLogger | |
| 125 | * The logger that will be used as the default one from now. | |
| 126 | * @return The previous default logger. | |
| 127 | * @since 1.0.0 | |
| 128 | */ | |
| 129 | public static Logger setDefaultLogger(Logger defaultLogger) { | |
| 130 | 0 | LogController.ensureInitialization(); |
| 131 | 0 | Logger previous = INSTANCE.defaultLogger; |
| 132 | 0 | if (defaultLogger != null) { |
| 133 | 0 | INSTANCE.defaultLogger = defaultLogger; |
| 134 | } | |
| 135 | 0 | return previous; |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Returns the default logger of this factory, the one used when no logger is found for a given | |
| 140 | * identifier. | |
| 141 | * | |
| 142 | * @return The default logger. | |
| 143 | * @since 1.0.0 | |
| 144 | */ | |
| 145 | public static Logger getDefaultLogger() { | |
| 146 | 1 | LogController.ensureInitialization(); |
| 147 | 1 | return INSTANCE.defaultLogger; |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Returns a list of all loggers registered in this factory. | |
| 152 | * | |
| 153 | * @return A list of loggers. | |
| 154 | * @since 1.0.0 | |
| 155 | */ | |
| 156 | public static List<Logger> getRegisteredLoggers() { | |
| 157 | 2 | LogController.ensureInitialization(); |
| 158 | 2 | return new ArrayList<Logger>(INSTANCE.registeredLoggers.values()); |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Registers an appender within this factory. The mapped identifier will be the loggers | |
| 163 | * identifier. | |
| 164 | * | |
| 165 | * @param logger | |
| 166 | * The logger to be registered. | |
| 167 | * @return The default logger if there was no previous mapping for the identifier or the | |
| 168 | * previous configured logger otherwise. | |
| 169 | * @see LoggerFactory#getDefaultLogger() | |
| 170 | * @since 1.0.0 | |
| 171 | */ | |
| 172 | public static Logger registerLogger(Logger logger) { | |
| 173 | 2 | LogController.ensureInitialization(); |
| 174 | 1 | Logger intendedLogger = null; |
| 175 | 1 | if (logger != null) { |
| 176 | 0 | intendedLogger = INSTANCE.registeredLoggers.put(logger.getIdentifier(), logger); |
| 177 | } | |
| 178 | 1 | if (intendedLogger == null) { |
| 179 | 1 | intendedLogger = INSTANCE.defaultLogger; |
| 180 | } | |
| 181 | 1 | return intendedLogger; |
| 182 | } | |
| 183 | ||
| 184 | } |