Optimized and cleaned up the logging service

This commit is contained in:
Rohit Awate 2018-05-12 12:19:15 +05:30
parent 79861e0d7e
commit c2f12d146e
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
5 changed files with 36 additions and 124 deletions

View file

@ -17,40 +17,8 @@
package com.rohitawate.everest.util.logging;
class Log {
private Level level;
private String message;
private String time;
private Exception exception;
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
Level level;
String message;
String time;
Exception exception;
}

View file

@ -39,15 +39,11 @@ class Logger {
* @param log - The log to be written to file.
*/
synchronized void log(Log log) {
if (log.getLevel().greaterThanEqualTo(this.writerLevel)) {
try {
String logFileContents = readFile(logFilePath);
String logEntry = generateLogEntry(log);
logFileContents = logFileContents.replace("<!-- Placeholder for new log -->", logEntry);
BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath));
System.out.println(log.message);
if (log.level.greaterThanEqualTo(this.writerLevel)) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) {
writer.flush();
writer.write(logFileContents);
writer.close();
writer.append(getLogEntry(log));
} catch (IOException e) {
e.printStackTrace();
}
@ -63,19 +59,19 @@ class Logger {
* Yellow = Warning
* Green = Info
*/
private String generateLogEntry(Log log) {
private String getLogEntry(Log log) {
String logEntry = this.logEntryTemplate;
logEntry = logEntry.replace("%% LogLevel %%", log.getLevel().toString());
logEntry = logEntry.replace("%% Time %%", log.getTime());
logEntry = logEntry.replace("%% Message %%", log.getMessage());
logEntry = logEntry.replace("%% LogLevel %%", log.level.toString());
logEntry = logEntry.replace("%% Time %%", log.time);
logEntry = logEntry.replace("%% Message %%", log.message);
StringBuilder builder = new StringBuilder();
if (log.getException() != null) {
StackTraceElement[] stackTrace = log.getException().getStackTrace();
builder.append(log.getException().toString());
if (log.exception != null) {
StackTraceElement[] stackTrace = log.exception.getStackTrace();
builder.append(log.exception.toString());
builder.append("<br>\n");
if (stackTrace.length != 0) {
for (StackTraceElement element : log.getException().getStackTrace()) {
for (StackTraceElement element : log.exception.getStackTrace()) {
builder.append(" -- ");
builder.append(element.toString());
builder.append("<br>\n");
@ -129,23 +125,4 @@ class Logger {
return builder.toString();
}
private String readFile(String filePath) {
StringBuilder builder = new StringBuilder();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
Scanner scanner = new Scanner(bufferedReader);
while (scanner.hasNext()) {
builder.append(scanner.nextLine());
builder.append("\n");
}
scanner.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}

View file

@ -24,17 +24,12 @@ import java.time.format.DateTimeFormatter;
public class LoggingService {
private Logger logger;
private DateTimeFormatter dateFormat;
private String message;
private Exception exception;
private LocalDateTime time;
private SevereLogger severeLogger = new SevereLogger();
private WarningLogger warningLogger = new WarningLogger();
private InfoLogger infoLogger = new InfoLogger();
private Log log;
public LoggingService(Level writerLevel) {
logger = new Logger(writerLevel);
dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
this.log = new Log();
this.logger = new Logger(writerLevel);
this.dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
}
public void logSevere(String message, Exception exception, LocalDateTime time) {
@ -53,47 +48,23 @@ public class LoggingService {
}
private void setValues(String message, Exception exception, LocalDateTime time) {
this.message = message;
this.exception = exception;
this.time = time;
this.log.message = message;
this.log.exception = exception;
this.log.time = dateFormat.format(time);
}
private class SevereLogger implements Runnable {
@Override
public void run() {
System.out.println(message);
Log log = new Log();
log.setLevel(Level.SEVERE);
log.setMessage(message);
log.setException(exception);
log.setTime(dateFormat.format(time));
logger.log(log);
}
}
private Runnable severeLogger = () -> {
this.log.level = Level.SEVERE;
this.logger.log(this.log);
};
private class WarningLogger implements Runnable {
@Override
public void run() {
System.out.println(message);
Log log = new Log();
log.setLevel(Level.WARNING);
log.setMessage(message);
log.setException(exception);
log.setTime(dateFormat.format(time));
logger.log(log);
}
}
private Runnable warningLogger = () -> {
this.log.level = Level.WARNING;
this.logger.log(log);
};
private class InfoLogger implements Runnable {
@Override
public void run() {
System.out.println(message);
Log log = new Log();
log.setLevel(Level.INFO);
log.setMessage(message);
log.setTime(dateFormat.format(time));
logger.log(log);
}
}
private Runnable infoLogger = () -> {
this.log.level = Level.INFO;
this.logger.log(log);
};
}

View file

@ -2,5 +2,4 @@
<strong>%% LogLevel %%</strong> %% Time %% <br>
<strong>%% Message %%</strong> <br>
%% StackTrace %%
</p>
<!-- Placeholder for new log -->
</p>

View file

@ -55,6 +55,3 @@
</head>
<body>
<h1>Everest Log: %% Date %%</h1>
<!-- Placeholder for new log -->
</body>
</html>