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/api/LogLine.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 logging-api/src/main/java/org/openecomp/cl/api/LogLine.java (limited to 'logging-api/src/main/java/org/openecomp/cl/api/LogLine.java') diff --git a/logging-api/src/main/java/org/openecomp/cl/api/LogLine.java b/logging-api/src/main/java/org/openecomp/cl/api/LogLine.java new file mode 100644 index 0000000..2b6f30f --- /dev/null +++ b/logging-api/src/main/java/org/openecomp/cl/api/LogLine.java @@ -0,0 +1,115 @@ +/*- + * ============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.api; + +import org.openecomp.cl.mdc.MdcContext; +import org.openecomp.cl.mdc.MdcOverride; + +import org.slf4j.MDC; + +/** + * This class is used to help standardize how log lines are written and provide + * profiling info. + */ +public abstract class LogLine { + + public static enum LogLineType { + AUDIT, ERROR, METRICS + } + + /** + * Enumerates the predefined fields of the log line. Note that this + * enumeration only exposes those fields that the client may set via the + * {@link LogFields} object. Fields which are automatically populated by the + * logging service or sourced from the {@link MdcContext} do not appear here. + */ + public enum DefinedFields { + + STATUS_CODE, + RESPONSE_CODE, + RESPONSE_DESCRIPTION, + INSTANCE_UUID, + SEVERITY, + SERVER_IP, + CLIENT_IP, + CLASS_NAME, + PROCESS_KEY, + TARGET_SVC_NAME, + TARGET_ENTITY, + ERROR_CODE, + ERROR_DESCRIPTION, + CUSTOM_1, + CUSTOM_2, + CUSTOM_3, + CUSTOM_4; + } + + protected String component = ""; + protected String logCode = ""; + protected String level = ""; + protected String message = ""; + protected MdcOverride override = new MdcOverride(); + protected LogFields fields = new LogFields(); + + /** + * Sets common values that the log line will use for populating the log + * string. + * + * @param component + * - The entity invoking the log. + * @param logCode + * - String version of the log message code. + * @param level + * - Log level (DEBUG, TRACE, INFO, WARN, ERROR...) + * @param msg + * - The log message + * @param fields + * - A map of predefined log line fields to values. + * @param override + * - Structure which overrides selective fields in the + * {@link MdcContext} + */ + public void init(String component, String logCode, String level, String msg, LogFields fields, + MdcOverride override) { + + this.component = component; + this.logCode = logCode; + this.level = level; + this.message = msg; + this.override = override; + this.fields = fields; + } + + protected String getMdcValue(String attribute) { + if (override.hasOverride(attribute)) { + return override.getAttributeValue(attribute); + } + + String value = (String) MDC.get(attribute) == null ? "" : (String) MDC.get(attribute); + return value; + } + + public abstract String getFormattedLine(); + + protected String fieldValue(Enum field) { + return (fields.fieldIsSet(field) ? fields.getField(field) : ""); + } +} -- cgit 1.2.3-korg