57d3eae884b0438ff17faa9ccf2b1469caa11794
[platform-oam.git] / logging-demo / src / main / java / org / acumos / demo / logging / util / ACUMOSLogConstants.java
1 /*
2   ===============LICENSE_START=======================================================
3   Acumos
4   ===================================================================================
5   Copyright (C) 2019 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6   ===================================================================================
7   This Acumos software file is distributed by AT&T and Tech Mahindra
8   under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11    
12            http://www.apache.org/licenses/LICENSE-2.0
13    
14   This file is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18   ===============LICENSE_END=========================================================
19   */
20 package org.acumos.demo.logging.util;
21
22 import org.slf4j.Marker;
23 import org.slf4j.MarkerFactory;
24 import org.slf4j.MDC;
25
26
27 /**
28  * Constants for standard ACUMOS headers, MDCs, etc.
29  *
30  */
31 public final class ACUMOSLogConstants {
32
33         /**
34          * Hide and forbid construction.
35          */
36         private ACUMOSLogConstants() {
37                 throw new UnsupportedOperationException();
38         }
39
40         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41         //
42         // Inner classes.
43         //
44         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45
46         /**
47          * Marker constants.
48          */
49         public static final class Markers {
50
51                 /** Marker reporting invocation. */
52                 public static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
53
54                 /** Marker reporting invocation. */
55                 public static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE_RETURN");
56
57                 /** Marker reporting synchronous invocation. */
58                 public static final Marker INVOKE_SYNCHRONOUS = build("INVOKE", "SYNCHRONOUS");
59
60                 /** Marker reporting asynchronous invocation. */
61                 public static final Marker INVOKE_ASYNCHRONOUS = build("INVOKE", "ASYNCHRONOUS");
62
63                 /** Marker reporting entry into a component. */
64                 public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
65
66                 /** Marker reporting exit from a component. */
67                 public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
68
69                 /**
70                  * Build nested, detached marker.
71                  * 
72                  * @param m1
73                  *            top token.
74                  * @param m2
75                  *            sub-token.
76                  * @return detached Marker.
77                  */
78                 private static Marker build(final String m1, final String m2) {
79                         final Marker marker = MarkerFactory.getDetachedMarker(m1);
80                         marker.add(MarkerFactory.getDetachedMarker(m2));
81                         return marker;
82                 }
83
84                 /**
85                  * Hide and forbid construction.
86                  */
87                 private Markers() {
88                         throw new UnsupportedOperationException();
89                 }
90         }
91
92         /**
93          * MDC name constants.
94          */
95         public static final class MDCs {
96
97                 // Tracing. ////////////////////////////////////////////////////////////
98
99                 /** MDC correlating messages for a logical transaction. */
100                 public static final String REQUEST_ID = "RequestID";
101
102                 /** MDC recording target service. */
103                 public static final String TARGET_SERVICE_NAME = "TargetServiceName";
104
105                 /** MDC recording target entity. */
106                 public static final String TARGET_ENTITY = "TargetEntity";
107
108                 // Network. ////////////////////////////////////////////////////////////
109
110                 /** MDC recording caller address. */
111                 public static final String CLIENT_IP_ADDRESS = "ClientIPAddress";
112
113                 /** MDC recording server address. */
114                 public static final String SERVER_FQDN = "ServerFQDN";
115
116                 /** MDC reporting outcome code. */
117                 public static final String RESPONSE_CODE = "ResponseCode";
118
119                 /** MDC reporting outcome description. */
120                 public static final String RESPONSE_DESCRIPTION = "ResponseDescription";
121
122                 /** MDC reporting outcome error level. */
123                 public static final String RESPONSE_SEVERITY = "Severity";
124
125                 /** MDC reporting outcome status of the request. */
126                 public static final String STATUS_CODE = "StatusCode";
127
128                 // Unsorted. ///////////////////////////////////////////////////////////
129
130                 /**
131                  * Hide and forbid construction.
132                  */
133                 private MDCs() {
134                         throw new UnsupportedOperationException();
135                 }
136         }
137
138         /**
139          * Header name constants.
140          */
141         public static final class Headers {
142
143                 /** HTTP X-ACUMOS-RequestID header. */
144                 public static final String REQUEST_ID = "X-ACUMOS-RequestID";
145
146                 /**
147                  * Hide and forbid construction.
148                  */
149                 private Headers() {
150                         throw new UnsupportedOperationException();
151                 }
152         }
153
154         /**
155          * Overrideable method to set MDCs based on property values.
156          */
157         public static void setDefaultMDCs() {
158                 MDC.put(MDCs.RESPONSE_CODE, "200");
159                 MDC.put(MDCs.RESPONSE_DESCRIPTION, "200 OK");
160                 MDC.put(MDCs.RESPONSE_SEVERITY, ResponseSeverity.INFO.toString());
161                 MDC.put(MDCs.STATUS_CODE, ResponseStatus.INPROGRESS.toString());
162         }
163
164         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165         //
166         // Enums.
167         //
168         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169
170         /**
171          * Response success or not, for setting StatusCode.
172          */
173         public enum ResponseStatus {
174                 
175                 COMPLETED,
176                 ERROR,
177                 INPROGRESS
178         }
179
180         /**
181          * Response of log level, for setting Severity.
182          */
183         public enum ResponseSeverity {
184
185                 INFO,
186                 ERROR,
187                 TRACE,
188                 DEBUG,
189                 WARN,
190                 FATAL
191         }
192
193         /**
194          * Synchronous or asynchronous execution, for setting invocation marker.
195          */
196         public enum InvocationMode {
197
198                 /** Synchronous, blocking. */
199                 SYNCHRONOUS("SYNCHRONOUS", Markers.INVOKE_SYNCHRONOUS),
200
201                 /** Asynchronous, non-blocking. */
202                 ASYNCHRONOUS("ASYNCHRONOUS", Markers.INVOKE_ASYNCHRONOUS);
203
204                 /** Enum value. */
205                 private String mString;
206
207                 /** Corresponding marker. */
208                 private Marker mMarker;
209
210                 /**
211                  * Construct enum.
212                  *
213                  * @param s
214                  *            enum value.
215                  * @param m
216                  *            corresponding Marker.
217                  */
218                 InvocationMode(final String s, final Marker m) {
219                         this.mString = s;
220                         this.mMarker = m;
221                 }
222
223                 /**
224                  * Get Marker for enum.
225                  *
226                  * @return Marker.
227                  */
228                 public Marker getMarker() {
229                         return this.mMarker;
230                 }
231
232                 @Override
233                 public String toString() {
234                         return this.mString;
235                 }
236         }
237
238 }