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 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 private LoggerFactory() {
63 registeredLoggers = new HashMap<String, Logger>();
64 resolver = new InternalCodeFrameResolver(1);
65 defaultLogger = LoggerConstants.EMPTY;
66 }
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 LogController.ensureInitialization();
80 Logger intendedLogger = INSTANCE.registeredLoggers.get(identifier);
81 if (intendedLogger == null) {
82 int lastDotIndex = identifier.lastIndexOf('.');
83 if (lastDotIndex > 0) {
84 intendedLogger = getLogger(identifier.substring(0, lastDotIndex));
85 } else {
86 intendedLogger = INSTANCE.defaultLogger;
87 }
88 }
89 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 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 String codeFrameQualifiedClassName = INSTANCE.resolver.getCodeFrame().getClassName();
117 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 LogController.ensureInitialization();
131 Logger previous = INSTANCE.defaultLogger;
132 if (defaultLogger != null) {
133 INSTANCE.defaultLogger = defaultLogger;
134 }
135 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 LogController.ensureInitialization();
147 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 LogController.ensureInitialization();
158 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 LogController.ensureInitialization();
174 Logger intendedLogger = null;
175 if (logger != null) {
176 intendedLogger = INSTANCE.registeredLoggers.put(logger.getIdentifier(), logger);
177 }
178 if (intendedLogger == null) {
179 intendedLogger = INSTANCE.defaultLogger;
180 }
181 return intendedLogger;
182 }
183
184 }