Skip to content

Commit

Permalink
feat: Introduce penna.api.audit
Browse files Browse the repository at this point in the history
This should allow for better logging in exceptional cases without the
need for the proper logging infrastructure. Since the logger won't be
available during runtime, we should use this as a direct means of
notifying stdout of important events.
  • Loading branch information
hkupty committed Mar 23, 2024
1 parent d101cb9 commit 8ee3347
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions penna-api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

exports penna.api.models;
exports penna.api.config;
exports penna.api.audit;
}
32 changes: 32 additions & 0 deletions penna-api/src/main/java/penna/api/audit/Logger.java
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);
}
}
22 changes: 22 additions & 0 deletions penna-api/src/main/java/penna/api/audit/PseudoLogger.java
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 penna-api/src/main/java/penna/api/audit/internal/StdoutLogger.java
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);
}
}

0 comments on commit 8ee3347

Please sign in to comment.