From d28008f80696a2f3d12287928d26be332e77f31b Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Fri, 9 Jun 2023 00:12:26 +0530 Subject: [PATCH] Add DeviceConnectivityLogs --- .../operation/mgt/OperationManagerImpl.java | 12 +- .../logger/DeviceConnectivityLogContext.java | 70 ++++ .../EntgraDeviceConnectivityLoggerImpl.java | 318 ++++++++++++++++++ .../logger/util/MDCContextUtil.java | 14 +- 4 files changed, 406 insertions(+), 8 deletions(-) create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceConnectivityLoggerImpl.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7373e52e5f..b037eeebb7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -19,9 +19,11 @@ package io.entgra.device.mgt.core.device.mgt.core.operation.mgt; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.DeviceConnectivityLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceConnectivityLoggerImpl; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraPolicyLoggerImpl; import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.ActivityPaginationRequest; @@ -91,7 +93,8 @@ import java.util.concurrent.ThreadPoolExecutor; */ public class OperationManagerImpl implements OperationManager { - private static final Log log = LogFactory.getLog(OperationManagerImpl.class); + DeviceConnectivityLogContext.Builder deviceConnectivityLogContextBuilder = new DeviceConnectivityLogContext.Builder(); + private static final EntgraLogger log = new EntgraDeviceConnectivityLoggerImpl(OperationManagerImpl.class); private static final int CACHE_VALIDITY_PERIOD = 5 * 60 * 1000; private static final String NOTIFIER_TYPE_LOCAL = "LOCAL"; private static final String SYSTEM = "system"; @@ -713,7 +716,7 @@ public class OperationManagerImpl implements OperationManager { DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceId, tenantId); break; } - + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(deviceId.getId()).setDeviceType(deviceId.getType()).build()); return getOperations(deviceId, Operation.Status.PENDING, enrolmentId); } @@ -740,6 +743,7 @@ public class OperationManagerImpl implements OperationManager { DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, device, tenantId); break; } + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(device.getDeviceIdentifier()).setDeviceType(device.getType()).build()); return getOperations(deviceIdentifier, Operation.Status.PENDING, enrolmentId); } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java new file mode 100644 index 0000000000..813814165a --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceConnectivityLogContext.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.notification.logger; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; + +public class DeviceConnectivityLogContext extends LogContext { + + private final String deviceId; + private final String deviceType; + + private DeviceConnectivityLogContext(Builder builder) { + this.deviceId = builder.deviceId; + this.deviceType = builder.deviceType; + } + + public String getDeviceId() { + return deviceId; + } + + public String getDeviceType() { + return deviceType; + } + + public static class Builder { + private String deviceId; + private String deviceType; + + public Builder() { + } + + public String getDeviceId() { + return deviceId; + } + + public Builder setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; + } + + public String getDeviceType() { + return deviceType; + } + + public Builder setDeviceType(String deviceType) { + this.deviceType = deviceType; + return this; + } + + public DeviceConnectivityLogContext build() { + return new DeviceConnectivityLogContext(this); + } + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceConnectivityLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceConnectivityLoggerImpl.java new file mode 100644 index 0000000000..aaa62bf488 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceConnectivityLoggerImpl.java @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.notification.logger.impl; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.DeviceConnectivityLogContext; +import io.entgra.device.mgt.core.notification.logger.util.MDCContextUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.MDC; + +public class EntgraDeviceConnectivityLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraDeviceConnectivityLoggerImpl(Class clazz) { + log = LogFactory.getLog(clazz); + } + + public void info(String message) { + log.info(message); + } + + public void info(String message, Throwable t) { + log.info(message, t); + } + + @Override + public void info(Object o) { + log.info(o); + } + + @Override + public void info(Object o, Throwable throwable) { + log.info(o, throwable); + } + + @Override + public void info(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.info(object, t); + } + + public void debug(String message) { + log.debug(message); + } + + public void debug(String message, Throwable t) { + log.debug(message, t); + } + + @Override + public void debug(Object o) { + log.debug(o); + } + + @Override + public void debug(Object o, Throwable throwable) { + log.debug(o, throwable); + } + + @Override + public void debug(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.debug(object, t); + } + + public void error(String message) { + log.error(message); + } + + public void error(String message, Throwable t) { + log.error(message, t); + } + + @Override + public void error(Object o) { + log.error(o); + } + + @Override + public void error(Object o, Throwable throwable) { + log.error(o, throwable); + } + + @Override + public void error(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.error(object, t); + } + + public void warn(String message) { + log.warn(message); + } + + public void warn(String message, Throwable t) { + log.warn(message, t); + } + + @Override + public void warn(Object o) { + log.warn(o); + } + + @Override + public void warn(Object o, Throwable throwable) { + log.warn(o, throwable); + } + + @Override + public void warn(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.warn(object, t); + } + + public void trace(String message) { + log.trace(message); + } + + public void trace(String message, Throwable t) { + log.trace(message, t); + } + + @Override + public void trace(Object o) { + log.trace(o); + } + + @Override + public void trace(Object o, Throwable throwable) { + log.trace(o, throwable); + } + + @Override + public void trace(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.trace(object, t); + } + + public void fatal(String message) { + log.fatal(message); + } + + public void fatal(String message, Throwable t) { + log.fatal(message, t); + } + + @Override + public void fatal(Object o) { + log.fatal(0); + } + + @Override + public void fatal(Object o, Throwable throwable) { + log.fatal(0, throwable); + } + + @Override + public void fatal(String message, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + DeviceConnectivityLogContext deviceConnectivityLogContext = (DeviceConnectivityLogContext) logContext; + MDCContextUtil.populateDeviceConnectivityMDCContext(deviceConnectivityLogContext); + log.fatal(object, t); + } + + @Override + public boolean isDebugEnabled() { + return log.isDebugEnabled(); + } + + @Override + public boolean isErrorEnabled() { + return log.isErrorEnabled(); + } + + @Override + public boolean isFatalEnabled() { + return log.isFatalEnabled(); + } + + @Override + public boolean isInfoEnabled() { + return log.isInfoEnabled(); + } + + @Override + public boolean isTraceEnabled() { + return log.isTraceEnabled(); + } + + @Override + public boolean isWarnEnabled() { + return log.isWarnEnabled(); + } + + @Override + public void clearLogContext() { + MDC.clear(); + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java index 0f639c3776..5387c1f5c0 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java @@ -17,10 +17,7 @@ */ package io.entgra.device.mgt.core.notification.logger.util; -import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; -import io.entgra.device.mgt.core.notification.logger.DeviceLogContext; -import io.entgra.device.mgt.core.notification.logger.PolicyLogContext; -import io.entgra.device.mgt.core.notification.logger.UserLogContext; +import io.entgra.device.mgt.core.notification.logger.*; import org.apache.log4j.MDC; public final class MDCContextUtil { @@ -94,6 +91,15 @@ public final class MDCContextUtil { MDC.put("Action", mdcContext.getAction()); } } + + public static void populateDeviceConnectivityMDCContext(final DeviceConnectivityLogContext mdcContext) { + if (mdcContext.getDeviceId() != null) { + MDC.put("DeviceId", mdcContext.getDeviceId()); + } + if (mdcContext.getDeviceType() != null) { + MDC.put("DeviceType", mdcContext.getDeviceType()); + } + } }