diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java | 447 |
1 files changed, 223 insertions, 224 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java index 1be2fa21dc..b790a02042 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,245 +16,244 @@ package org.openecomp.sdc.logging.api; +import org.openecomp.sdc.logging.provider.LoggerCreationService; + +import java.util.Objects; /** * <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a> * <p>The service used by this factory must implement {@link LoggerCreationService}. If no - * implementation has been configured or could not be instantiated, a <b>no-op logger</b> will be + * implementation has been configured or could be instantiated, a <b>no-op logger</b> will be * used, and <b>no events</b> will be logged. This is done to prevent recursion if attempts are * being made to log exceptions that resulted from logger initialization. </p> * * @author evitaliy - * @see BaseFactory - * @see LoggerCreationService * @since 13/09/2016. + * + * @see ServiceBinder + * @see LoggerCreationService */ -@SuppressWarnings("ThrowableInstanceNeverThrown") -public class LoggerFactory extends BaseFactory { - - private static final LoggerCreationService SERVICE; +public class LoggerFactory { - static { - LoggerCreationService service; + // use the no-op service to prevent recursion in case of an attempt to log an exception as a + // result of a logger initialization error + private static final LoggerCreationService SERVICE = ServiceBinder.getCreationServiceBinding().orElse( + new NoOpLoggerCreationService()); - try { - service = locateService(LoggerCreationService.class); - } catch (Exception ex) { - new RuntimeException("Failed to instantiate logger factory", ex).printStackTrace(); - // use the no-op service to prevent recursion in case of an attempt to log an exception as a - // result of a logger initialization error - service = new NoOpLoggerCreationService(); + private LoggerFactory() { + // prevent instantiation } - SERVICE = service; - } - - public static Logger getLogger(String clazzName) { - return SERVICE.getLogger(clazzName); - } - - public static Logger getLogger(Class<?> clazz) { - return SERVICE.getLogger(clazz); - } - - private static class NoOpLoggerCreationService implements LoggerCreationService { - private static final Logger NO_OP_LOGGER = new NoOpLogger(); - - private static class NoOpLogger implements Logger { - @Override - public String getName() { - return "No-Op Logger"; - } - - @Override - public boolean isMetricsEnabled() { - return false; - } - - @Override - public void metrics(String msg) { - //this is no_op_method - } - - @Override - public void metrics(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void metrics(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void metrics(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void metrics(String msg, Throwable t) { - //this is no_op_method - } - - @Override - public boolean isAuditEnabled() { - return false; - } - - @Override - public void audit(String msg) { - //this is no_op_method - } - - @Override - public void audit(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void audit(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void audit(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void audit(String msg, Throwable t) { - //this is no_op_method - } - - @Override - public boolean isDebugEnabled() { - return false; - } - - @Override - public void debug(String msg) { - //this is no_op_method - } - - @Override - public void debug(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void debug(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void debug(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void debug(String msg, Throwable t) { - //this is no_op_method - } - - @Override - public boolean isInfoEnabled() { - return false; - } - - @Override - public void info(String msg) { - //this is no_op_method - } - - @Override - public void info(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void info(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void info(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void info(String msg, Throwable t) { - //this is no_op_method - } - - @Override - public boolean isWarnEnabled() { - return false; - } - - @Override - public void warn(String msg) { - //this is no_op_method - } - - @Override - public void warn(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void warn(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void warn(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void warn(String msg, Throwable t) { - //this is no_op_method - } - - @Override - public boolean isErrorEnabled() { - return false; - } - - @Override - public void error(String msg) { - //this is no_op_method - } - - @Override - public void error(String msg, Object arg) { - //this is no_op_method - } - - @Override - public void error(String msg, Object arg1, Object arg2) { - //this is no_op_method - } - - @Override - public void error(String msg, Object... arguments) { - //this is no_op_method - } - - @Override - public void error(String msg, Throwable t) { - //this is no_op_method - } + public static Logger getLogger(String clazzName) { + return SERVICE.getLogger(clazzName); } - @Override - public Logger getLogger(String className) { - return NO_OP_LOGGER; + public static Logger getLogger(Class<?> clazz) { + return SERVICE.getLogger(clazz); } - @Override - public Logger getLogger(Class<?> clazz) { - return NO_OP_LOGGER; + private static class NoOpLoggerCreationService implements LoggerCreationService { + + private static final Logger NO_OP_LOGGER = new NoOpLogger(); + + private static class NoOpLogger implements Logger { + + @Override + public String getName() { + return "No-Op Logger"; + } + + @Override + public boolean isMetricsEnabled() { + return false; + } + + @Override + public void metrics(String msg) { + // no-op + } + + @Override + public void metrics(String msg, Object arg) { + // no-op + } + + @Override + public void metrics(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void metrics(String msg, Object... arguments) { + // no-op + } + + @Override + public void metrics(String msg, Throwable t) { + // no-op + } + + @Override + public boolean isAuditEnabled() { + return false; + } + + @Override + public void audit(String msg) { + // no-op + } + + @Override + public void audit(String msg, Object arg) { + // no-op + } + + @Override + public void audit(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void audit(String msg, Object... arguments) { + // no-op + } + + @Override + public void audit(String msg, Throwable t) { + // no-op + } + + @Override + public boolean isDebugEnabled() { + return false; + } + + @Override + public void debug(String msg) { + // no-op + } + + @Override + public void debug(String msg, Object arg) { + // no-op + } + + @Override + public void debug(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void debug(String msg, Object... arguments) { + // no-op + } + + @Override + public void debug(String msg, Throwable t) { + // no-op + } + + @Override + public boolean isInfoEnabled() { + return false; + } + + @Override + public void info(String msg) { + // no-op + } + + @Override + public void info(String msg, Object arg) { + // no-op + } + + @Override + public void info(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void info(String msg, Object... arguments) { + // no-op + } + + @Override + public void info(String msg, Throwable t) { + // no-op + } + + @Override + public boolean isWarnEnabled() { + return false; + } + + @Override + public void warn(String msg) { + // no-op + } + + @Override + public void warn(String msg, Object arg) { + // no-op + } + + @Override + public void warn(String msg, Object... arguments) { + // no-op + } + + @Override + public void warn(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void warn(String msg, Throwable t) { + // no-op + } + + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public void error(String msg) { + // no-op + } + + @Override + public void error(String msg, Object arg) { + // no-op + } + + @Override + public void error(String msg, Object arg1, Object arg2) { + // no-op + } + + @Override + public void error(String msg, Object... arguments) { + // no-op + } + + @Override + public void error(String msg, Throwable t) { + // no-op + } + } + + @Override + public Logger getLogger(String className) { + Objects.requireNonNull(className, "Name cannot be null"); + return NO_OP_LOGGER; + } + + @Override + public Logger getLogger(Class<?> clazz) { + Objects.requireNonNull(clazz, "Class cannot be null"); + return NO_OP_LOGGER; + } } - } } |