User log context added

issue-9736
Pramila Niroshan 2 years ago
parent b99587f1df
commit 4005a9e8cb

@ -113,7 +113,9 @@
org.wso2.carbon.event.processor.stub,
org.wso2.carbon.identity.jwt.client.extension.service,
org.apache.commons.codec.binary,
io.entgra.server.bootup.heartbeat.beacon
io.entgra.server.bootup.heartbeat.beacon,
io.entgra.device.mgt.extensions.logger.*,
io.entgra.notification.logger.*
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.mgt.core.internal,
@ -352,6 +354,10 @@
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>io.entgra.task.mgt.common</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>io.entgra.notification.logger</artifactId>
</dependency>
</dependencies>
</project>

@ -25,11 +25,13 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>logger</artifactId>
<version>5.0.17-SNAPSHOT</version>
<version>5.0.21-SNAPSHOT</version>
</parent>
<artifactId>io.entgra.notification.logger</artifactId>
<name>Entgra - Notification Logger</name>
<packaging>bundle</packaging>
<url>http://entgra.io</url>
<dependencies>
<dependency>
@ -69,8 +71,7 @@
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Device Notification Logger Bundle</Bundle-Description>
<Import-Package>
io.entgra.logger;version="[2.0,3)",
io.entgra.logger.spi;version="[2.0,3)",
io.entgra.device.mgt.extensions.logger.*
org.apache.commons.logging;version="[1.2,2)",
org.apache.log4j;version="[1.2,2)",
org.wso2.carbon.context;version="[4.4,5)

