2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License 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 * ================================================================================
20 package org.openecomp.portalsdk.core.logging.aspect;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
25 import javax.servlet.http.HttpServletRequest;
27 import org.openecomp.portalsdk.core.logging.format.AuditLogFormatter;
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.openecomp.portalsdk.core.service.AppService;
30 import org.openecomp.portalsdk.core.util.SystemProperties;
31 import org.openecomp.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
33 import org.springframework.beans.factory.annotation.Autowired;
35 import com.att.eelf.configuration.Configuration;
37 @org.springframework.context.annotation.Configuration
38 public class EELFLoggerAdvice {
41 AppService appService;
43 EELFLoggerDelegate adviceLogger = EELFLoggerDelegate.getLogger(EELFLoggerAdvice.class);
45 // DateTime Format according to the ECOMP Application Logging Guidelines.
46 private static final SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
49 * Gets the current date and time in expected ECOMP log format.
51 * @return Current date and time
53 public static String getCurrentDateTimeUTC() {
54 String currentDateTime = ecompLogDateFormat.format(new Date());
55 return currentDateTime;
60 * @param securityEventType
63 * @return One-element array containing an empty String object.
65 public Object[] before(SecurityEventTypeEnum securityEventType, Object[] args, Object[] passOnArgs) {
67 String className = "";
68 if (passOnArgs[0] != null) {
69 className = passOnArgs[0].toString();
72 String methodName = "";
73 if (passOnArgs[1] != null) {
74 methodName = passOnArgs[1].toString();
77 String appName = appService.getDefaultAppName();
78 if (appName == null || appName == "") {
79 appName = SystemProperties.SDK_NAME;
82 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
84 // Initialize Request defaults only for controller methods.
85 MDC.put(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
86 MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
87 MDC.put(SystemProperties.TARGET_SERVICE_NAME, methodName);
88 if (securityEventType != null) {
89 MDC.put(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, getCurrentDateTimeUTC());
90 HttpServletRequest req = null;
91 if (args[0] != null && args[0] instanceof HttpServletRequest) {
92 req = (HttpServletRequest) args[0];
93 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(req, appName);
96 logger.debug(EELFLoggerDelegate.debugLogger, (methodName + " was invoked."));
97 } catch (Exception e) {
98 adviceLogger.error(EELFLoggerDelegate.errorLogger,
99 "Exception occurred in EELFLoggerAdvice.before() method. Details: " + e.getMessage());
102 return new Object[] { "" };
107 * @param securityEventType
113 public void after(SecurityEventTypeEnum securityEventType, String result, Object[] args, Object[] returnArgs,
114 Object[] passOnArgs) {
116 String className = "";
117 if (passOnArgs[0] != null) {
118 className = passOnArgs[0].toString();
121 String methodName = "";
122 if (passOnArgs[1] != null) {
123 methodName = passOnArgs[1].toString();
126 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(className);
128 String appName = appService.getDefaultAppName();
129 if (appName == null || appName == "") {
130 appName = SystemProperties.SDK_NAME;
133 if (MDC.get(SystemProperties.TARGET_SERVICE_NAME) == null
134 || MDC.get(SystemProperties.TARGET_SERVICE_NAME) == "") {
135 MDC.put(SystemProperties.TARGET_SERVICE_NAME, methodName);
138 if (MDC.get(SystemProperties.TARGET_ENTITY) == null || MDC.get(SystemProperties.TARGET_ENTITY) == "") {
139 MDC.put(SystemProperties.TARGET_ENTITY, appName + "_BE");
142 MDC.put(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP,
143 MDC.get(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP));
144 MDC.put(SystemProperties.METRICSLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
145 this.calculateDateTimeDifference(MDC.get(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP),
146 MDC.get(SystemProperties.METRICSLOG_END_TIMESTAMP));
148 logger.info(EELFLoggerDelegate.metricsLogger, methodName + " operation is completed.");
149 logger.debug(EELFLoggerDelegate.debugLogger, "Finished executing " + methodName + ".");
151 if (securityEventType != null) {
153 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP,
154 MDC.get(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP));
155 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, getCurrentDateTimeUTC());
156 this.calculateDateTimeDifference(MDC.get(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
157 MDC.get(SystemProperties.AUDITLOG_END_TIMESTAMP));
159 this.logSecurityMessage(logger, securityEventType, result, methodName);
161 // clear when finishes audit logging
162 MDC.remove(Configuration.MDC_KEY_REQUEST_ID);
163 MDC.remove(SystemProperties.PARTNER_NAME);
164 MDC.remove(SystemProperties.MDC_LOGIN_ID);
165 MDC.remove(SystemProperties.PROTOCOL);
166 MDC.remove(SystemProperties.FULL_URL);
167 MDC.remove(Configuration.MDC_SERVICE_NAME);
168 MDC.remove(SystemProperties.RESPONSE_CODE);
169 MDC.remove(SystemProperties.STATUS_CODE);
170 MDC.remove(className + methodName + SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
171 MDC.remove(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
172 MDC.remove(SystemProperties.AUDITLOG_END_TIMESTAMP);
175 MDC.remove(className + methodName + SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
176 MDC.remove(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP);
177 MDC.remove(SystemProperties.METRICSLOG_END_TIMESTAMP);
178 MDC.remove(SystemProperties.MDC_TIMER);
179 MDC.remove(SystemProperties.TARGET_ENTITY);
180 MDC.remove(SystemProperties.TARGET_SERVICE_NAME);
181 } catch (Exception e) {
182 adviceLogger.error(EELFLoggerDelegate.errorLogger,
183 "Exception occurred in EELFLoggerAdvice.after() method. Details: " + e.getMessage());
190 * @param securityEventType
194 private void logSecurityMessage(EELFLoggerDelegate logger, SecurityEventTypeEnum securityEventType, String result,
196 StringBuilder additionalInfoAppender = new StringBuilder();
197 String auditMessage = "";
199 additionalInfoAppender.append(String.format("%s request was received.", restMethod));
202 MDC.put(SystemProperties.STATUS_CODE, result);
204 String fullURL = MDC.get(SystemProperties.FULL_URL);
205 if (fullURL != null && fullURL != "") {
206 additionalInfoAppender.append(" Request-URL:" + MDC.get(SystemProperties.FULL_URL));
209 auditMessage = AuditLogFormatter.getInstance().createMessage(MDC.get(SystemProperties.PROTOCOL),
210 securityEventType.name(), MDC.get(SystemProperties.MDC_LOGIN_ID), additionalInfoAppender.toString());
212 logger.info(EELFLoggerDelegate.auditLogger, auditMessage);
217 * @param beginDateTime
220 private void calculateDateTimeDifference(String beginDateTime, String endDateTime) {
221 if (beginDateTime != null && endDateTime != null) {
223 Date beginDate = ecompLogDateFormat.parse(beginDateTime);
224 Date endDate = ecompLogDateFormat.parse(endDateTime);
225 String timeDifference = String.format("%d ms", endDate.getTime() - beginDate.getTime());
226 MDC.put(SystemProperties.MDC_TIMER, timeDifference);
227 } catch (Exception e) {
228 adviceLogger.error(EELFLoggerDelegate.errorLogger,
229 "Exception occurred in EELFLoggerAdvice.calculateDateTimeDifference() method. Details: "