Release 1.9.2 DCAEGEN2 VESCollector container
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / VESLogger.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed 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  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae.common;
23
24 import com.att.nsa.clock.SaClock;
25 import com.att.nsa.logging.LoggingContext;
26 import com.att.nsa.logging.LoggingContextFactory;
27 import com.att.nsa.logging.log4j.EcompFields;
28 import java.util.UUID;
29
30 public class VESLogger {
31
32
33     public static final String REQUEST_ID = "requestId";
34
35     // Common LoggingContext
36     private static LoggingContext commonLC;
37     // Thread-specific LoggingContext
38     private static LoggingContext threadLC;
39
40     /**
41      * Returns the common LoggingContext instance that is the base context for
42      * all subsequent instances.
43      *
44      * @return the common LoggingContext
45      */
46     public static LoggingContext getCommonLoggingContext() {
47         if (commonLC == null) {
48             commonLC = new LoggingContextFactory.Builder().build();
49             final UUID uuid = UUID.randomUUID();
50
51             commonLC.put(REQUEST_ID, uuid.toString());
52         }
53         return commonLC;
54     }
55
56     /**
57      * Get a logging context for the current thread that's based on the common
58      * logging context. Populate the context with context-specific values.
59      *
60      * @param aUuid uuid for request id
61      * @return a LoggingContext for the current thread
62      */
63     public static LoggingContext getLoggingContextForThread(UUID aUuid) {
64         // note that this operation requires everything from the common context
65         // to be (re)copied into the target context. That seems slow, but it
66         // actually
67         // helps prevent the thread from overwriting supposedly common data. It
68         // also
69         // should be fairly quick compared with the overhead of handling the
70         // actual
71         // service call.
72
73         threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
74         // Establish the request-specific UUID, as long as we are here...
75         threadLC.put(REQUEST_ID, aUuid.toString());
76         threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
77
78         return threadLC;
79     }
80
81     /**
82      * Get a logging context for the current thread that's based on the common
83      * logging context. Populate the context with context-specific values.
84      *
85      * @param aUuid uuid for request id
86      * @return a LoggingContext for the current thread
87      */
88     public static LoggingContext getLoggingContextForThread(String aUuid) {
89         // note that this operation requires everything from the common context
90         // to be (re)copied into the target context. That seems slow, but it
91         // actually
92         // helps prevent the thread from overwriting supposedly common data. It
93         // also
94         // should be fairly quick compared with the overhead of handling the
95         // actual
96         // service call.
97
98         threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build();
99         // Establish the request-specific UUID, as long as we are here...
100         threadLC.put(REQUEST_ID, aUuid);
101         threadLC.put("statusCode", "COMPLETE");
102         threadLC.put(EcompFields.kEndTimestamp, SaClock.now());
103         return threadLC;
104     }
105
106 }