2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.logging.api;
25 * <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a>
27 * <p>The service used by this factory must implement {@link LoggerCreationService}. If no implementation has been
28 * configured or could not be instantiated, a <b>no-op logger</b> will be used, and <b>no events</b> will be logged.
29 * This is done to prevent recursion if attempts are being made to log exceptions that resulted from logger
37 * @see LoggerCreationService
39 @SuppressWarnings("ThrowableInstanceNeverThrown")
40 public class LoggerFactory extends BaseFactory {
42 private static final LoggerCreationService SERVICE;
45 LoggerCreationService service;
48 service = locateService(LoggerCreationService.class);
49 } catch (Throwable e) {
50 new RuntimeException("Failed to instantiate logger factory", e).printStackTrace();
51 // use the no-op service to prevent recursion in case of an attempt to log an exception as a
52 // result of a logger initialization error
53 service = new NoOpLoggerCreationService();
59 public static Logger getLogger(String clazzName) {
60 return SERVICE.getLogger(clazzName);
63 public static Logger getLogger(Class<?> clazz) {
64 return SERVICE.getLogger(clazz);
67 private static class NoOpLoggerCreationService implements LoggerCreationService {
69 private static final Logger NO_OP_LOGGER = new Logger() {
72 public String getName() {
73 return "No-Op Logger";
77 public boolean isMetricsEnabled() {
82 public void metrics(String msg) { }
85 public void metrics(String msg, Object arg) { }
88 public void metrics(String msg, Object arg1, Object arg2) { }
91 public void metrics(String msg, Object... arguments) { }
94 public void metrics(String msg, Throwable t) { }
97 public boolean isAuditEnabled() {
102 public void audit(String msg) { }
105 public void audit(String msg, Object arg) { }
108 public void audit(String msg, Object arg1, Object arg2) { }
111 public void audit(String msg, Object... arguments) { }
114 public void audit(String msg, Throwable t) { }
117 public boolean isDebugEnabled() {
122 public void debug(String msg) { }
125 public void debug(String msg, Object arg) { }
128 public void debug(String msg, Object arg1, Object arg2) { }
131 public void debug(String msg, Object... arguments) { }
134 public void debug(String msg, Throwable t) { }
137 public boolean isInfoEnabled() {
142 public void info(String msg) { }
145 public void info(String msg, Object arg) { }
148 public void info(String msg, Object arg1, Object arg2) { }
151 public void info(String msg, Object... arguments) { }
154 public void info(String msg, Throwable t) { }
157 public boolean isWarnEnabled() {
162 public void warn(String msg) { }
165 public void warn(String msg, Object arg) { }
168 public void warn(String msg, Object... arguments) { }
171 public void warn(String msg, Object arg1, Object arg2) { }
174 public void warn(String msg, Throwable t) { }
177 public boolean isErrorEnabled() {
182 public void error(String msg) { }
185 public void error(String msg, Object arg) { }
188 public void error(String msg, Object arg1, Object arg2) { }
191 public void error(String msg, Object... arguments) { }
194 public void error(String msg, Throwable t) { }
198 public Logger getLogger(String className) {
203 public Logger getLogger(Class<?> clazz) {