2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.api;
19 import org.openecomp.sdc.logging.spi.LoggerCreationService;
21 import java.util.Objects;
24 * <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a>
25 * <p>The service used by this factory must implement {@link LoggerCreationService}. If no
26 * implementation has been configured or could be instantiated, a <b>no-op logger</b> will be
27 * used, and <b>no events</b> will be logged. This is done to prevent recursion if attempts are
28 * being made to log exceptions that resulted from logger initialization. </p>
34 * @see LoggerCreationService
36 public class LoggerFactory {
38 // use the no-op service to prevent recursion in case of an attempt to log an exception as a
39 // result of a logger initialization error
40 private static final LoggerCreationService SERVICE = ServiceBinder.getCreationServiceBinding().orElseGet(
41 NoOpLoggerCreationService::new);
43 private LoggerFactory() {
44 // prevent instantiation
47 public static Logger getLogger(String clazzName) {
48 return SERVICE.getLogger(clazzName);
51 public static Logger getLogger(Class<?> clazz) {
52 return SERVICE.getLogger(clazz);
55 private static class NoOpLoggerCreationService implements LoggerCreationService {
57 private static class NoOpLogger implements Logger {
59 private static final Logger INSTANCE = new NoOpLogger();
62 public String getName() {
63 return "No-Op Logger";
67 public boolean isMetricsEnabled() {
72 public void metrics(MetricsData msg) {
77 public void metrics(String msg) {
82 public boolean isAuditEnabled() {
87 public void audit(AuditData data) {
92 public boolean isDebugEnabled() {
97 public void debug(String msg) {
102 public void debug(String msg, Object arg) {
107 public void debug(String msg, Object arg1, Object arg2) {
112 public void debug(String msg, Object... arguments) {
117 public void debug(String msg, Throwable t) {
122 public boolean isInfoEnabled() {
127 public void info(String msg) {
132 public void info(String msg, Object arg) {
137 public void info(String msg, Object arg1, Object arg2) {
142 public void info(String msg, Object... arguments) {
147 public void info(String msg, Throwable t) {
152 public boolean isWarnEnabled() {
157 public void warn(String msg) {
162 public void warn(String msg, Object arg) {
167 public void warn(String msg, Object... arguments) {
172 public void warn(String msg, Object arg1, Object arg2) {
177 public void warn(String msg, Throwable t) {
182 public boolean isErrorEnabled() {
187 public void error(String msg) {
192 public void error(String msg, Object arg) {
197 public void error(String msg, Object arg1, Object arg2) {
202 public void error(String msg, Object... arguments) {
207 public void error(String msg, Throwable t) {
213 public Logger getLogger(String className) {
214 Objects.requireNonNull(className, "Name cannot be null");
215 return NoOpLogger.INSTANCE;
219 public Logger getLogger(Class<?> clazz) {
220 Objects.requireNonNull(clazz, "Class cannot be null");
221 return NoOpLogger.INSTANCE;