-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from hkupty/minilogger-refactor
feat: Refactor mini logger
- Loading branch information
Showing
12 changed files
with
133 additions
and
42 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ gradle-app.setting | |
.idea_modules/ | ||
www/public | ||
www/resources | ||
|
||
.jqwik-database |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ | |
|
||
exports penna.api.models; | ||
exports penna.api.config; | ||
exports penna.api.audit; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package penna.api.audit; | ||
|
||
import penna.api.audit.internal.StdoutLogger; | ||
|
||
/** | ||
* This class provides static methods for the logger to report back, specially during initialization. | ||
* It is supposed to be as simple as possible and as minimally used as possible. | ||
*/ | ||
public class Logger { | ||
private Logger() {} | ||
private static final PseudoLogger impl = new StdoutLogger(); | ||
|
||
/** | ||
* Reports through the underlying logger some important event. To be used sparingly. | ||
* @param level The target level. | ||
* @param event The message to be printed. | ||
*/ | ||
public static void report(String level, String event) { | ||
impl.report(level, event); | ||
} | ||
|
||
/** | ||
* Reports an error, usually in the form of an {@link Exception}. To be used sparingly, in situations | ||
* where the logger malfunctions and needs to explain upstream why it didn't work. | ||
* @param level The target level. | ||
* @param event The message to be printed. | ||
* @param throwable The exception or error that caused the report. | ||
*/ | ||
public static void reportError(String level, String event, Throwable throwable) { | ||
impl.reportError(level, event, throwable); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package penna.api.audit; | ||
|
||
/** | ||
* This is the internal logging mechanism, so we can still log important messages if something fails on initialization. | ||
*/ | ||
public interface PseudoLogger { | ||
/** | ||
* This method should be used to notify an event; | ||
* @param level How critical this event is; | ||
* @param event The event as a short string; | ||
*/ | ||
void report(String level, String event); | ||
|
||
|
||
/** | ||
* This method should be used to notify a failure or an exception, handled or not; | ||
* @param level How critical is the situation | ||
* @param event The event as a short string; | ||
* @param throwable The error that happened | ||
*/ | ||
void reportError(String level, String event, Throwable throwable); | ||
} |
49 changes: 49 additions & 0 deletions
49
penna-api/src/main/java/penna/api/audit/internal/StdoutLogger.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package penna.api.audit.internal; | ||
|
||
import penna.api.audit.PseudoLogger; | ||
|
||
/** | ||
* Simple string-concat based logger just to print important messages to stdout. | ||
* See {@link PseudoLogger} for more information. | ||
*/ | ||
public class StdoutLogger implements PseudoLogger { | ||
private final String version = StdoutLogger.class.getPackage().getImplementationVersion(); | ||
|
||
/** | ||
* Builds an audit logger instance. | ||
* See {@link PseudoLogger} for more information. | ||
*/ | ||
public StdoutLogger() {} | ||
|
||
@Override | ||
public void report(String level, String event) { | ||
String sb = "{\"logger\":\"penna.api.audit.Logger\",\"level\":\"" + | ||
level + | ||
"\",\"message\":\"" + | ||
event + | ||
"\",\"pennaVersion\":\"" + | ||
version + | ||
"\"}"; | ||
System.out.println(sb); | ||
} | ||
|
||
@Override | ||
public void reportError(String level, String event, Throwable throwable) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("{\"logger\":\"penna.api.audit.Logger\",\"level\":\""); | ||
sb.append(level); | ||
sb.append("\",\"message\":\""); | ||
sb.append(event); | ||
sb.append("\",\"error\":\""); | ||
sb.append(throwable.getMessage()); | ||
sb.append("\",\"stacktrace\":\""); | ||
for (StackTraceElement stackTraceElement : throwable.getStackTrace()) { | ||
sb.append(stackTraceElement.toString()); | ||
sb.append("\\n"); | ||
} | ||
sb.append("\",\"pennaVersion\":\""); | ||
sb.append(version); | ||
sb.append("\"}"); | ||
System.out.println(sb); | ||
} | ||
} |
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
33 changes: 0 additions & 33 deletions
33
penna-core/src/main/java/penna/core/minilog/MiniLogger.java
This file was deleted.
Oops, something went wrong.
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
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
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