From 783df3831052b84552b7691f4006fb845ac79ba7 Mon Sep 17 00:00:00 2001 From: "Brittany Plummer (bp896r)" Date: Tue, 3 Mar 2020 11:50:53 -0500 Subject: Added new annotation for ScheduledLogging Issue-ID: LOG-1208 Change-Id: If6d8f8c18affcec8a11eab877f6f716ed34909ba Signed-off-by: Brittany Plummer (bp896r) --- .../filter/base/AbstractMDCSetupAspect.java | 78 ++++++++++++++++++++++ .../org/onap/logging/filter/base/ErrorCode.java | 40 +++++++++++ .../onap/logging/filter/base/ScheduledLogging.java | 32 +++++++++ .../filter/base/ScheduledTaskException.java | 45 +++++++++++++ .../filter/base/ScheduledTasksMDCSetupAspect.java | 47 +++++++++++++ 5 files changed, 242 insertions(+) create mode 100644 reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java create mode 100644 reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java create mode 100644 reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java create mode 100644 reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java create mode 100644 reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java (limited to 'reference/logging-filter/logging-filter-base/src/main/java') diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java new file mode 100644 index 0000000..c9424c4 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/AbstractMDCSetupAspect.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.logging.filter.base; + +import java.util.UUID; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.MDC; + +public abstract class AbstractMDCSetupAspect extends MDCSetup { + + public abstract void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable; + + protected void setupMDC(String methodName) { + try { + setEntryTimeStamp(); + setServerFQDN(); + String partnerName = getProperty(Constants.Property.PARTNER_NAME); + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, UUID.randomUUID().toString()); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString()); + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, UUID.randomUUID().toString()); + MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, partnerName); + MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, Constants.DefaultValues.UNKNOWN); + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, methodName); + setLogTimestamp(); + setElapsedTime(); + MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName); + logger.info(ONAPLogConstants.Markers.ENTRY, "Entering"); + } catch (Exception e) { + logger.warn("Error in ScheduledTasksMDCSetup: {}", e.getMessage()); + } + } + + protected void exitAndClearMDC() { + try { + setStatusCode(); + setLogTimestamp(); + setElapsedTime(); + logger.info(ONAPLogConstants.Markers.EXIT, "Exiting."); + } catch (Exception e) { + logger.warn("Error in ScheduledTasksMDCSetup clear MDC: {}", e.getMessage()); + } + MDC.clear(); + } + + protected void setStatusCode() { + String currentStatusCode = MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE); + if (currentStatusCode == null || !currentStatusCode.equals(ONAPLogConstants.ResponseStatus.ERROR.toString())) { + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.COMPLETE.toString()); + } + } + + public void errorMDCSetup(ErrorCode errorCode, String errorDescription) { + MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(errorCode.getValue())); + MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString()); + } + +} diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java new file mode 100644 index 0000000..3935ccd --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ErrorCode.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.logging.filter.base; + +public enum ErrorCode { + PermissionError(100), + AvailabilityError(200), + DataError(300), + SchemaError(400), + BusinessProcessError(500), + UnknownError(900); + + private int value; + + ErrorCode(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } +} diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java new file mode 100644 index 0000000..39e5653 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledLogging.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.logging.filter.base; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; +import java.lang.annotation.RetentionPolicy; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ScheduledLogging { + +} diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java new file mode 100644 index 0000000..499d452 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTaskException.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.logging.filter.base; + +public class ScheduledTaskException extends Exception { + + private static final long serialVersionUID = 1L; + private ErrorCode errorCode; + + public ScheduledTaskException(ErrorCode errorCode, String errorMessage, Throwable cause) { + super(errorMessage, cause); + this.setErrorCode(errorCode); + } + + public ScheduledTaskException(ErrorCode errorCode, String errorMessage) { + super(errorMessage); + this.setErrorCode(errorCode); + } + + public ErrorCode getErrorCode() { + return errorCode; + } + + public void setErrorCode(ErrorCode errorCode) { + this.errorCode = errorCode; + } +} diff --git a/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java new file mode 100644 index 0000000..74642b1 --- /dev/null +++ b/reference/logging-filter/logging-filter-base/src/main/java/org/onap/logging/filter/base/ScheduledTasksMDCSetupAspect.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - Logging + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.logging.filter.base; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.onap.logging.filter.base.AbstractMDCSetupAspect; +import org.onap.logging.filter.base.ScheduledTaskException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Aspect +public class ScheduledTasksMDCSetupAspect extends AbstractMDCSetupAspect { + + protected static Logger logger = LoggerFactory.getLogger(ScheduledTasksMDCSetupAspect.class); + + @Around("@annotation(ScheduledLogging)") + public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { + setupMDC(joinPoint.getSignature().getName()); + try { + joinPoint.proceed(); + } catch (ScheduledTaskException e) { + errorMDCSetup(e.getErrorCode(), e.getMessage()); + logger.error("ScheduledTaskException: ", e); + } + exitAndClearMDC(); + } +} -- cgit 1.2.3-korg