Last active
December 16, 2021 07:28
-
-
Save kimmowikman/388a77953b7b7071956a9400a8766480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fi.luotocompany.logger; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
/** | |
* Simple and secure logger by Kimmo Wikman, Luoto Company | |
*/ | |
public class SimpleLogger { | |
public enum LogLevel {DEBUG, INFO, WARN, ERROR} | |
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | |
private final LogLevel level; | |
private final String sourceName; | |
public SimpleLogger(final LogLevel level, final Class source) { | |
this.level = level; | |
this.sourceName = source.getName(); | |
} | |
public void log(final LogLevel logLevel, final String message) { | |
if (this.level.ordinal() <= logLevel.ordinal()) { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append(LocalDateTime.now().format(DATE_FORMAT)) | |
.append(" [") | |
.append(logLevel.toString()) | |
.append("] ") | |
.append(sourceName) | |
.append(" - ") | |
.append(message); | |
System.out.println(sb.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment