Add logs for device enrolment history

pull/136/head
prathabanKavin 2 years ago
parent 663dcd7ee7
commit 10a17ae745

@ -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;
@ -346,6 +349,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)).build());
status = true;
} else {
log.warn("Unable to update device enrollment for device : " + device.getDeviceIdentifier() +
@ -382,6 +386,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)).build());
} else {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("No device type registered with name - " + device.getType()
@ -604,7 +609,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)).build());
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while dis-enrolling '" + deviceId.getType() +

@ -0,0 +1,118 @@
/*
* 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 DeviceEnrolmentLogContext(Builder builder) {
this.deviceId = builder.deviceId;
this.deviceType = builder.deviceType;
this.owner = builder.owner;
this.ownership = builder.ownership;
this.tenantID = builder.tenantID;
}
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 static class Builder {
private String deviceId;
private String deviceType;
private String owner;
private String ownership;
private String tenantID;
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 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);
}
}
}

@ -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();
}
}

@ -109,6 +109,24 @@ public final class MDCContextUtil {
MDC.put("DeviceType", mdcContext.getDeviceType());
}
}
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());
}
}
}

Loading…
Cancel
Save