From a974aa0cfb827476104c140096de676711d2b673 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:31:53 -0500 Subject: Initial OpenECOMP policy/common commit Change-Id: I61cd29d6d8bf8702c1a66915895b519bf3484afa Signed-off-by: Pamela Dragosh --- .../common/logging/flexlogger/EelfLogger.java | 481 ++++++++++++++++++++ .../common/logging/flexlogger/FlexLogger.java | 366 +++++++++++++++ .../logging/flexlogger/FlexLoggerTester.java | 81 ++++ .../policy/common/logging/flexlogger/Logger.java | 226 ++++++++++ .../policy/common/logging/flexlogger/Logger4J.java | 454 +++++++++++++++++++ .../common/logging/flexlogger/LoggerType.java | 30 ++ .../common/logging/flexlogger/PropertyUtil.java | 403 +++++++++++++++++ .../common/logging/flexlogger/SystemOutLogger.java | 497 +++++++++++++++++++++ 8 files changed, 2538 insertions(+) create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/EelfLogger.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLogger.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLoggerTester.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger4J.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/LoggerType.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/PropertyUtil.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/SystemOutLogger.java (limited to 'common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger') diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/EelfLogger.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/EelfLogger.java new file mode 100644 index 00000000..6e74b94c --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/EelfLogger.java @@ -0,0 +1,481 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.util.UUID; + +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import com.att.eelf.configuration.EELFLogger.Level; + +/** + * + * EelfLogger implements all the methods of interface Logger by calling PolicyLogger methods + * + */ + +public class EelfLogger implements Logger { + + private String className = ""; + private String transId = UUID.randomUUID().toString(); + + /** + * Constructor + * @param clazz + */ + public EelfLogger(Class clazz) { + if(clazz != null){ + className = clazz.getName(); + } + PolicyLogger.postMDCInfoForEvent(null); + } + + /** + * Constructor + * @param s + */ + public EelfLogger(String s) { + if(s != null){ + className = s; + } + PolicyLogger.postMDCInfoForEvent(null); + } + + /** + * Constructor + * @param clazz + * @param isNewTransaction + */ + public EelfLogger(Class clazz, boolean isNewTransaction) { + if(clazz != null){ + className = clazz.getName(); + } + if(isNewTransaction){ + transId = PolicyLogger.postMDCInfoForEvent(null); + }else{ + transId = PolicyLogger.getTransId(); + } + } + + /** + * Constructor + * @param s + * @param isNewTransaction + */ + public EelfLogger(String s, boolean isNewTransaction) { + if(s != null){ + className = s; + } + if(isNewTransaction){ + transId = PolicyLogger.postMDCInfoForEvent(null); + }else{ + transId = PolicyLogger.getTransId(); + } + } + + /** + * Constructor + * @param clazz + * @param transId + */ + public EelfLogger(Class clazz, String transId) { + if(clazz != null){ + className = clazz.getName(); + } + PolicyLogger.postMDCInfoForEvent(transId); + } + + /** + * Constructor + * @param s + * @param transId + */ + public EelfLogger(String s, String transId) { + if(s != null){ + className = s; + } + PolicyLogger.postMDCInfoForEvent(transId); + } + + /** + * Sets transaction Id for logging + * @param transId + */ + @Override + public void setTransId(String transId){ + + PolicyLogger.setTransId(transId); + this.transId = transId; + } + + /** + * Returns transaction Id for logging + */ + @Override + public String getTransId(){ + return transId; + } + + /** + * Records a message + * @param message + */ + @Override + public void debug(Object message) { + PolicyLogger.debug(className, ""+message); + } + + /** + * Records an error message + * @param message + */ + @Override + public void error(Object message) { + PolicyLogger.error(className, ""+message); + } + + /** + * Records a message + * @param message + */ + @Override + public void info(Object message) { + PolicyLogger.info(className, ""+message); + } + + /** + * Records a message + * @param message + */ + @Override + public void warn(Object message) { + PolicyLogger.warn(className, ""+message); + } + + /** + * Records a message + * @param message + */ + @Override + public void trace(Object message) { + PolicyLogger.trace(className, ""+message); + } + + /** + * Returns true for debug enabled, or false for not + * @return boolean + */ + @Override + public boolean isDebugEnabled(){ + return PolicyLogger.isDebugEnabled(); + } + + /** + * Returns true for info enabled, or false for not + * @return boolean + */ + @Override + public boolean isInfoEnabled(){ + return PolicyLogger.isInfoEnabled(); + } + + /** + * Returns true for warn enabled, or false for not + * @return boolean + */ + @Override + public boolean isWarnEnabled(){ + return PolicyLogger.isWarnEnabled(); + } + + /** + * Returns true for error enabled, or false for not + * @return boolean + */ + @Override + public boolean isErrorEnabled(){ + return PolicyLogger.isErrorEnabled(); + } + + /** + * Returns true for audit enabled, or false for not + * @return boolean + */ + @Override + public boolean isAuditEnabled(){ + if(PolicyLogger.AUDIT_LEVEL != null && PolicyLogger.AUDIT_LEVEL.toString().equals(Level.OFF.toString())){ + return false; + }else { + return true; + } + } + + /** + * Returns true for metrics enabled, or false for not + * @return boolean + */ + @Override + public boolean isMetricsEnabled(){ + if(PolicyLogger.METRICS_LEVEL != null && PolicyLogger.METRICS_LEVEL.toString().equals(Level.OFF.toString())){ + return false; + }else { + return true; + } + } + + /** + * Returns true for trace enabled, or false for not + * @return boolean + */ + @Override + public boolean isTraceEnabled(){ + return PolicyLogger.isDebugEnabled(); + } + + /** + * Records an audit message + * @param arg0 + */ + @Override + public void audit(Object arg0) { + PolicyLogger.audit(className, ""+ arg0); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void debug(Object message, Throwable t) { + PolicyLogger.debug(MessageCodes.GENERAL_INFO, t, message.toString()); + } + + /** + * Records an error message + * @param message + * @param t + */ + @Override + public void error(Object message, Throwable t) { + PolicyLogger.error(MessageCodes.ERROR_UNKNOWN, t, message.toString()); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void info(Object message, Throwable t) { + PolicyLogger.info(MessageCodes.GENERAL_INFO, t, message.toString()); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void warn(Object message, Throwable t) { + PolicyLogger.warn(MessageCodes.GENERAL_WARNING, t, message.toString()); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void trace(Object message, Throwable t) { + PolicyLogger.trace(message); + } + + /** + * Records an audit message + * @param arg0 + * @param t + */ + @Override + public void audit(Object arg0, Throwable t) { + PolicyLogger.audit(arg0); + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(String eventId) { + PolicyLogger.recordAuditEventStart(eventId); + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(UUID eventId) { + PolicyLogger.recordAuditEventStart(eventId); + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(String eventId, String rule, String policyVersion) { + PolicyLogger.recordAuditEventEnd(eventId, rule, policyVersion); + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule, String policyVersion) { + PolicyLogger.recordAuditEventEnd(eventId, rule, policyVersion); + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(String eventId, String rule) { + PolicyLogger.recordAuditEventEnd(eventId, rule); + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule) { + PolicyLogger.recordAuditEventEnd(eventId, rule); + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(String eventId, String arg1) { + PolicyLogger.recordMetricEvent(eventId, arg1); + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(UUID eventId, String arg1) { + PolicyLogger.recordMetricEvent(eventId, arg1); + } + + /** + * Records a metrics message + * @param arg0 + */ + @Override + public void metrics(Object arg0) { + PolicyLogger.metrics(className, arg0); + } + + /** + * Records an error message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void error(MessageCodes msg, Throwable arg0, String... arguments){ + PolicyLogger.error(msg, className, arg0, arguments); + } + + /** + * Records an error message + * @param msg + * @param arguments + */ + @Override + public void error(MessageCodes msg, String... arguments){ + PolicyLogger.error(msg, arguments); + } + + /** + * Populates MDC Info + * @param transId + */ + @Override + public String postMDCInfoForEvent(String transId) { + return PolicyLogger.postMDCInfoForEvent(transId); + + } + + /** + * Records a message + * @param msg + * @param arguments + */ + @Override + public void warn(MessageCodes msg, String... arguments){ + PolicyLogger.warn(msg, className, arguments); + } + + /** + * Records a message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void warn(MessageCodes msg, Throwable arg0, String... arguments){ + PolicyLogger.warn(msg, className, arg0, arguments); + } + + /** + * Populates MDC Info for the rule triggered + * @param transId + */ + @Override + public void postMDCInfoForTriggeredRule(String transId){ + PolicyLogger.postMDCInfoForTriggeredRule(transId); + } + + /** + * Populates MDC Info + * @param o + */ + @Override + public void postMDCInfoForEvent(Object o){ + PolicyLogger.postMDCInfoForEvent(o); + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLogger.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLogger.java new file mode 100644 index 00000000..4748e34f --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLogger.java @@ -0,0 +1,366 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.Date; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.common.logging.flexlogger.PropertyUtil; +import org.openecomp.policy.common.logging.flexlogger.PropertyUtil.Listener; + +/** + * + * FlexLogger acts as factory to generate instances of Logger based on logger type + * + */ +public class FlexLogger extends SecurityManager{ + + private static LoggerType loggerType = LoggerType.EELF; + private static boolean initLoggerCalled = false; + private static ConcurrentHashMap logger4JMap = new ConcurrentHashMap(); + private static ConcurrentHashMap eelfLoggerMap = new ConcurrentHashMap(); + private static ConcurrentHashMap systemOutMap = new ConcurrentHashMap(); + //--- init logger first + static { + loggerType = initlogger(); + } + + /** + * Returns an instance of Logger + * @param clazz + */ + static public Logger getLogger(Class clazz) { + + if (initLoggerCalled == false) { + loggerType = initlogger(); + } + Logger logger = null; + System.out.println("FlexLogger:getLogger : loggerType = " + loggerType); + switch (loggerType) { + + case EELF: + logger = getEelfLogger(clazz, false); + break; + case LOG4J: + logger = getLog4JLogger(clazz); + break; + case SYSTEMOUT: + logger = getSystemOutLogger(null); + break; + } + + return logger; + + } + + /** + * Returns an instance of Logger + * @param s + */ + static public Logger getLogger(String s) { + + if (initLoggerCalled == false) { + loggerType = initlogger(); + } + Logger logger = null; + System.out.println("FlexLogger:getLogger : loggerType = " + loggerType); + switch (loggerType) { + + case EELF: + logger = getEelfLogger(null,false); + break; + case LOG4J: + logger = getLog4JLogger(s); + break; + case SYSTEMOUT: + logger = getSystemOutLogger(null); + break; + } + + return logger; + + } + + /** + * Returns an instance of Logger + * @param clazz + * @param isNewTransaction + */ + static public Logger getLogger(Class clazz, boolean isNewTransaction) { + + if (initLoggerCalled == false) { + loggerType = initlogger(); + } + Logger logger = null; + System.out.println("FlexLogger:getLogger : loggerType = " + loggerType); + switch (loggerType) { + + case EELF: + logger = getEelfLogger(clazz, isNewTransaction); + break; + case LOG4J: + logger = getLog4JLogger(clazz); + break; + case SYSTEMOUT: + logger = getSystemOutLogger(null); + break; + } + + return logger; + + } + + /** + * Returns an instance of Logger + * @param s + * @param isNewTransaction + */ + static public Logger getLogger(String s, boolean isNewTransaction) { + + if (initLoggerCalled == false) { + loggerType = initlogger(); + } + Logger logger = null; + System.out.println("FlexLogger:getLogger : loggerType = " + loggerType); + switch (loggerType) { + + case EELF: + logger = getEelfLogger(null, isNewTransaction); + break; + case LOG4J: + logger = getLog4JLogger(s); + break; + case SYSTEMOUT: + logger = getSystemOutLogger(null); + break; + } + + return logger; + } + + /** + * Returns the calling class name + */ + public String getClassName(){ + System.out.println("getClassContext()[3].getName() " + getClassContext()[3].getName()); + return getClassContext()[3].getName(); + } + + /** + * Returns an instance of Logger4J + * @param clazz + */ + private static Logger4J getLog4JLogger(Class clazz){ + String className = new FlexLogger().getClassName(); + + if(!logger4JMap.containsKey(className)){ + //for 1610 release use the default debug.log for log4j + Logger4J logger = new Logger4J("debugLogger", className); + logger4JMap.put(className, logger); + } + + return logger4JMap.get(className); + } + + /** + * Returns an instance of Logger4J + * @param s + */ + private static Logger4J getLog4JLogger(String s){ + String className = new FlexLogger().getClassName(); + + if(!logger4JMap.containsKey(className)){ + Logger4J logger = new Logger4J(s, className); + logger4JMap.put(className, logger); + } + + return logger4JMap.get(className); + } + + /** + * Returns an instance of EelfLogger + * @param clazz + * @param isNewTransaction + */ + private static EelfLogger getEelfLogger(Class clazz, boolean isNewTransaction){ + + String className = ""; + EelfLogger logger = null; + if(clazz != null){ + className = clazz.getName(); + }else{ + className = new FlexLogger().getClassName(); + } + + if(!eelfLoggerMap.containsKey(className)){ + logger = new EelfLogger(clazz, isNewTransaction); + eelfLoggerMap.put(className, logger); + }else{ + logger = eelfLoggerMap.get(className); + if(logger == null){ + logger = new EelfLogger(clazz, isNewTransaction); + eelfLoggerMap.put(className, logger); + } + //installl already created but it is new transaction + if(isNewTransaction){ + String transId = PolicyLogger.postMDCInfoForEvent(null); + logger.setTransId(transId); + } + } + System.out.println("eelfLoggerMap size : " + eelfLoggerMap.size() + " class name: " + className); + return logger; + } + + /** + * Returns an instance of SystemOutLogger + * @param clazz + */ + private static SystemOutLogger getSystemOutLogger(Class clazz){ + + String className = new FlexLogger().getClassName(); + + if(!systemOutMap.containsKey(className)){ + SystemOutLogger logger = new SystemOutLogger(className); + systemOutMap.put(className, logger); + } + + return systemOutMap.get(className); + } + + /** + * loads the logger properties + */ + private static LoggerType initlogger() { + LoggerType loggerType = LoggerType.EELF; + String overrideLogbackLevel = "FALSE"; + String logger_Type = ""; + try { + + Properties properties = null; + properties = PropertyUtil.getProperties( + "config/policyLogger.properties"); + System.out.println("FlexLogger:properties => " + properties); + + if(properties != null) { + overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); + System.out.println("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); + logger_Type = properties.getProperty("logger.type"); + if (logger_Type != null){ + + if (logger_Type.equalsIgnoreCase("EELF")){ + + loggerType = LoggerType.EELF; + + }else if (logger_Type.equalsIgnoreCase("LOG4J")){ + + loggerType = LoggerType.LOG4J; + + }else if (logger_Type.equalsIgnoreCase("SYSTEMOUT")){ + + loggerType = LoggerType.SYSTEMOUT; + + } + + System.out.println("FlexLogger.logger_Type value: " + logger_Type); + } + } + //--- only use reload policyLogger.properties file listener for logger type EEFL and overrideLogbackLevel flag is true + if(logger_Type.equalsIgnoreCase("EELF") && overrideLogbackLevel != null && overrideLogbackLevel.equalsIgnoreCase("TRUE")){ + + System.out.println("FlexLogger: start listener."); + properties = PropertyUtil.getProperties( + "config/policyLogger.properties", new PropertiesCallBack( + "FlexLogger-CallBack")); + }else{ + System.out.println("FlexLogger: no listener needed."); + } + + try { + + loggerType = PolicyLogger.init(properties); + initLoggerCalled = true; + + } catch (Exception e) { + System.out.println("initlogger" + e); + } + + } catch (IOException e1) { + System.out.println("initlogger" + e1); + } + + return loggerType; + } + + /** + * PropertiesCallBack is listening any updates on the policyLogger.properties + */ + static public class PropertiesCallBack implements Listener { + String name; + + public PropertiesCallBack(String name) { + this.name = name; + } + + /** + * This method will be called automatically if he policyLogger.properties got updated + */ + public void propertiesChanged(Properties properties, + Set changedKeys) { + + String debugLevel = properties.getProperty("debugLogger.level"); + String metricsLevel = properties.getProperty("metricsLogger.level"); + String auditLevel = properties.getProperty("audit.level"); + String errorLevel = properties.getProperty("error.level"); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); + Instant startTime = Instant.now(); + String formatedTime = sdf.format(Date.from(startTime)); + System.out.println("FlexLogger.propertiesChanged : called at time : " + formatedTime); + System.out.println("FlexLogger.propertiesChanged : debugLevel : " + debugLevel); + + if (changedKeys != null) { + + if (changedKeys.contains("debugLogger.level")) { + PolicyLogger.setDebugLevel(debugLevel); + } + + if (changedKeys.contains("metricsLogger.level")) { + PolicyLogger.setMetricsLevel(metricsLevel); + } + + if (changedKeys.contains("error.level")) { + PolicyLogger.setErrorLevel(errorLevel); + } + + if (changedKeys.contains("audit.level")) { + PolicyLogger.setAuditLevel(auditLevel); + } + } + } + } + +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLoggerTester.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLoggerTester.java new file mode 100644 index 00000000..3e0d558a --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/FlexLoggerTester.java @@ -0,0 +1,81 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.util.UUID; + +public class FlexLoggerTester { + + + public void testLogging(){ + + // get an instance of logger + Logger logger = FlexLogger.getLogger(FlexLoggerTester.class); + + //logger.info("this is a testing of FlexLogger with logger type:" + FlexLogger.loggerType); + + logger.info("logger.isAuditEnabled():" + logger.isAuditEnabled()); + logger.info("logger.isDebugEnabled():" + logger.isDebugEnabled()); + logger.info("logger.isErrorEnabled():" + logger.isErrorEnabled()); + logger.info("logger.isInfoEnabled():" + logger.isInfoEnabled()); + logger.info("logger.isMetricsEnabled():" + logger.isMetricsEnabled()); + logger.info("logger.isWarnEnabled():" + logger.isWarnEnabled()); + + if(logger.isDebugEnabled()) + logger.debug("this is from logger.debug call"); + else + logger.info("this is from logger.info call"); + + if(logger.isMetricsEnabled()) logger.metrics("this is from logger.metrics call"); + + logger.error("this is from logger.error call"); + if(logger.isAuditEnabled()) + logger.audit("this is from logger.audit call"); + else{ + logger.audit("shouldn't see this line in audit log"); + logger.info("shouldn't see this line in audit log"); + } + + if(logger.isMetricsEnabled()) + logger.metrics("this is from logger.metrics call"); + else{ + logger.metrics("shouldn't see this line in metrics log"); + logger.info("shouldn't see this line in metrics log"); + } + + if(logger.isErrorEnabled()) { + logger.error("this is from logger.error call"); + }else{ + logger.error("shouldn't see this logger.error call in error.log"); + logger.info("error is not enabled"); + } + + logger.info("logger.isDebugEnabled() returned value:" + logger.isDebugEnabled()); + logger.recordAuditEventEnd("123345456464998", "from recordAuditEventEnd call", "12345"); + logger.recordAuditEventEnd(UUID.randomUUID(), "from recordAuditEventEnd call", "abcdf"); + logger.recordAuditEventStart("from recordAuditEventStart call"); + logger.recordAuditEventStart(UUID.randomUUID().toString()); + logger.recordMetricEvent("123345456464998", "from recordMetricEvent call"); + logger.recordMetricEvent(UUID.randomUUID(), "from recordMetricEvent call"); + logger.trace("from trace call"); + + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger.java new file mode 100644 index 00000000..ae210081 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger.java @@ -0,0 +1,226 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import static org.openecomp.policy.common.logging.eelf.Configuration.STATUS_CODE; + +import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; +import java.util.UUID; + +import org.slf4j.MDC; + +import org.openecomp.policy.common.logging.eelf.MessageCodes; + +/** + * + * Interface Logger - implemented by Logger4J, EelfLogger and SystemOutLogger + * + */ +public interface Logger { + + /** + * Prints messages with the level.DEBUG + */ + public void debug(Object message); + + /** + * Prints messages with the level.ERROR + */ + public void error(Object message); + + /** + * Prints messages with the level.ERROR + */ + public void error(MessageCodes msg, Throwable arg0, String... arguments); + + /** + * Prints messages with the level.INFO + */ + public void info(Object message); + + /** + * Prints messages with the level.WARN + */ + public void warn(Object message); + + /** + * Prints messages with the level.TRACE + */ + public void trace(Object message); + + /** + * Prints messages in audit log with the level.INFO + */ + public void audit(Object arg0); + + /** + * Prints messages with the level.DEBUG + */ + public void debug(Object message, Throwable t); + + /** + * Prints messages with the level.ERROR + */ + public void error(Object message, Throwable t); + + /** + * Prints messages with the level.INFO + */ + public void info(Object message, Throwable t); + + /** + * Prints messages with the level.WARN + */ + public void warn(Object message, Throwable t); + + /** + * Prints messages with the level.TRACE + */ + public void trace(Object message, Throwable t); + + /** + * Prints messages in audit log with the level.INFO + */ + public void audit(Object arg0, Throwable t); + + /** + * Records event Id in audit log with the level.INFO + */ + public void recordAuditEventStart(String eventId); + + /** + * Records the starting time of the event with its request Id as the key + */ + public void recordAuditEventStart(UUID eventId); + + /** + * Records the ending time of the event with its request Id as the key + */ + public void recordAuditEventEnd(String eventId, String rule, String policyVersion ); + + /** + * Records the ending time of the event with its request Id as the key + */ + public void recordAuditEventEnd(UUID eventId, String rule, String policyVersion); + + /** + * Records the ending time of the event with its request Id as the key + */ + public void recordAuditEventEnd(String eventId, String rule); + + /** + * Records the ending time of the event with its request Id as the key + */ + public void recordAuditEventEnd(UUID eventId, String rule); + + + /** + * Records the Metrics with event Id and log message + */ + public void recordMetricEvent(String eventId, String arg1); + + /** + * Records the Metrics with event Id and log message + */ + public void recordMetricEvent(UUID eventId, String arg1); + + /** + * Records the Metrics log message + */ + public void metrics(Object arg0); + + /** + * Returns a boolean value, true for debug logging enabled, false for not enabled + */ + public boolean isDebugEnabled(); + + /** + * Returns a boolean value, true for error logging enabled, false for not enabled + */ + public boolean isErrorEnabled(); + + /** + * Returns a boolean value, true for warn logging enabled, false for not enabled + */ + public boolean isWarnEnabled(); + + /** + * Returns a boolean value, true for info logging enabled, false for not enabled + */ + public boolean isInfoEnabled(); + + /** + * Returns a boolean value, true for error logging enabled, false for not enabled + */ + public boolean isAuditEnabled(); + + /** + * Returns a boolean value, true for warn logging enabled, false for not enabled + */ + public boolean isMetricsEnabled(); + + /** + * Returns a boolean value, true for trace logging enabled, false for not enabled + */ + public boolean isTraceEnabled(); + + + /** + * Populates MDC info + */ + public String postMDCInfoForEvent(String transId); + + /** + * Prints messages with the level.WARN + */ + public void warn(MessageCodes msg, String... arguments) ; + + /** + * Prints messages with the level.WARN + */ + public void warn(MessageCodes msg, Throwable arg0, String... arguments) ; + + /** + * Prints messages with the level.ERROR + */ + public void error(MessageCodes msg, String... arguments) ; + + /** + * Sets transaction Id + */ + public void setTransId(String transId); + + /** + * Returns transaction Id + */ + String getTransId(); + + /** + * Populates MDC Info for the rule triggered + */ + public void postMDCInfoForTriggeredRule(String transId); + + /** + * Populates MDC Info + */ + public void postMDCInfoForEvent(Object o); + +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger4J.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger4J.java new file mode 100644 index 00000000..02f95cf2 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/Logger4J.java @@ -0,0 +1,454 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.util.UUID; + +import org.apache.log4j.Logger; +import org.apache.log4j.Priority; + +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import com.att.eelf.configuration.EELFLogger.Level; + +/** + * + * Logger4J implements all the methods of interface Logger by calling org.apache.log4j.Logger + * + */ +public class Logger4J implements org.openecomp.policy.common.logging.flexlogger.Logger { + + private Logger log = null; + private String methodName = ""; + private String className = ""; + private String transId = UUID.randomUUID().toString(); + + /** + * Constructor + * @param clazz + */ + public Logger4J (Class clazz){ + System.out.println("create instance of Logger4J"); + if(clazz != null){ + log = Logger.getLogger(clazz); + className = clazz.getName(); + } + } + + /** + * Constructor + * @param s + * @param className + */ + public Logger4J (String s, String className){ + System.out.println("create instance of Logger4J"); + if(s != null){ + log = Logger.getLogger(s); + } + this.className = className; + } + + /** + * Sets transaction Id + */ + @Override + public void setTransId(String transId){ + log.info(transId); + this.transId = transId; + } + + /** + * Returns transaction Id + */ + @Override + public String getTransId(){ + return transId; + } + + /** + * Records a message + * @param message + */ + @Override + public void debug(Object message) { + if(isDebugEnabled()){ + log.debug(transId + "|" + message); + } + } + + /** + * Records an error message + * @param message + */ + @Override + public void error(Object message) { + log.error( transId + "|" + className +"|" + message); + } + + /** + * Records a message + * @param message + */ + @Override + public void info(Object message) { + log.info( transId + "|" + className +"|" + message); + } + + /** + * Records a message + * @param message + */ + @Override + public void warn(Object message) { + log.warn( transId + "|" + className +"|" + message); + } + + /** + * Records a message + * @param message + */ + @Override + public void trace(Object message) { + log.trace(transId + "|"+ className +"|" + message); + } + + /** + * Returns true for debug enabled, or false for not + * @return boolean + */ + @Override + public boolean isDebugEnabled(){ + return log.isDebugEnabled(); + } + + /** + * Returns true for error enabled, or false for not + * @return boolean + */ + @SuppressWarnings("deprecation") + @Override + public boolean isErrorEnabled(){ + return log.isEnabledFor(Priority.ERROR); + } + + /** + * Returns true for info enabled, or false for not + * @return boolean + */ + @Override + public boolean isInfoEnabled(){ + return log.isInfoEnabled(); + } + + /** + * Returns true for warn enabled, or false for not + * @return boolean + */ + @SuppressWarnings("deprecation") + @Override + public boolean isWarnEnabled(){ + //return log4j value + return log.isEnabledFor(Priority.WARN); + } + + /** + * Returns true for audit enabled, or false for not + * @return boolean + */ + @Override + public boolean isAuditEnabled(){ + if(PolicyLogger.AUDIT_LEVEL != null && PolicyLogger.AUDIT_LEVEL.toString().equals(Level.OFF.toString())){ + return false; + }else { + return true; + } + } + + /** + * Returns true for metrics enabled, or false for not + * @return boolean + */ + @Override + public boolean isMetricsEnabled(){ + if(PolicyLogger.METRICS_LEVEL != null && PolicyLogger.METRICS_LEVEL.toString().equals(Level.OFF.toString())){ + return false; + }else { + return true; + } + } + + /** + * Records an audit message + * @param arg0 + */ + @Override + public void audit(Object arg0) { + log.info(className +"|" +arg0); + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(String eventId) { + log.info(className +"|recordAuditEventStart with eventId " + eventId); + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(UUID eventId) { + if(eventId != null){ + recordAuditEventStart(eventId.toString()); + } + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(String eventId, String rule, String policyVersion) { + log.info(className +"|"+ eventId + ":" + rule); + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule, String policyVersion) { + if(eventId != null){ + recordAuditEventEnd(eventId.toString(), rule, policyVersion); + }else{ + recordAuditEventEnd(eventId, rule, policyVersion); + } + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(String eventId, String rule) { + log.info(className +"|" +eventId + ":" + rule); + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule) { + if(eventId != null){ + recordAuditEventEnd(eventId.toString(), rule); + }else{ + recordAuditEventEnd(eventId, rule); + } + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(String eventId, String arg1) { + log.info(className +"|" +eventId + ":" + arg1); + + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(UUID eventId, String arg1) { + if(eventId != null){ + recordMetricEvent(eventId.toString(), arg1); + }else{ + recordMetricEvent(eventId, arg1); + } + } + + /** + * Records a metrics message + * @param arg0 + */ + @Override + public void metrics(Object arg0) { + log.info(arg0); + } + + /** + * Records an error message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void error(MessageCodes msg, Throwable arg0, String... arguments){ + log.error(transId + "|" + className +"|" + "MessageCodes :" + msg + arguments); + + } + + /** + * Records an error message + * @param msg + * @param arguments + */ + @Override + public void error(MessageCodes msg, String... arguments){ + log.error(transId + "|" + className +"|" + "MessageCode:" + msg + arguments); + } + + /** + * Returns transaction Id + * @param transId + */ + @Override + public String postMDCInfoForEvent(String transId) { + if(transId == null || transId.isEmpty()){ + transId = UUID.randomUUID().toString(); + } + + return transId; + } + + /** + * Records a message + * @param msg + * @param arguments + */ + @Override + public void warn(MessageCodes msg, String... arguments){ + log.warn(className +"|" +"MessageCodes:" + msg + arguments); + } + + /** + * Records a message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void warn(MessageCodes msg, Throwable arg0, String... arguments){ + log.warn(className +"|" +"MessageCodes:" + msg + arguments); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void debug(Object message, Throwable t) { + log.debug(message, t); + } + + /** + * Records an error message + * @param message + * @param t + */ + @Override + public void error(Object message, Throwable t) { + log.error(message, t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void info(Object message, Throwable t) { + log.info(message, t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void warn(Object message, Throwable t) { + log.warn(message, t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void trace(Object message, Throwable t) { + log.trace(message, t); + } + + /** + * Records an audit message + * @param arg0 + * @param t + */ + + @Override + public void audit(Object arg0, Throwable t) { + log.info(arg0, t); + } + + /** + * Returns true for trace enabled, or false for not + * @return boolean + */ + @Override + public boolean isTraceEnabled() { + return log.isTraceEnabled(); + } + + /** + * Records transaction Id + * @param transId + */ + @Override + public void postMDCInfoForTriggeredRule(String transId){ + log.info(transId); + } + + /** + * Records transaction Id + * @param o + */ + @Override + public void postMDCInfoForEvent(Object o){ + log.info(o); + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/LoggerType.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/LoggerType.java new file mode 100644 index 00000000..8fe55b29 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/LoggerType.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +/** + * + * Logger types + * + */ +public enum LoggerType { + EELF, LOG4J, SYSTEMOUT +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/PropertyUtil.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/PropertyUtil.java new file mode 100644 index 00000000..462e10cb --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/PropertyUtil.java @@ -0,0 +1,403 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Properties; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; + +/** + * This class provides utilities to read properties from a properties + * file, and optionally get notifications of future changes + */ +public class PropertyUtil +{ + /** + * Read in a properties file + * @param file the properties file + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + static public Properties getProperties(File file) throws IOException + { + // create an InputStream (may throw a FileNotFoundException) + FileInputStream fis = new FileInputStream(file); + try + { + // create the properties instance + Properties rval = new Properties(); + + // load properties (may throw an IOException) + rval.load(fis); + return(rval); + } + finally + { + // close input stream + fis.close(); + } + } + + /** + * Read in a properties file + * @param fileName the properties file + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + static public Properties getProperties(String fileName) throws IOException + { + return(getProperties(new File(fileName))); + } + + /* ============================================================ */ + + // timer thread used for polling for property file changes + private static Timer timer = null; + + /** + * This is the callback interface, used for sending notifications of + * changes in the properties file. + */ + public interface Listener + { + /** + * Notification of a properties file change + * @param properties the new properties + * @param the set of property names that have changed, including + * additions and removals + */ + void propertiesChanged(Properties properties, Set changedKeys); + } + + // this table maps canonical file into a 'ListenerRegistration' instance + static private HashMap registrations = + new HashMap(); + + /** + * This is an internal class - one instance of this exists for each + * property file that is being monitored. Note that multiple listeners + * can be registered for the same file. + */ + private static class ListenerRegistration + { + // the canonical path of the file being monitored + File file; + + // the most recent value of 'file.lastModified()' + long lastModified; + + // the most recent set of properties + Properties properties; + + // the set of listeners monitoring this file + LinkedList listeners; + + // the 'TimerTask' instance, used for periodic polling + TimerTask timerTask; + + /** + * Constructor - create a 'ListenerRegistration' instance for this + * file, but with no listeners + */ + ListenerRegistration(File file) throws IOException + { + this.file = file; + + // The initial value of 'lastModified' is set to 0 to ensure that we + // correctly handle the case where the file is modified within the + // same second that polling begins. + lastModified = 0; + + // fetch current properties + properties = getProperties(file); + + // no listeners yet + listeners = new LinkedList(); + + // add to static table, so this instance can be shared + registrations.put(file, this); + + if (timer == null) + { + // still need to create a timer thread + synchronized(PropertyUtil.class) + { + // an additional check is added inside the 'synchronized' block, + // just in case someone beat us to it + if (timer == null) + { + timer = new Timer("PropertyUtil-Timer", true); + } + } + } + + // create and schedule the timer task, so this is periodically polled + timerTask = new TimerTask() + { + public void run() + { + try + { + poll(); + } + catch (Exception e) + { + System.err.println(e); + } + } + }; + timer.schedule(timerTask, 10000L, 10000L); + } + + /** + * Add a listener to the notification list + * @param listener this is the listener to add to the list + * @return the properties at the moment the listener was added to the list + */ + synchronized Properties addListener(Listener listener) + { + listeners.add(listener); + return((Properties)properties.clone()); + } + + /** + * Remove a listener from the notification list + * @param listener this is the listener to remove + */ + synchronized void removeListener(Listener listener) + { + listeners.remove(listener); + + // See if we need to remove this 'ListenerRegistration' instance + // from the table. The 'synchronized' block is needed in case + // another listener is being added at about the same time that this + // one is being removed. + synchronized(registrations) + { + if (listeners.size() == 0) + { + timerTask.cancel(); + registrations.remove(file); + } + } + } + + /** + * This method is periodically called to check for property list updates + * @throws IOException if there is an error in reading the properties file + */ + synchronized void poll() throws IOException + { + long timestamp = file.lastModified(); + if (timestamp != lastModified) + { + // update the record, and send out the notifications + lastModified = timestamp; + + // Save old set, and initial set of changed properties. + Properties oldProperties = properties; + HashSet changedProperties = + new HashSet(oldProperties.stringPropertyNames()); + + // Fetch the list of listeners that we will potentially notify, + // and the new properties. Note that this is in a 'synchronized' + // block to ensure that all listeners receiving notifications + // actually have a newer list of properties than the one + // returned on the initial 'getProperties' call. + properties = getProperties(file); + + Set newPropertyNames = properties.stringPropertyNames(); + changedProperties.addAll(newPropertyNames); + + // At this point, 'changedProperties' is the union of all properties + // in both the old and new properties files. Iterate through all + // of the entries in the new properties file - if the entry + // matches the one in the old file, remove it from + // 'changedProperties'. + for (String name : newPropertyNames) + { + if (properties.getProperty(name).equals + (oldProperties.getProperty(name))) + { + // Apparently, any property that exists must be of type + // 'String', and can't be null. For this reason, we don't + // need to worry about the case where + // 'properties.getProperty(name)' returns 'null'. Note that + // 'oldProperties.getProperty(name)' may be 'null' if the + // old property does not exist. + changedProperties.remove(name); + } + } + + // 'changedProperties' should be correct at this point + if (changedProperties.size() != 0) + { + // there were changes - notify everyone in 'listeners' + for (final Listener notify : listeners) + { + // Copy 'properties' and 'changedProperties', so it doesn't + // cause problems if the recipient makes changes. + final Properties tmpProperties = + (Properties)(properties.clone()); + final HashSet tmpChangedProperties = + new HashSet(changedProperties); + + // Do the notification in a separate thread, so blocking + // won't cause any problems. + new Thread() + { + public void run() + { + notify.propertiesChanged + (tmpProperties, tmpChangedProperties); + } + }.start(); + } + } + } + } + } + + /** + * Read in a properties file, and register for update notifications. + * NOTE: it is possible that the first callback will occur while this + * method is still in progress. To avoid this problem, use 'synchronized' + * blocks around this invocation and in the callback -- that will ensure + * that the processing of the initial properties complete before any + * updates are processed. + * + * @param file the properties file + * @param notify if not null, this is a callback interface that is used for + * notifications of changes + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + static public Properties getProperties(File file, Listener listener) + throws IOException + { + if (listener == null) + { + // no listener specified -- just fetch the properties + return(getProperties(file)); + } + + // Convert the file to a canonical form in order to avoid the situation + // where different names refer to the same file. + file = file.getCanonicalFile(); + + // See if there is an existing registration. The 'synchronized' block + // is needed to handle the case where a new listener is added at about + // the same time that another one is being removed. + synchronized(registrations) + { + ListenerRegistration reg = registrations.get(file); + if (reg == null) + { + // a new registration is needed + reg = new ListenerRegistration(file); + } + return(reg.addListener(listener)); + } + } + + /** + * Read in a properties file, and register for update notifications. + * NOTE: it is possible that the first callback will occur while this + * method is still in progress. To avoid this problem, use 'synchronized' + * blocks around this invocation and in the callback -- that will ensure + * that the processing of the initial properties complete before any + * updates are processed. + * + * @param fileName the properties file + * @param notify if not null, this is a callback interface that is used for + * notifications of changes + * @return a Properties object, containing the associated properties + * @throws IOException - subclass 'FileNotFoundException' if the file + * does not exist or can't be opened, and 'IOException' if there is + * a problem loading the properties file. + */ + static public Properties getProperties(String fileName, Listener listener) + throws IOException + { + return(getProperties(new File(fileName), listener)); + } + + /** + * Stop listenening for updates + * @param file the properties file + * @param notify if not null, this is a callback interface that was used for + * notifications of changes + */ + static public void stopListening(File file, Listener listener) + { + if (listener != null) + { + ListenerRegistration reg = registrations.get(file); + if (reg != null) + { + reg.removeListener(listener); + } + } + } + + /** + * Stop listenening for updates + * @param fileName the properties file + * @param notify if not null, this is a callback interface that was used for + * notifications of changes + */ + static public void stopListening(String fileName, Listener listener) + { + stopListening(new File(fileName), listener); + } + + /* ============================================================ */ + + // TEMPORARY - used to test callback interface + static public class Test implements Listener + { + String name; + + public Test(String name) + { + this.name = name; + } + + public void propertiesChanged(Properties properties, Set changedKeys) + { + System.out.println("Test(" + name + ")\nproperties = " + properties + + "\nchangedKeys = " + changedKeys); + } + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/SystemOutLogger.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/SystemOutLogger.java new file mode 100644 index 00000000..26f06640 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/flexlogger/SystemOutLogger.java @@ -0,0 +1,497 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.common.logging.flexlogger; + +import java.util.UUID; + +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import com.att.eelf.configuration.EELFLogger.Level; + +/** + * + * SystemOutLogger implements all the methods of interface Logger by calling System.out.println + * + */ +public class SystemOutLogger implements Logger { + + private String className = ""; + private boolean isDebugEnabled = true; + private boolean isInfoEnabled = true; + private boolean isWarnEnabled = true; + private boolean isErrorEnabled = true; + private boolean isAuditEnabled = true; + private boolean isMetricsEnabled = true; + private String transId = UUID.randomUUID().toString(); + + /** + * Constructor + * @param clazz + */ + public SystemOutLogger (Class clazz){ + System.out.println("create instance of SystemOutLogger"); + if(clazz != null){ + className = clazz.getName(); + } + initLevel(); + } + + /** + * Constructor + * @param s + */ + public SystemOutLogger (String s){ + System.out.println("create instance of SystemOutLogger"); + if(s != null){ + className = s; + } + initLevel(); + } + + /** + * Sets logging levels + */ + private void initLevel(){ + + if(PolicyLogger.DEBUG_LEVEL != null && PolicyLogger.DEBUG_LEVEL.toString().equals(Level.DEBUG.toString())){ + isDebugEnabled = true; + isInfoEnabled = true; + isWarnEnabled = true; + }else{ + isDebugEnabled = false; + } + + if(PolicyLogger.DEBUG_LEVEL != null && PolicyLogger.DEBUG_LEVEL.toString().equals(Level.INFO.toString())){ + isInfoEnabled = true; + isWarnEnabled = true; + isDebugEnabled = false; + } + + if(PolicyLogger.DEBUG_LEVEL != null && PolicyLogger.DEBUG_LEVEL.toString().equals(Level.OFF.toString())){ + isInfoEnabled = false; + isWarnEnabled = false; + isDebugEnabled = false; + } + + if(PolicyLogger.ERROR_LEVEL != null && PolicyLogger.ERROR_LEVEL.toString().equals(Level.OFF.toString())){ + isErrorEnabled = false; + } + + if(PolicyLogger.AUDIT_LEVEL != null && PolicyLogger.AUDIT_LEVEL.toString().equals(Level.OFF.toString())){ + isAuditEnabled = false; + } + + if(PolicyLogger.METRICS_LEVEL != null && PolicyLogger.METRICS_LEVEL.toString().equals(Level.OFF.toString())){ + isMetricsEnabled = false; + } + } + + /** + * Sets transaction Id + */ + @Override + public void setTransId(String transId){ + + System.out.println(transId); + this.transId = transId; + } + + /** + * Returns transaction Id + */ + @Override + public String getTransId(){ + + return transId; + } + + /** + * Records a message + * @param message + */ + @Override + public void debug(Object message) { + + System.out.println(transId + "|" + className+" : "+message); + } + + /** + * Records an error message + * @param message + */ + @Override + public void error(Object message) { + + System.out.println(transId + "|" + className+" : "+message); + } + + /** + * Records a message + * @param message + */ + @Override + public void info(Object message) { + + System.out.println(transId + "|" + className+" : "+message); + + } + + /** + * Records a message + * @param message + */ + @Override + public void warn(Object message) { + + System.out.println(transId + "|" + className+" : "+message); + } + + /** + * Records a message + * @param message + */ + @Override + public void trace(Object message) { + + System.out.println(transId + "|" + className+" : "+message); + } + + /** + * Returns true for debug enabled, or false for not + * @return boolean + */ + @Override + public boolean isDebugEnabled(){ + + return isDebugEnabled; + } + + /** + * Returns true for warn enabled, or false for not + * @return boolean + */ + @Override + public boolean isWarnEnabled(){ + + return isWarnEnabled; + } + + /** + * Returns true for info enabled, or false for not + * @return boolean + */ + @Override + public boolean isInfoEnabled(){ + + return isInfoEnabled; + } + + /** + * Returns true for error enabled, or false for not + * @return boolean + */ + @Override + public boolean isErrorEnabled(){ + + return isErrorEnabled; + } + + /** + * Returns true for audit enabled, or false for not + * @return boolean + */ + @Override + public boolean isAuditEnabled(){ + + return isAuditEnabled; + } + + /** + * Returns true for metrics enabled, or false for not + * @return boolean + */ + @Override + public boolean isMetricsEnabled(){ + + return isMetricsEnabled; + } + + /** + * Records an audit message + * @param arg0 + */ + @Override + public void audit(Object arg0) { + + System.out.println(transId + "|" +className+" : "+arg0); + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(String eventId) { + + System.out.println(transId + "|" +className+" : "+eventId); + + } + + /** + * Records an audit message + * @param eventId + */ + @Override + public void recordAuditEventStart(UUID eventId) { + + System.out.println(eventId); + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(String eventId, String rule, String policyVersion) { + + System.out.println(className+" : "+eventId + ":" + rule + ":" + policyVersion); + } + + /** + * Records an audit message + * @param eventId + * @param rule + * @param policyVersion + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule, String policyVersion) { + + System.out.println(className+" : "+eventId + ":" + rule + ":" + policyVersion); + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(String eventId, String rule) { + + System.out.println(className+" : "+eventId + ":" + rule); + } + + /** + * Records an audit message + * @param eventId + * @param rule + */ + @Override + public void recordAuditEventEnd(UUID eventId, String rule) { + + System.out.println(className+" : "+eventId + ":" + rule); + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(String eventId, String arg1) { + + System.out.println(className+" : "+"eventId:" + ":" + eventId + "message:" + arg1); + + } + + /** + * Records a metrics message + * @param eventId + * @param arg1 + */ + @Override + public void recordMetricEvent(UUID eventId, String arg1) { + + System.out.println(className+" : "+eventId + ":" + arg1); + } + + /** + * Records a metrics message + * @param arg0 + */ + @Override + public void metrics(Object arg0) { + + System.out.println(className+" : "+arg0); + } + + /** + * Records an error message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void error(MessageCodes msg, Throwable arg0, String... arguments){ + + System.out.println(className+" : "+"MessageCodes :" + msg + arguments); + + } + + /** + * Records an error message + * @param msg + * @param arguments + */ + @Override + public void error(MessageCodes msg, String... arguments){ + + System.out.println(transId + "|" + className+" : "+"MessageCode:" + msg + arguments); + } + + /** + * Returns transaction Id + * @param transId + */ + @Override + public String postMDCInfoForEvent(String transId) { + + if(transId == null || transId.isEmpty()){ + transId = UUID.randomUUID().toString(); + } + + return transId; + } + + /** + * Records a message + * @param msg + * @param arguments + */ + @Override + public void warn(MessageCodes msg, String... arguments){ + + System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + arguments); + } + + /** + * Records a message + * @param msg + * @param arg0 + * @param arguments + */ + @Override + public void warn(MessageCodes msg, Throwable arg0, String... arguments){ + + System.out.println(transId + "|" + className+" : "+"MessageCodes:" + msg + arguments); + + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void debug(Object message, Throwable t) { + System.out.println(transId + "|" + className+" : "+ message + ":" + t); + } + + /** + * Records an error message + * @param message + * @param t + */ + @Override + public void error(Object message, Throwable t) { + System.out.println(transId + "|" + className+" : "+ message + ":" + t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void info(Object message, Throwable t) { + System.out.println(transId + "|" + className+" : "+ message + ":" + t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void warn(Object message, Throwable t) { + System.out.println(transId + "|" + className+" : "+ message + ":" + t); + } + + /** + * Records a message + * @param message + * @param t + */ + @Override + public void trace(Object message, Throwable t) { + System.out.println(transId + "|" + className+" : "+ message + ":" + t); + } + + /** + * Records an audit message + * @param arg0 + * @param t + */ + @Override + public void audit(Object arg0, Throwable t) { + System.out.println(transId + "|" + className+" : "+ arg0 + ":" + t); + } + + /** + * Returns true for trace enabled, or false for not + * @return boolean + */ + @Override + public boolean isTraceEnabled() { + // default + return false; + } + + /** + * Records transaction Id + * @param transId + */ + @Override + public void postMDCInfoForTriggeredRule(String transId){ + + System.out.println(transId); + } + + /** + * Records transaction Id + * @param o + */ + @Override + public void postMDCInfoForEvent(Object o){ + System.out.println(o); + } +} -- cgit 1.2.3-korg