2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dcae.common;
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;
30 public class VESLogger {
33 public static final String REQUEST_ID = "requestId";
35 // Common LoggingContext
36 private static LoggingContext commonLC;
37 // Thread-specific LoggingContext
38 private static LoggingContext threadLC;
41 * Returns the common LoggingContext instance that is the base context for
42 * all subsequent instances.
44 * @return the common LoggingContext
46 public static LoggingContext getCommonLoggingContext() {
47 if (commonLC == null) {
48 commonLC = new LoggingContextFactory.Builder().build();
49 final UUID uuid = UUID.randomUUID();
51 commonLC.put(REQUEST_ID, uuid.toString());
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.
60 * @param aUuid uuid for request id
61 * @return a LoggingContext for the current thread
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
67 // helps prevent the thread from overwriting supposedly common data. It
69 // should be fairly quick compared with the overhead of handling the
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());
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.
85 * @param aUuid uuid for request id
86 * @return a LoggingContext for the current thread
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
92 // helps prevent the thread from overwriting supposedly common data. It
94 // should be fairly quick compared with the overhead of handling the
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());