Skip to content

Commit

Permalink
refs : #18514 : Optimized logEntry class and added LogPriority enum
Browse files Browse the repository at this point in the history
  • Loading branch information
remiges-surajg committed May 31, 2024
1 parent abb557f commit 24f76da
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.remiges.logharbour.model.LogPriorityLevels;
import com.remiges.logharbour.model.LoggerContext;
import com.remiges.logharbour.model.LogEntry.LogPriority;

@Configuration
public class LoggerContextConfig {

@Bean
public LoggerContext loggerContext() {
return new LoggerContext(LogPriorityLevels.INFO);
return new LoggerContext(LogPriority.INFO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.remiges.logharbour.exception.LogException;
import com.remiges.logharbour.model.ChangeDetails;
import com.remiges.logharbour.model.ChangeInfo;
import com.remiges.logharbour.model.GetLogsResponse;
import com.remiges.logharbour.model.LogEntry;
import com.remiges.logharbour.model.LogEntry.LogPriority;
import com.remiges.logharbour.model.LogEntry.Status;
import com.remiges.logharbour.model.LogPriorityLevels;
import com.remiges.logharbour.model.LoggerContext;
import com.remiges.logharbour.model.LogharbourRequestBo;
import com.remiges.logharbour.model.LoginDetails;
import com.remiges.logharbour.model.LoginUser;
import com.remiges.logharbour.service.LHLoggerTestService;
import com.remiges.logharbour.util.LHLogger;
Expand Down Expand Up @@ -161,10 +157,10 @@ public String postActivityLogs() throws Exception {
Logharbour logharbour = new LHLoggerTestService(kafkaTemplate);

LHLogger lhLogger = new LHLogger(logharbour.getKafkaConnection(), logharbour.getFileWriter("logharbour.txt"),
logharbour.getLoggerContext(LogPriorityLevels.INFO), logharbour.getKafkaTopic(),
logharbour.getLoggerContext(LogPriority.INFO), logharbour.getKafkaTopic(),
new ObjectMapper());

lhLogger.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.DEBUG1, "User1",
lhLogger.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.WARN, "User1",
"Insert", LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS, "", "127.1.2.1");

lhLogger.logActivity("Log Activitiy Test", loginUser);
Expand All @@ -189,7 +185,7 @@ public String postChangeLogs() throws Exception {
Logharbour logharbour = new LHLoggerTestService(kafkaTemplate);

LHLogger lhLogger = new LHLogger(logharbour.getKafkaConnection(), logharbour.getFileWriter("logharbour.txt"),
logharbour.getLoggerContext(LogPriorityLevels.INFO), logharbour.getKafkaTopic(),
logharbour.getLoggerContext(LogPriority.INFO), logharbour.getKafkaTopic(),
new ObjectMapper());

lhLogger.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.DEBUG0, "User2",
Expand All @@ -208,10 +204,10 @@ public String postDebugLogs() throws Exception {
Logharbour logharbour = new LHLoggerTestService(kafkaTemplate);

LHLogger lhLogger = new LHLogger(logharbour.getKafkaConnection(), logharbour.getFileWriter("logharbour.txt"),
logharbour.getLoggerContext(LogPriorityLevels.INFO), logharbour.getKafkaTopic(),
logharbour.getLoggerContext(LogPriority.INFO), logharbour.getKafkaTopic(),
new ObjectMapper());

lhLogger.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.DEBUG2, "Kra User",
lhLogger.setLogDetails("Kra", "Linux System", "Adhaar Kyc Module", LogPriority.DEBUG1, "Kra User",
"Insert", LHLogger.class.getName().toString(), "Instance Id", Status.SUCCESS, "", "187.0.2.1");

lhLogger.logDebug("Log Activitiy Test", loginUser);
Expand All @@ -221,7 +217,7 @@ public String postDebugLogs() throws Exception {
@PostMapping("/clone-log")
public String activityLogs() throws Exception {

LoggerContext context = new LoggerContext(LogPriorityLevels.INFO);
LoggerContext context = new LoggerContext(LogPriority.INFO);
context.setDebugMode(true);
PrintWriter printWriter = new PrintWriter("logharbour.txt");
LHLogger lhLogger = new LHLogger(kafkaTemplate, printWriter, context, "logharbour", new ObjectMapper());
Expand All @@ -247,23 +243,4 @@ public String activityLogs() throws Exception {

}

/**
* POC of Endpoint to test logging functionality. Creates a log entry with
* predefined details and logs it using the LHLogger.
*
* @return A string message indicating the result of the logging operation.
* @throws JsonProcessingException If there is an error processing JSON.
* @throws LogException
*/
@GetMapping("/check")
public String postLog() throws JsonProcessingException, LogException {
LoginDetails loginDetails = new LoginDetails();
loginDetails.setId("20");
loginDetails.setName("Deepak Kumar");

LogEntry logEntry = new LogEntry();

logHarbour.logActivity("suraj", logEntry);
return "able to log file";
}
}
83 changes: 73 additions & 10 deletions logharbour/src/main/java/com/remiges/logharbour/model/LogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import lombok.NoArgsConstructor;
import lombok.ToString;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand Down Expand Up @@ -58,16 +61,16 @@ public LogEntry(String app, String system, String module, LogPriority pri, Strin
this.data = data;
}

public enum LogPriority {
DEBUG2,
DEBUG1,
DEBUG0,
INFO,
WARN,
ERR,
CRIT,
SEC
}
// public enum LogPriority {
// DEBUG2,
// DEBUG1,
// DEBUG0,
// INFO,
// WARN,
// ERR,
// CRIT,
// SEC
// }

public enum LogType {
CHANGE,
Expand All @@ -81,4 +84,64 @@ public enum Status {
SUCCESS
}

/**
* Enum representing different levels of log priorities.
*/
public enum LogPriority {
DEBUG2(1, "Debug2"),
DEBUG1(2, "Debug1"),
DEBUG0(3, "Debug0"),
INFO(4, "Info"),
WARN(5, "Warn"),
ERR(6, "Err"),
CRIT(7, "Crit"),
SEC(8, "Sec"),
UNKNOWN(0, "Unknown");

private final int level;
private final String name;

/**
* Constructor for LogPriorityLevels.
*
* @param level the numerical level of the log priority
* @param name the name of the log priority
*/
LogPriority(int level, String name) {
this.level = level;
this.name = name;
}

@JsonValue
public String getName() {
return name;
}

/**
* Creates a LogPriorityLevels instance from the provided name.
*
* @param name the name of the log priority level
* @return the corresponding LogPriorityLevels instance, or UNKNOWN if no match is found
*/
@JsonCreator
public static LogPriority fromName(String name) {
for (LogPriority priority : LogPriority.values()) {
if (priority.name.equalsIgnoreCase(name)) {
return priority;
}
}
return UNKNOWN;
}

public int getLevel() {
return level;
}

@Override
public String toString() {
return name;
}
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

@Data
public class LoggerContext {
private LogPriorityLevels minLogPriority;
private LogEntry.LogPriority minLogPriority;
private final AtomicBoolean debugMode;

/**
* Constructs a new LoggerContext with the specified minimum log priority.
*
* @param minLogPriority the minimum log priority level for this context
*/
public LoggerContext(LogPriorityLevels minLogPriority) {
public LoggerContext(LogEntry.LogPriority minLogPriority) {
this.minLogPriority = minLogPriority;
this.debugMode = new AtomicBoolean(false);
}

public synchronized LogPriorityLevels getMinLogPriority() {
public synchronized LogEntry.LogPriority getMinLogPriority() {
return minLogPriority;
}

public synchronized void setMinLogPriority(LogPriorityLevels minLogPriority) {
public synchronized void setMinLogPriority(LogEntry.LogPriority minLogPriority) {
this.minLogPriority = minLogPriority;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import com.remiges.logharbour.model.LogPriorityLevels;
import com.remiges.logharbour.model.LogEntry.LogPriority;
import com.remiges.logharbour.model.LoggerContext;
import com.remiges.logharbour.util.Logharbour;

Expand Down Expand Up @@ -40,8 +40,8 @@ public PrintWriter getFileWriter(String fileName) throws IOException {
}

@Override
public LoggerContext getLoggerContext(LogPriorityLevels minLogPriority) {
LoggerContext loggerContext = new LoggerContext(LogPriorityLevels.INFO);
public LoggerContext getLoggerContext(LogPriority minLogPriority) {
LoggerContext loggerContext = new LoggerContext(LogPriority.INFO);
loggerContext.setDebugMode(true);
return loggerContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.springframework.kafka.core.KafkaTemplate;

import com.remiges.logharbour.model.LogPriorityLevels;
import com.remiges.logharbour.model.LogEntry.LogPriority;
import com.remiges.logharbour.model.LoggerContext;

/**
Expand Down Expand Up @@ -38,6 +38,6 @@ public interface Logharbour {
* @param minLogPriority The minimum priority level for logging.
* @return LogHarbourContext configured with the specified minimum log priority.
*/
public LoggerContext getLoggerContext(LogPriorityLevels minLogPriority);
public LoggerContext getLoggerContext(LogPriority minLogPriority);

}

0 comments on commit 24f76da

Please sign in to comment.