From b40a52e5b2c8aaae975bed054408c39f4e3fd0db Mon Sep 17 00:00:00 2001 From: Steve Smokowski Date: Thu, 9 Feb 2017 15:59:56 -0500 Subject: Initial OpenEcomp A&AI Logging Service commit Change-Id: Ie67c5746c2aeafc0fa4318b5ba7ef8af1b3a2eda Signed-off-by: Steve Smokowski --- .../main/java/org/openecomp/cl/mdc/MdcContext.java | 78 ++++++++++++++++++++++ .../java/org/openecomp/cl/mdc/MdcOverride.java | 44 ++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 logging-api/src/main/java/org/openecomp/cl/mdc/MdcContext.java create mode 100644 logging-api/src/main/java/org/openecomp/cl/mdc/MdcOverride.java (limited to 'logging-api/src/main/java/org/openecomp/cl/mdc') diff --git a/logging-api/src/main/java/org/openecomp/cl/mdc/MdcContext.java b/logging-api/src/main/java/org/openecomp/cl/mdc/MdcContext.java new file mode 100644 index 0000000..6e9e6f4 --- /dev/null +++ b/logging-api/src/main/java/org/openecomp/cl/mdc/MdcContext.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * Common Logging Library + * ================================================================================ + * 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.cl.mdc; + +import org.slf4j.MDC; + +import java.net.InetAddress; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * This class manages the MDC (mapped diagnostic context). Calling the init + * method when a new event is processed will save thread-specific context + * information which will be used when generating logs. + */ +public final class MdcContext { + public static String MDC_REQUEST_ID = "RequestId"; + public static String MDC_SERVER_FQDN = "ServerFQDN"; + public static String MDC_SERVICE_NAME = "ServiceName"; + public static String MDC_PARTNER_NAME = "PartnerName"; + public static String MDC_START_TIME = "StartTime"; + public static String MDC_REMOTE_HOST = "RemoteHost"; + public static String MDC_SERVICE_INSTANCE_ID = "ServiceInstanceId"; + public static String MDC_CLIENT_ADDRESS = "ClientAddress"; + + /** + * Initializes the fields of the Mapped Diagnostic Context. + * + * @param transId - Unique transaction identifier. + * @param serviceName - The name of the service generating the diagnostic. + * @param serviceInstance - Unique identifier of the specific instance + * generating the diagnostic. + * @param partnerName - Name of the entity initiating the transction to + * be logged + * @param clientAddress - IP address of the transaction client. + */ + public static void initialize(String transId, + String serviceName, + String serviceInstance, + String partnerName, + String clientAddress) { + MDC.clear(); + MDC.put(MDC_REQUEST_ID, transId); + MDC.put(MDC_SERVICE_NAME, serviceName); + MDC.put(MDC_SERVICE_INSTANCE_ID, serviceInstance); + MDC.put(MDC_PARTNER_NAME, partnerName); + MDC.put(MDC_CLIENT_ADDRESS, clientAddress); + MDC.put(MDC_START_TIME, + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(new Date())); + + try { + MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); + } catch (Exception e) { + // If, for some reason we are unable to get the canonical host name, we + // just want to leave the field unpopulated. There is not much value + // in doing anything else with an exception at this point. + } + + } +} diff --git a/logging-api/src/main/java/org/openecomp/cl/mdc/MdcOverride.java b/logging-api/src/main/java/org/openecomp/cl/mdc/MdcOverride.java new file mode 100644 index 0000000..413ce36 --- /dev/null +++ b/logging-api/src/main/java/org/openecomp/cl/mdc/MdcOverride.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START======================================================= + * Common Logging Library + * ================================================================================ + * 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.cl.mdc; + +import java.util.HashMap; +import java.util.Map; + +/** + * This class stores a map of MDC context attribute/values which can be used to + * override the actual MDC context. + */ +public class MdcOverride { + private Map overrides = new HashMap(); + + public void addAttribute(String attr, String val) { + overrides.put(attr, val); + } + + public String getAttributeValue(String attr) { + return overrides.get(attr); + } + + public boolean hasOverride(String attr) { + return overrides.containsKey(attr); + } +} -- cgit 1.2.3-korg