@ -0,0 +1,103 @@
/*
* Copyright (c) 2023, Entgra (pvt) Ltd. (http://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.notification.logger;
import io.entgra.device.mgt.extensions.logger.LogContext;
public class UserLogContext extends LogContext {
private final String userName;
private final String userEmail;
private final String metaInfo;
private final String tenantID;
private UserLogContext(Builder builder) {
this.userEmail = builder.userEmail;
this.userName = builder.userName;
this.metaInfo = builder.metaInfo;
this.tenantID = builder.tenantID;
}
public String getTenantID() {
return tenantID;
}
public String getUserName() {
return userName;
}
public String getUserEmail() {
return userEmail;
}
public String getMetaInfo() {
return metaInfo;
}
public static class Builder {
private String userName;
private String userEmail;
private String metaInfo;
private String tenantID;
public Builder() {
}
public String getMetaInfo() {
return metaInfo;
}
public Builder setMetaInfo(String metaInfo) {
this.metaInfo = metaInfo;
return this;
}
public String getTenantID() {
return tenantID;
}
public Builder setTenantID(String tenantID) {
this.tenantID = tenantID;
return this;
}
public String getUserName() {
return userName;
}
public Builder setUserName(String userName) {
this.userName = userName;
return this;
}
public String getUserEmail() {
return userEmail;
}
public Builder setUserEmail(String userEmail) {
this.userEmail = userEmail;
return this;
}
public UserLogContext build() {
return new UserLogContext(this);
}
}
}

@ -0,0 +1,294 @@
/*
* Copyright (c) 2023, Entgra (pvt) Ltd. (http://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.notification.logger.impl;
import io.entgra.device.mgt.extensions.logger.LogContext;
import io.entgra.device.mgt.extensions.logger.spi.EntgraLogger;
import io.entgra.notification.logger.UserLogContext;
import io.entgra.notification.logger.util.MDCContextUtil;
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;
public EntgraUserLoggerImpl(Class<?> clazz) {
log = LogFactory.getLog(clazz);
}
@Override
public void info(Object object, LogContext logContext) {
}
@Override
public void info(Object object, Throwable t, LogContext logContext) {
}
@Override
public void debug(Object object, LogContext logContext) {
}
@Override
public void debug(Object object, Throwable t, LogContext logContext) {
}
@Override
public void error(Object object, LogContext logContext) {
}
@Override
public void error(Object object, Throwable t, LogContext logContext) {
}
@Override
public void fatal(Object object, LogContext logContext) {
}
@Override
public void fatal(Object object, Throwable t, LogContext logContext) {
}
@Override
public void trace(Object object, LogContext logContext) {
}
@Override
public void trace(Object object, Throwable t, LogContext logContext) {
}
@Override
public void warn(Object object, LogContext logContext) {
}
@Override
public void warn(Object object, Throwable t, LogContext logContext) {
}
public void info(String message) {
}
public void info(String message, Throwable t) {
log.info(message, t);
}
@Override
public void info(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.info(message);
}
public void debug(String message) {
log.debug(message);
}
public void debug(String message, Throwable t) {
log.debug(message, t);
}
@Override
public void debug(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.debug(message);
}
public void error(String message) {
log.error(message);
}
public void error(String message, Throwable t) {
log.error(message, t);
}
@Override
public void error(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.error(message);
}
@Override
public void error(String message, Throwable t, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.error(message, t);
}
public void warn(String message) {
log.warn(message);
}
public void warn(String message, Throwable t) {
log.warn(message, t);
}
@Override
public void warn(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.warn(message);
}
@Override
public void warn(String message, Throwable t, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.warn(message, t);
}
public void trace(String message) {
log.trace(message);
}
public void trace(String message, Throwable t) {
log.trace(message, t);
}
@Override
public void trace(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.trace(message);
}
public void fatal(String message) {
log.fatal(message);
}
public void fatal(String message, Throwable t) {
log.fatal(message, t);
}
@Override
public void fatal(String message, LogContext logContext) {
UserLogContext userLogContext = (UserLogContext) logContext;
MDCContextUtil.populateUserMDCContext(userLogContext);
log.fatal(message);
}
@Override
public void debug(Object o) {
log.debug(o);
}
@Override
public void debug(Object o, Throwable throwable) {
log.debug(o, throwable);
}
@Override
public void error(Object o) {
log.error(o);
}
@Override
public void error(Object o, Throwable throwable) {
log.error(o, throwable);
}
@Override
public void fatal(Object o) {
log.fatal(0);
}
@Override
public void fatal(Object o, Throwable throwable) {
log.fatal(0, throwable);
}
@Override
public void info(Object o) {
log.info(o);
}
@Override
public void info(Object o, Throwable throwable) {
log.info(o, throwable);
}
@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 trace(Object o) {
log.trace(o);
}
@Override
public void trace(Object o, Throwable throwable) {
log.trace(o, throwable);
}
@Override
public void warn(Object o) {
log.warn(o);
}
@Override
public void warn(Object o, Throwable throwable) {
log.warn(o, throwable);
}
@Override
public void clearLogContext() {
MDC.clear();
}
}

@ -18,6 +18,7 @@
package io.entgra.notification.logger.util;
import io.entgra.notification.logger.DeviceLogContext;
import io.entgra.notification.logger.UserLogContext;
import org.apache.log4j.MDC;
public final class MDCContextUtil {
@ -36,6 +37,21 @@ public final class MDCContextUtil {
MDC.put("TenantId", mdcContext.getTenantID());
}
}
public static void populateUserMDCContext(final UserLogContext mdcContext) {
if (mdcContext.getUserName() != null) {
MDC.put("UserName", mdcContext.getUserName());
}
if (mdcContext.getUserEmail() != null) {
MDC.put("UserEmail", mdcContext.getUserEmail());
}
if (mdcContext.getMetaInfo() != null) {
MDC.put("MetaInfo", mdcContext.getMetaInfo());
}
if (mdcContext.getTenantID() != null) {
MDC.put("TenantId", mdcContext.getTenantID());
}
}
}

@ -1,57 +0,0 @@
/*
* Copyright (c) 2023, Entgra (pvt) Ltd. (http://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.logger.impl;
import io.entgra.device.mgt.extensions.logger.spi.EntgraLogger;
import io.entgra.notification.logger.DeviceLogContext;
import io.entgra.notification.logger.impl.EntgraLoggerImpl;
import org.testng.annotations.Test;
public class EntgraLoggerImplTest {
private final EntgraLogger log = new EntgraLoggerImpl(EntgraLoggerImplTest.class);
@Test
public void logTest() {
DeviceLogContext.Builder deviceLogContext = new DeviceLogContext.Builder();
deviceLogContext.setDeviceName("M02S");
deviceLogContext.setDeviceType("Android");
deviceLogContext.setOperationCode("1222");
deviceLogContext.setTenantID("1234");
log.debug("Test debug message", deviceLogContext.build());
log.info("Test info message", deviceLogContext.build());
log.error("Test error message", deviceLogContext.build());
log.warn("Test warn message", deviceLogContext.build());
log.trace("Test trace message", deviceLogContext.build());
log.fatal("Test fatal message", deviceLogContext.build());
log.error("debug message test", new Throwable("error throw"));
log.info("info message test");
// log.isDebugEnabled();
// log.isErrorEnabled();
// log.isFatalEnabled();
// log.isInfoEnabled();
// log.isTraceEnabled();
// log.isWarnEnabled();
}
}

@ -24,13 +24,13 @@
<parent>
<artifactId>carbon-devicemgt</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId>
<version>5.0.17-SNAPSHOT</version>
<version>5.0.21-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>logger</artifactId>
<packaging>pom</packaging>
<name>Entgra IoT - Notification logger</name>
<name>Entgra - Notification Logger Component</name>
<url>http://entgra.io</url>
<modules>

@ -23,14 +23,14 @@
<parent>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>logger-feature</artifactId>
<version>5.0.17-SNAPSHOT</version>
<version>5.0.21-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>io.entgra.notification.logger.feature</artifactId>
<packaging>pom</packaging>
<name>Entgra - Device Notification logger</name>
<name>Entgra - Notification logger</name>
<url>http://entgra.io</url>
<description>
This feature bundles for the Entgra Notification logger

@ -46,6 +46,7 @@
<module>components/transport-mgt</module>
<module>components/analytics-mgt</module>
<module>components/webapp-authenticator-framework</module>
<module>components/logger</module>
<module>components/task-mgt</module>
<module>features/device-mgt</module>
<module>features/apimgt-extensions</module>
@ -59,6 +60,7 @@
<module>features/transport-mgt</module>
<module>features/analytics-mgt</module>
<module>features/webapp-authenticator-framework</module>
<module>features/logger</module>
<module>features/task-mgt</module>
</modules>
@ -356,6 +358,11 @@
<artifactId>org.wso2.carbon.apimgt.keymgt.extension</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>io.entgra.notification.logger</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<!-- Device Management dependencies -->
<!-- Task Management dependencies -->

Loading…
Cancel
Save