aboutsummaryrefslogtreecommitdiffstats
path: root/logging-api/src/main/java/org/onap/aai/cl/api/LogLine.java
blob: eb6dcc89be07357b88d7b5999037e71ca3c5ba9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * 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=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.aai.cl.api;

import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.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) : "");
  }
}