From 313dce44ae1ab2328c33a6448e87b3fbb81b15f0 Mon Sep 17 00:00:00 2001 From: olegb Date: Mon, 19 Feb 2018 16:11:35 +0200 Subject: Changed audit API of logger Change-Id: I3e091ba7ce592fae536da1eaf28a220284b846c3 Issue-ID: SDC-772 Signed-off-by: olegb --- .../org/openecomp/sdc/logging/api/AuditData.java | 50 ++++++++++ .../java/org/openecomp/sdc/logging/api/Logger.java | 14 +-- .../openecomp/sdc/logging/api/LoggerFactory.java | 25 +---- .../sdc/logging/api/LoggerFactoryTest.java | 2 +- .../openecomp/sdc/logging/api/SpyAuditData.java | 63 +++++++++++++ .../sdc/logging/slf4j/SLF4JLoggerWrapper.java | 70 ++++++++++---- .../logging/slf4j/SLF4JLoggingServiceProvider.java | 19 ++-- .../openecomp/sdc/logging/LogFileCreationTest.java | 101 +++++++++++++++++++++ .../org/openecomp/sdc/logging/api/LoggerTest.java | 61 ------------- .../sdc/logging/aspects/MetricsAspectTest.java | 23 +---- 10 files changed, 287 insertions(+), 141 deletions(-) create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/api/LoggerTest.java (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java new file mode 100644 index 0000000000..7292762474 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/AuditData.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.logging.api; + +/** + * @author KATYR + * @since February 15, 2018 + * This interface defines part of the Audit log application is responsible to provide. + * Fields list is according to ONAP application logging guidelines + * (https://wiki.onap.org/download/attachments/1015849/ONAP%20application%20logging%20guidelines.pdf?api=v2) + * StartTime -> BeginTimestamp (Date-time that processing for the activities begins) + * EndTime-> EndTimestamp (Date-time that processing for the activities being logged ends) + * StatusCode -> StatusCode (indicate high level success or failure of the operation activities that is invoked) + * ResponseCode -> ResponseCode(application-specific response code returned by the operation activities) + * ResponseDescription - > ResponseDescription (human readable description of the response code) + * ClientIpAddress -> ClientIpAddress (Requesting remote client application’s IP address) + */ + +public interface AuditData { + + enum StatusCode { + COMPLETE, ERROR + } + + long getStartTime(); + + long getEndTime(); + + StatusCode getStatusCode(); + + String getResponseCode(); + + String getResponseDescription(); + + String getClientIpAddress(); +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/Logger.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/Logger.java index cf5bdf6721..d02e99f9a2 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/Logger.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/Logger.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,15 +43,7 @@ public interface Logger { boolean isAuditEnabled(); - void audit(String msg); - - void audit(String msg, Object arg); - - void audit(String msg, Object arg1, Object arg2); - - void audit(String msg, Object... arguments); - - void audit(String msg, Throwable t); + void audit(AuditData data); boolean isDebugEnabled(); @@ -100,4 +92,4 @@ public interface Logger { void error(String msg, Object... arguments); void error(String msg, Throwable t); -} +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java index 826f6d509f..399fd3717e 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,27 +99,7 @@ public class LoggerFactory { } @Override - public void audit(String msg) { - // no-op - } - - @Override - public void audit(String msg, Object arg) { - // no-op - } - - @Override - public void audit(String msg, Object arg1, Object arg2) { - // no-op - } - - @Override - public void audit(String msg, Object... arguments) { - // no-op - } - - @Override - public void audit(String msg, Throwable t) { + public void audit(AuditData data) { // no-op } @@ -257,3 +237,4 @@ public class LoggerFactory { } } } + diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java index 9fde4e5e6c..e174a32271 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggerFactoryTest.java @@ -70,7 +70,7 @@ public class LoggerFactoryTest { logger.warn(""); logger.info(""); logger.debug(""); - logger.audit(""); + logger.audit(new SpyAuditData()); logger.metrics(""); } } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java new file mode 100644 index 0000000000..8766f2598b --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/SpyAuditData.java @@ -0,0 +1,63 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ +package org.openecomp.sdc.logging.api; + +import java.util.HashSet; +import java.util.Set; + +public class SpyAuditData implements AuditData{ + private final Set calledMethods = new HashSet<>(); + + @Override + public long getStartTime() { + calledMethods.add("getStartTime"); + return 0; + } + + @Override + public long getEndTime() { + calledMethods.add("getEndTime"); + return 0; + } + + @Override + public AuditData.StatusCode getStatusCode() { + calledMethods.add("getEndTime"); + return null; + } + + @Override + public String getResponseCode() { + calledMethods.add("getResponseCode"); + return null; + } + + @Override + public String getResponseDescription() { + calledMethods.add("getResponseDescription"); + return null; + } + + @Override + public String getClientIpAddress() { + calledMethods.add("getClientIpAddress"); + return null; + } + + public boolean wasCalled(String method) { + return calledMethods.contains(method); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java index 5d223450bd..416af8fff1 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggerWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,15 @@ package org.openecomp.sdc.logging.slf4j; +import org.openecomp.sdc.logging.api.AuditData; import org.openecomp.sdc.logging.api.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.openecomp.sdc.logging.slf4j.SLF4JLoggingServiceProvider.PREFIX; /** * @author EVITALIY @@ -25,6 +32,16 @@ import org.slf4j.LoggerFactory; */ class SLF4JLoggerWrapper implements Logger { + private static final String BEGIN_TIMESTAMP = PREFIX + "BeginTimestamp"; + private static final String END_TIMESTAMP = PREFIX + "EndTimestamp"; + private static final String ELAPSED_TIME = PREFIX + "ElapsedTime"; + private static final String STATUS_CODE = PREFIX + "StatusCode"; + private static final String RESPONSE_CODE = PREFIX + "ResponseCode"; + private static final String RESPONSE_DESCRIPTION = PREFIX + "ResponsDescription"; + private static final String CLIENT_IP_ADDRESS = PREFIX + "ClientIpAddress"; + + //The specified format presents time in UTC formatted per ISO 8601, as required by ONAP logging guidelines + private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); private final org.slf4j.Logger logger; SLF4JLoggerWrapper(Class clazz) { @@ -76,28 +93,43 @@ class SLF4JLoggerWrapper implements Logger { } @Override - public void audit(String msg) { - logger.info(Markers.AUDIT, msg); - } + public void audit(AuditData data) { - @Override - public void audit(String msg, Object arg) { - logger.info(Markers.AUDIT, msg, arg); - } + if (data == null) { + return; + } - @Override - public void audit(String msg, Object arg1, Object arg2) { - logger.info(Markers.AUDIT, msg, arg1, arg2); - } + MDC.put(BEGIN_TIMESTAMP, DATE_FORMAT.format(new Date(data.getStartTime()))); + MDC.put(END_TIMESTAMP, DATE_FORMAT.format(new Date(data.getEndTime()))); + MDC.put(ELAPSED_TIME, String.valueOf(data.getEndTime() - data.getStartTime())); - @Override - public void audit(String msg, Object... arguments) { - logger.info(Markers.AUDIT, msg, arguments); - } + if (data.getStatusCode() != null) { + MDC.put(STATUS_CODE, data.getStatusCode() == AuditData.StatusCode.COMPLETE ? "COMPLETE" : "ERROR"); + } - @Override - public void audit(String msg, Throwable t) { - logger.info(Markers.AUDIT, msg, t); + if (data.getResponseCode() != null) { + MDC.put(RESPONSE_CODE, data.getResponseCode()); + } + + if (data.getResponseDescription() != null) { + MDC.put(RESPONSE_DESCRIPTION, data.getResponseDescription()); + } + + if (data.getClientIpAddress() != null) { + MDC.put(CLIENT_IP_ADDRESS, data.getClientIpAddress()); + } + + try { + logger.info(Markers.AUDIT, ""); + } finally { + MDC.remove(BEGIN_TIMESTAMP); + MDC.remove(END_TIMESTAMP); + MDC.remove(ELAPSED_TIME); + MDC.remove(STATUS_CODE); + MDC.remove(RESPONSE_CODE); + MDC.remove(RESPONSE_DESCRIPTION); + MDC.remove(CLIENT_IP_ADDRESS); + } } @Override diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java index d2fb0b0226..86b2297371 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/SLF4JLoggingServiceProvider.java @@ -1,12 +1,12 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * 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. @@ -16,20 +16,25 @@ package org.openecomp.sdc.logging.slf4j; +import java.util.Objects; +import java.util.concurrent.Callable; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.spi.LoggingServiceProvider; import org.slf4j.MDC; -import java.util.Objects; -import java.util.concurrent.Callable; - /** * @author evitaliy * @since 13/09/2016. */ public class SLF4JLoggingServiceProvider implements LoggingServiceProvider { + public static final String PREFIX = ""; private static final String KEY_CANNOT_BE_NULL = "Key cannot be null"; + private static final String REQUEST_ID = PREFIX + "RequestId"; + private static final String SERVICE_NAME = PREFIX + "ServiceName"; + private static final String PARTNER_NAME = PREFIX + "PartnerName"; + + private static final String[] ALL_FIELDS = { REQUEST_ID, SERVICE_NAME, PARTNER_NAME }; @Override public Logger getLogger(String className) { @@ -69,12 +74,14 @@ public class SLF4JLoggingServiceProvider implements LoggingServiceProvider { @Override public Runnable copyToRunnable(Runnable runnable) { Objects.requireNonNull(runnable, "Runnable cannot be null"); + // TODO: Copy only the fields this service is responsible for return new MDCRunnableWrapper(runnable); } @Override public Callable copyToCallable(Callable callable) { Objects.requireNonNull(callable, "Runnable cannot be null"); + // TODO: Copy only the fields this service is responsible for return new MDCCallableWrapper<>(callable); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java new file mode 100644 index 0000000000..4595e567c8 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/LogFileCreationTest.java @@ -0,0 +1,101 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.logging; + +import org.openecomp.sdc.logging.api.AuditData; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.testng.annotations.Test; + +public class LogFileCreationTest { + private static final boolean ENABLED = false; // for manual testing change to 'true' + private static final Logger LOGGER = LoggerFactory.getLogger(LogFileCreationTest.class); + + @Test(enabled = ENABLED) + public void testMetrics() { + LOGGER.metrics("This is metrics"); + } + + @Test(enabled = ENABLED) + public void testAudit() { + SpyAuditData auditData = new SpyAuditData(); + LOGGER.audit(auditData); + } + + @Test(enabled = ENABLED) + public void testDebug() { + LOGGER.debug("This is debug"); + } + + @Test(enabled = ENABLED) + public void testInfo() { + LOGGER.info("This is info"); + } + + @Test(enabled = ENABLED) + public void testWarn() { + LOGGER.warn("This is warning"); + } + + @Test(enabled = ENABLED) + public void testError() { + LOGGER.error("This is error"); + } + + private class SpyAuditData implements AuditData { + @Override + public long getStartTime() { + + return 0; + + } + + @Override + public long getEndTime(){ + + return 0; + } + + @Override + public StatusCode getStatusCode(){ + + return null; + + } + + @Override + public String getResponseCode(){ + + return null; + + } + + @Override + public String getResponseDescription(){ + + return null; + + } + + @Override + public String getClientIpAddress(){ + + return null; + + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/api/LoggerTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/api/LoggerTest.java deleted file mode 100644 index 1359a1ff2a..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/api/LoggerTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright © 2016-2017 European Support Limited - * - * 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. - */ - -package org.openecomp.sdc.logging.api; - -import org.testng.annotations.Test; - -/** - * This is only for manual testing - change {@link #ENABLED} to 'true' - * - * @author evitaliy - * @since 13/09/2016. - */ -public class LoggerTest { - - private static final boolean ENABLED = false; // for manual testing change to 'true' - private static final Logger LOGGER = LoggerFactory.getLogger(LoggerTest.class); - - @Test(enabled = ENABLED) - public void testMetrics() { - LOGGER.metrics("This is metrics"); - } - - @Test(enabled = ENABLED) - public void testAudit() { - LOGGER.audit("This is audit"); - } - - @Test(enabled = ENABLED) - public void testDebug() { - LOGGER.debug("This is debug"); - } - - @Test(enabled = ENABLED) - public void testInfo() { - LOGGER.info("This is info"); - } - - @Test(enabled = ENABLED) - public void testWarn() { - LOGGER.warn("This is warning"); - } - - @Test(enabled = ENABLED) - public void testError() { - LOGGER.error("This is error"); - } -} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java index 42cbdcef14..4d3d6be34b 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/aspects/MetricsAspectTest.java @@ -21,6 +21,7 @@ import org.aspectj.lang.Signature; import org.aspectj.lang.reflect.SourceLocation; import org.aspectj.runtime.internal.AroundClosure; import org.easymock.EasyMock; +import org.openecomp.sdc.logging.api.AuditData; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.powermock.api.easymock.PowerMock; @@ -296,27 +297,7 @@ public class MetricsAspectTest extends PowerMockTestCase { } @Override - public void audit(String var1) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void audit(String var1, Object var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void audit(String var1, Object var2, Object var3) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void audit(String var1, Object... var2) { - throw new RuntimeException("Not implemented"); - } - - @Override - public void audit(String var1, Throwable throwable) { + public void audit(AuditData var1) { throw new RuntimeException("Not implemented"); } -- cgit 1.2.3-korg