Add PolicyLogContext

pull/136/head
prathabanKavin 1 year ago
parent cebc3bf5d4
commit 1b3a8d4245

@ -0,0 +1,102 @@
/*
* 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 payloadVersion;
private final String tenantID;
private final String profileID;
private PolicyLogContext(Builder builder) {
this.policyName = builder.policyName;
this.payloadVersion = builder.payloadVersion;
this.tenantID = builder.tenantID;
this.profileID = builder.profileID;
}
public String getPolicyName() {
return policyName;
}
public String getPayloadVersion() {
return payloadVersion;
}
public String getTenantID() {
return tenantID;
}
public String getProfileID() {
return profileID;
}
public static class Builder {
private String policyName;
private String payloadVersion;
private String tenantID;
private String profileID;
public Builder() {
}
public String getTenantID() {
return tenantID;
}
public Builder setTenantID(String tenantID) {
this.tenantID = tenantID;
return this;
}
public String getProfileID() {
return profileID;
}
public Builder setProfileID(String profileID) {
this.profileID = profileID;
return this;
}
public String getPolicyName() {
return policyName;
}
public Builder setPolicyName(String policyName) {
this.policyName = policyName;
return this;
}
public String getPayloadVersion() {
return payloadVersion;
}
public Builder setPayloadVersion(String payloadVersion) {
this.payloadVersion = payloadVersion;
return this;
}
public Throwable build() {
return new Throwable(this.build());
}
}
}

@ -0,0 +1,319 @@
/*
* 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();
}
}

@ -18,6 +18,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.PolicyLogContext;
import io.entgra.device.mgt.core.notification.logger.UserLogContext;
import org.apache.log4j.MDC;
@ -62,6 +63,22 @@ public final class MDCContextUtil {
}
}
public static void populatePolicyMDCContext(final PolicyLogContext mdcContext) {
if (mdcContext.getPolicyName() != null) {
MDC.put("PolicyName", mdcContext.getPolicyName());
System.out.println(mdcContext.getPolicyName());
}
if (mdcContext.getPayloadVersion() != null) {
MDC.put("PayloadVersion", mdcContext.getPayloadVersion());
}
if (mdcContext.getProfileID() != null) {
MDC.put("ProfileId", mdcContext.getProfileID());
}
if (mdcContext.getTenantID() != null) {
MDC.put("TenantId", mdcContext.getTenantID());
}
}
}

@ -19,6 +19,10 @@
package io.entgra.device.mgt.core.policy.mgt.core.mgt.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 io.entgra.device.mgt.core.notification.logger.impl.EntgraUserLoggerImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -61,6 +65,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 +78,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();
@ -176,6 +182,7 @@ public class PolicyManagerImpl implements PolicyManager {
@Override
public Policy updatePolicy(Policy policy) throws PolicyManagementException {
String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
try {
// Previous policy needs to be obtained before beginning the transaction
Policy previousPolicy = this.getPolicy(policy.getId());
@ -317,6 +324,7 @@ public class PolicyManagerImpl implements PolicyManager {
} finally {
PolicyManagementDAOFactory.closeConnection();
}
log.info("Policy Update", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayloadVersion(policy.getPolicyPayloadVersion()).setTenantID(tenantId).setProfileID(String.valueOf(policy.getProfileId())).build());
return policy;
}

Loading…
Cancel
Save