diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 2412176b677..d89039dee5b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -20,6 +20,9 @@ package io.entgra.device.mgt.core.application.mgt.core.impl; import com.google.gson.Gson; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethodBase; @@ -111,8 +114,8 @@ import java.util.stream.Collectors; * This is the default implementation for the Subscription Manager. */ public class SubscriptionManagerImpl implements SubscriptionManager { - - private static final Log log = LogFactory.getLog(SubscriptionManagerImpl.class); + AppInstallLogContext.Builder appInstallLogContextBuilder = new AppInstallLogContext.Builder(); + private static final EntgraLogger log = new EntgraAppInstallLoggerImpl(SubscriptionManagerImpl.class); private SubscriptionDAO subscriptionDAO; private ApplicationDAO applicationDAO; private LifecycleStateManager lifecycleStateManager; @@ -644,7 +647,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { Properties properties, boolean isOperationReExecutingDisabled) throws ApplicationManagementException { - + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); //Get app subscribing info of each device SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices, applicationDTO.getApplicationReleaseDTOs().get(0).getId()); @@ -694,10 +699,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { Activity activity = addAppOperationOnDevices(applicationDTO, new ArrayList<>(entry.getValue()), entry.getKey(), action, properties); activityList.add(activity); + for (DeviceIdentifier identifier : deviceIdentifiers) { + log.info(String.format("Web app %s triggered", action), appInstallLogContextBuilder.setAppId(String.valueOf(applicationDTO.getId())).setAppName(applicationDTO.getName()).setAppType(applicationDTO.getType()).setSubType(subType).setTenantId(tenantId).setTenantDomain(tenantDomain).setDevice(String.valueOf(identifier)).setUserName(username).setAction(action).build()); + } } } else { Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action, properties); activityList.add(activity); + for (DeviceIdentifier identifier : deviceIdentifiers) { + log.info(String.format("App %s triggered", action), appInstallLogContextBuilder.setAppId(String.valueOf(applicationDTO.getId())).setAppName(applicationDTO.getName()).setAppType(applicationDTO.getType()).setSubType(subType).setTenantId(tenantId).setTenantDomain(tenantDomain).setDevice(String.valueOf(identifier)).setUserName(username).setAction(action).build()); + } } ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); 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 7373e52e5f6..51b93d5fdda 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"; @@ -174,6 +177,8 @@ public class OperationManagerImpl implements OperationManager { } } + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); if (!validDeviceIds.isEmpty()) { @@ -250,6 +255,10 @@ public class OperationManagerImpl implements OperationManager { activity.setActivityStatus( this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult)); } + for (DeviceIdentifier deviceId : authorizedDeviceIds) { + device = getDevice(deviceId); + log.info("Operation added", deviceConnectivityLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(deviceType).setActionTag("ADD_OPERATION").setTenantDomain(tenantDomain).setTenantId(tenantId).setUserName(initiatedBy).setOperationCode(operationCode).build()); + } return activity; } catch (OperationManagementDAOException e) { OperationManagementDAOFactory.rollbackTransaction(); @@ -696,7 +705,9 @@ public class OperationManagerImpl implements OperationManager { if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() + "]"); } - + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + @@ -709,16 +720,18 @@ public class OperationManagerImpl implements OperationManager { case INACTIVE: case UNREACHABLE: this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceId, tenantId); break; } - + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(deviceId.getId()).setDeviceType(deviceId.getType()).setActionTag("PENDING_OPERATION").setTenantDomain(tenantDomain).setTenantId(String.valueOf(tenantId)).setUserName(userName).build()); return getOperations(deviceId, Operation.Status.PENDING, enrolmentId); } @Override public List getPendingOperations(Device device) throws OperationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + @@ -736,10 +749,10 @@ public class OperationManagerImpl implements OperationManager { this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); device.setEnrolmentInfo(enrolmentInfo); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, device, tenantId); break; } + log.info("Device Connected", deviceConnectivityLogContextBuilder.setDeviceId(device.getDeviceIdentifier()).setDeviceType(device.getType()).setActionTag("PENDING_OPERATION").setTenantDomain(tenantDomain).setTenantId(String.valueOf(tenantId)).setUserName(userName).build()); return getOperations(deviceIdentifier, Operation.Status.PENDING, enrolmentId); } 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index f9c5ec4f7c2..8dcaf1ebcab 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -20,10 +20,11 @@ package io.entgra.device.mgt.core.device.mgt.core.service; import com.google.common.reflect.TypeToken; 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.DeviceEnrolmentLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceEnrolmentLoggerImpl; import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; @@ -172,7 +173,9 @@ import java.util.stream.Collectors; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { - private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); + DeviceEnrolmentLogContext.Builder deviceEnrolmentLogContextBuilder = new DeviceEnrolmentLogContext.Builder(); + + private static final EntgraLogger log = new EntgraDeviceEnrolmentLoggerImpl(DeviceManagementProviderServiceImpl.class); private static final String OPERATION_RESPONSE_EVENT_STREAM_DEFINITION = "org.wso2.iot.OperationResponseStream"; private final DeviceManagementPluginRepository pluginRepository; @@ -260,6 +263,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return false; } + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry(); String deviceSerialNumber = null; if (enrollmentConfiguration != null) { @@ -346,6 +351,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() + "'"); } + log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(existingDevice.getId())).setDeviceType(String.valueOf(existingDevice.getType())).setOwner(newEnrolmentInfo.getOwner()).setOwnership(String.valueOf(newEnrolmentInfo.getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); status = true; } else { log.warn("Unable to update device enrollment for device : " + device.getDeviceIdentifier() + @@ -382,6 +388,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } device.setEnrolmentInfo(enrollment); DeviceManagementDAOFactory.commitTransaction(); + log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(enrollment.getOwner()).setOwnership(String.valueOf(enrollment.getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); } else { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("No device type registered with name - " + device.getType() @@ -570,7 +577,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } int tenantId = this.getTenantId(); - + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Device device = this.getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { @@ -604,7 +612,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } //procees to dis-enroll a device from traccar ends - + log.info("Device disenrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(device.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(device.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while dis-enrolling '" + deviceId.getType() + diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/AppInstallLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/AppInstallLogContext.java new file mode 100644 index 00000000000..33cffac3666 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/AppInstallLogContext.java @@ -0,0 +1,181 @@ +/* + * 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 AppInstallLogContext extends LogContext { + private final String appId; + private final String appName; + private final String appType; + private final String subType; + private final String tenantId; + private final String tenantDomain; + private final String device; + private final String userName; + private final String action; + + private AppInstallLogContext(Builder builder) { + this.appId = builder.appId; + this.appName = builder.appName; + this.appType = builder.appType; + this.subType = builder.subType; + this.tenantId = builder.tenantId; + this.tenantDomain = builder.tenantDomain; + this.device = builder.device; + this.userName = builder.userName; + this.action = builder.action; + } + + public String getAppId() { + return appId; + } + + public String getAppName() { + return appName; + } + + public String getAppType() { + return appType; + } + + public String getSubType() { + return subType; + } + + public String getTenantId() { + return tenantId; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public String getDevice() { + return device; + } + + public String getUserName() { + return userName; + } + + public String getAction() { + return action; + } + + public static class Builder { + private String appId; + private String appName; + private String appType; + private String subType; + private String tenantId; + private String tenantDomain; + private String device; + private String userName; + private String action; + + public Builder() { + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getAppId() { + return appId; + } + + public Builder setAppId(String appId) { + this.appId = appId; + return this; + } + + public String getAppName() { + return appName; + } + + public Builder setAppName(String appName) { + this.appName = appName; + return this; + } + + public String getAppType() { + return appType; + } + + public Builder setAppType(String appType) { + this.appType = appType; + return this; + } + + public String getSubType() { + return subType; + } + + public Builder setSubType(String subType) { + this.subType = subType; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public String getTenantId() { + return tenantId; + } + + public Builder setTenantId(String tenantId) { + this.tenantId = tenantId; + return this; + } + + public String getDevice() { + return device; + } + + public Builder setDevice(String device) { + this.device = device; + return this; + } + + public String getAction() { + return action; + } + + public Builder setAction(String action) { + this.action = action; + return this; + } + + public AppInstallLogContext build() { + return new AppInstallLogContext(this); + } + } +} 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 00000000000..d110e84a43b --- /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,150 @@ +/* + * 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 final String actionTag; + private final String operationCode; + private final String tenantId; + private final String tenantDomain; + private final String userName; + + private DeviceConnectivityLogContext(Builder builder) { + this.deviceId = builder.deviceId; + this.deviceType = builder.deviceType; + this.actionTag = builder.actionTag; + this.operationCode = builder.operationCode; + this.tenantId = builder.tenantId; + this.tenantDomain = builder.tenantDomain; + this.userName = builder.userName; + } + + public String getDeviceId() { + return deviceId; + } + + public String getDeviceType() { + return deviceType; + } + + public String getActionTag() { + return actionTag; + } + + public String getOperationCode() { + return operationCode; + } + + public String getTenantId() { + return tenantId; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public String getUserName() { + return userName; + } + + public static class Builder { + private String deviceId; + private String deviceType; + private String operationCode; + private String actionTag; + private String tenantId; + private String tenantDomain; + private String userName; + + 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 String getOperationCode() { + return operationCode; + } + + public Builder setOperationCode(String operationCode) { + this.operationCode = operationCode; + return this; + } + + public String getTenantId() { + return tenantId; + } + + public Builder setTenantId(String tenantId) { + this.tenantId = tenantId; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getActionTag() { + return actionTag; + } + + public Builder setActionTag(String actionTag) { + this.actionTag = actionTag; + 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/DeviceEnrolmentLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java new file mode 100644 index 00000000000..b355b296716 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/DeviceEnrolmentLogContext.java @@ -0,0 +1,149 @@ +/* + * 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 DeviceEnrolmentLogContext extends LogContext { + private final String deviceId; + private final String deviceType; + private final String owner; + private final String ownership; + private final String tenantID; + private final String tenantDomain; + private final String userName; + + private DeviceEnrolmentLogContext(Builder builder) { + this.deviceId = builder.deviceId; + this.deviceType = builder.deviceType; + this.owner = builder.owner; + this.ownership = builder.ownership; + this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + this.userName = builder.userName; + } + + public String getTenantID() { + return tenantID; + } + + public String getDeviceId() { + return deviceId; + } + + public String getOwner() { + return owner; + } + + public String getDeviceType() { + return deviceType; + } + + public String getOwnership() { + return ownership; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public String getUserName() { + return userName; + } + public static class Builder { + private String deviceId; + private String deviceType; + private String owner; + private String ownership; + private String tenantID; + private String tenantDomain; + private String userName; + + public Builder() { + } + + public String getDeviceType() { + return deviceType; + } + + public Builder setDeviceType(String deviceType) { + this.deviceType = deviceType; + return this; + } + + public String getTenantID() { + return tenantID; + } + + public Builder setTenantID(String tenantID) { + this.tenantID = tenantID; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getDeviceId() { + return deviceId; + } + + public Builder setDeviceId(String deviceId) { + this.deviceId = deviceId; + return this; + } + + public String getOwner() { + return owner; + } + + public Builder setOwner(String owner) { + this.owner = owner; + return this; + } + + public String getOwnership() { + return ownership; + } + + public Builder setOwnership(String ownership) { + this.ownership = ownership; + return this; + } + + public DeviceEnrolmentLogContext build() { + return new DeviceEnrolmentLogContext(this); + } + + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java new file mode 100644 index 00000000000..9dbd10a0592 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java @@ -0,0 +1,133 @@ +/* + * 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 PolicyLogContext extends LogContext { + private final String policyName; + private final String payload; + private final String actionTag; + private final String userName; + private final String tenantID; + private final String tenantDomain; + + private PolicyLogContext(Builder builder) { + this.policyName = builder.policyName; + this.payload = builder.payload; + this.actionTag = builder.actionTag; + this.userName = builder.userName; + this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + } + + public String getPolicyName() { + return policyName; + } + + public String getPayload() { + return payload; + } + + public String getActionTag() { + return actionTag; + } + + public String getUserName() { + return userName; + } + + public String getTenantID() { + return tenantID; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public static class Builder { + private String policyName; + private String payload; + private String actionTag; + private String userName; + private String tenantID; + private String tenantDomain; + + public Builder() { + } + + public String getPolicyName() { + return policyName; + } + + public Builder setPolicyName(String policyName) { + this.policyName = policyName; + return this; + } + + public String getPayload() { + return payload; + } + + public Builder setPayload(String payload) { + this.payload = payload; + return this; + } + + public String getActionTag() { + return actionTag; + } + + public Builder setActionTag(String actionTag) { + this.actionTag = actionTag; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getTenantID() { + return tenantID; + } + + public Builder setTenantID(String tenantID) { + this.tenantID = tenantID; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public PolicyLogContext build() { + return new PolicyLogContext(this); + } + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraAppInstallLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraAppInstallLoggerImpl.java new file mode 100644 index 00000000000..81f071c2afe --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraAppInstallLoggerImpl.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.AppInstallLogContext; +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 EntgraAppInstallLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraAppInstallLoggerImpl(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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + AppInstallLogContext appInstallLogContext = (AppInstallLogContext) logContext; + MDCContextUtil.populateAppInstallMDCContext(appInstallLogContext); + 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/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 00000000000..aaa62bf4887 --- /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/impl/EntgraDeviceEnrolmentLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceEnrolmentLoggerImpl.java new file mode 100644 index 00000000000..db0b7dd2653 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceEnrolmentLoggerImpl.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.DeviceEnrolmentLogContext; +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 EntgraDeviceEnrolmentLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraDeviceEnrolmentLoggerImpl(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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + DeviceEnrolmentLogContext deviceEnrolmentLogContext = (DeviceEnrolmentLogContext) logContext; + MDCContextUtil.populateDeviceEnrolmentMDCContext(deviceEnrolmentLogContext); + 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/impl/EntgraDeviceLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceLoggerImpl.java index ae10ae87c2f..a51987463f1 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceLoggerImpl.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraDeviceLoggerImpl.java @@ -17,7 +17,6 @@ */ 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.DeviceLogContext; @@ -26,7 +25,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.MDC; - public class EntgraDeviceLoggerImpl implements EntgraLogger { private static Log log = null; @@ -74,7 +72,6 @@ public class EntgraDeviceLoggerImpl implements EntgraLogger { log.info(object, t); } - public void debug(String message) { log.debug(message); } @@ -114,7 +111,6 @@ public class EntgraDeviceLoggerImpl implements EntgraLogger { log.debug(object, t); } - public void error(String message) { log.error(message); } @@ -161,7 +157,6 @@ public class EntgraDeviceLoggerImpl implements EntgraLogger { log.error(object, t); } - public void warn(String message) { log.warn(message); } @@ -208,7 +203,6 @@ public class EntgraDeviceLoggerImpl implements EntgraLogger { log.warn(object, t); } - public void trace(String message) { log.trace(message); } @@ -248,7 +242,6 @@ public class EntgraDeviceLoggerImpl implements EntgraLogger { log.trace(object, t); } - public void fatal(String message) { log.fatal(message); } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraPolicyLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraPolicyLoggerImpl.java new file mode 100644 index 00000000000..218252f260a --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraPolicyLoggerImpl.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.PolicyLogContext; +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 EntgraPolicyLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraPolicyLoggerImpl(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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + PolicyLogContext policyLogContext = (PolicyLogContext) logContext; + MDCContextUtil.populatePolicyMDCContext(policyLogContext); + 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/impl/EntgraUserLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java index 6a082b013d4..fa482605f6e 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java @@ -17,7 +17,6 @@ */ 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.UserLogContext; @@ -26,7 +25,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.MDC; - public class EntgraUserLoggerImpl implements EntgraLogger { private static Log log = null; 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 09f903188a0..dc046e8eeb8 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,8 +17,7 @@ */ package io.entgra.device.mgt.core.notification.logger.util; -import io.entgra.device.mgt.core.notification.logger.DeviceLogContext; -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 { @@ -62,6 +61,105 @@ public final class MDCContextUtil { } } + + public static void populatePolicyMDCContext(final PolicyLogContext mdcContext) { + if (mdcContext.getPolicyName() != null) { + MDC.put("PolicyName", mdcContext.getPolicyName()); + } + if (mdcContext.getPayload() != null) { + MDC.put("Payload", mdcContext.getPayload()); + } + if (mdcContext.getActionTag() != null) { + MDC.put("ActionTag", mdcContext.getActionTag()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getTenantID() != null) { + MDC.put("TenantId", mdcContext.getTenantID()); + } + } + + public static void populateAppInstallMDCContext(final AppInstallLogContext mdcContext) { + if (mdcContext.getAppId() != null) { + MDC.put("AppId", mdcContext.getAppId()); + } + if (mdcContext.getAppName() != null) { + MDC.put("AppName", mdcContext.getAppName()); + } + if (mdcContext.getAppType() != null) { + MDC.put("AppType", mdcContext.getAppType()); + } + if (mdcContext.getSubType() != null) { + MDC.put("SubType", mdcContext.getSubType()); + } + if (mdcContext.getDevice() != null) { + MDC.put("Device", mdcContext.getDevice()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getTenantId() != null) { + MDC.put("TenantId", mdcContext.getTenantId()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + if (mdcContext.getAction() != null) { + 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()); + } + if (mdcContext.getOperationCode() != null) { + MDC.put("OperationCode", mdcContext.getOperationCode()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getTenantId() != null) { + MDC.put("TenantId", mdcContext.getTenantId()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + if (mdcContext.getActionTag() != null) { + MDC.put("ActionTag", mdcContext.getActionTag()); + } + } + + public static void populateDeviceEnrolmentMDCContext(final DeviceEnrolmentLogContext mdcContext) { + if (mdcContext.getDeviceId() != null) { + MDC.put("DeviceId", mdcContext.getDeviceId()); + } + if (mdcContext.getDeviceType() != null) { + MDC.put("DeviceType", mdcContext.getDeviceType()); + } + if (mdcContext.getOwner() != null) { + MDC.put("Owner", mdcContext.getOwner()); + } + if (mdcContext.getOwnership() != null) { + MDC.put("Ownership", mdcContext.getOwnership()); + } + if (mdcContext.getTenantID() != null) { + MDC.put("TenantId", mdcContext.getTenantID()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + } } diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 2521eab3a11..7ead0213c9f 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.policy.mgt.core.impl; import io.entgra.device.mgt.core.device.mgt.common.PolicyPaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.PolicyLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraPolicyLoggerImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -50,7 +53,8 @@ import java.util.Set; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { - private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class); + PolicyLogContext.Builder policyLogContextBuilder = new PolicyLogContext.Builder(); + private static final EntgraLogger log = new EntgraPolicyLoggerImpl(PolicyAdministratorPointImpl.class); private PolicyManager policyManager; private ProfileManager profileManager; @@ -131,6 +135,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); if (log.isDebugEnabled()) { @@ -164,11 +170,13 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.scheduleTask(taskInfo.getName()); + log.info("Apply changes to device", policyLogContextBuilder.setActionTag("PUBLISH_CHANGES").setUserName(userName).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).build()); } else { if (!taskManager.isTaskScheduled(taskName)) { TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); taskManager.scheduleTask(taskInfo.getName()); + log.info("Apply changes to device", policyLogContextBuilder.setActionTag("PUBLISH_CHANGES").setUserName(userName).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).build()); } else { throw new PolicyManagementException("There is a task already running for policy changes. Please try " + "to apply " + diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index fd5b3b5fdb2..9d362bcc9ef 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -18,10 +18,12 @@ package io.entgra.device.mgt.core.policy.mgt.core.mgt.impl; +import com.google.gson.Gson; import io.entgra.device.mgt.core.device.mgt.common.PolicyPaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.PolicyLogContext; +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 io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.DynamicTaskContext; @@ -61,6 +63,7 @@ import io.entgra.device.mgt.core.policy.mgt.core.mgt.ProfileManager; import io.entgra.device.mgt.core.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; import io.entgra.device.mgt.core.policy.mgt.core.util.PolicyManagementConstants; import io.entgra.device.mgt.core.policy.mgt.core.util.PolicyManagerUtil; +import org.wso2.carbon.context.PrivilegedCarbonContext; import java.sql.SQLException; import java.sql.Timestamp; @@ -73,12 +76,13 @@ import java.util.Map; public class PolicyManagerImpl implements PolicyManager { + PolicyLogContext.Builder policyLogContextBuilder = new PolicyLogContext.Builder(); private final PolicyDAO policyDAO; private final ProfileDAO profileDAO; private final FeatureDAO featureDAO; private final ProfileManager profileManager; private final PolicyConfiguration policyConfiguration; - private static final Log log = LogFactory.getLog(PolicyManagerImpl.class); + private static final EntgraLogger log = new EntgraPolicyLoggerImpl(PolicyManagerImpl.class); public PolicyManagerImpl() { this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); @@ -90,6 +94,9 @@ public class PolicyManagerImpl implements PolicyManager { @Override public Policy addPolicy(Policy policy) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getProfile() != null && policy.getProfile().getProfileId() == 0) { @@ -171,11 +178,16 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } + String stringPayload = new Gson().toJson(policy); + log.info("Policy created", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("ADD_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return policy; } @Override public Policy updatePolicy(Policy policy) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); try { // Previous policy needs to be obtained before beginning the transaction Policy previousPolicy = this.getPolicy(policy.getId()); @@ -317,6 +329,8 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } + String stringPayload = new Gson().toJson(policy); + log.info("Policy updated", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("UPDATE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return policy; } @@ -506,6 +520,10 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); + String stringPayload = new Gson().toJson(policies); boolean bool; try { List existingPolicies; @@ -534,18 +552,24 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } + log.info("Policy priorities updated", policyLogContextBuilder.setPayload(stringPayload).setActionTag("UPDATE_POLICY_PRIORITIES").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return bool; } @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { try { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); PolicyManagementDAOFactory.beginTransaction(); policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); policyDAO.deletePolicy(policy.getId()); featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); + String stringPayload = new Gson().toJson(policy); + log.info("Policy deleted", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("DELETE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return true; } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -566,6 +590,9 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); boolean bool; List policies = this.getPolicies(); Policy pol = null; @@ -596,6 +623,8 @@ public class PolicyManagerImpl implements PolicyManager { featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); + String stringPayload = new Gson().toJson(policy); + log.info("Policy deleted", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("DELETE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return bool; } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -654,6 +683,9 @@ public class PolicyManagerImpl implements PolicyManager { List deviceList = new ArrayList<>(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder .getInstance().getDeviceManagementService(); + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { try { Device device = deviceManagementService.getDevice(deviceIdentifier, false); @@ -682,6 +714,9 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setDevices(deviceList); } + String policyPayload = new Gson().toJson(policy); + String deviceListPayload = new Gson().toJson(deviceList); + log.info("Policy added to devices", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(" Policy: " + policyPayload + " DeviceList: " + deviceListPayload).setActionTag("ADD_POLICY_TO_DEVICE").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while adding the policy ("