Skip to content

Commit

Permalink
Merge pull request #97 from hkupty/minilogger-refactor
Browse files Browse the repository at this point in the history
feat: Refactor mini logger
  • Loading branch information
hkupty authored Mar 23, 2024
2 parents 2babc3e + fa33146 commit a70391d
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
path: penna-api/build/reports/pmd/main.html
- store_artifacts:
path: penna-core/build/reports/pmd/main.html
- store_artifacts:
path: penna-core/.jqwik-database
- store_artifacts:
path: penna-dev/build/reports/pmd/main.html
- store_artifacts:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ gradle-app.setting
.idea_modules/
www/public
www/resources

.jqwik-database
6 changes: 6 additions & 0 deletions buildSrc/src/main/groovy/penna.publishing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ java {
withSourcesJar()
}

jar {
manifest {
attributes 'Implementation-Version': version,
'Implementation-Title': project.name
}
}

publishing {
publications {
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import org.slf4j.event.LoggingEvent;
import org.slf4j.spi.LoggingEventBuilder;
import penna.core.internals.LogUnitContextPool;
import penna.core.minilog.MiniLogger;
import penna.core.models.KeyValuePair;
import penna.core.models.PennaLogEvent;
import penna.core.sink.Sink;

import java.io.IOException;
import java.util.function.Supplier;

import static penna.api.audit.Logger.reportError;

public record LogUnitContext(
LogUnitContextPool pool,
int selfReference,
Expand Down Expand Up @@ -116,7 +117,7 @@ public void log() {
try {
sink.write(logEvent);
} catch (IOException e) {
MiniLogger.error("Unable to write log.", e);
reportError("ERROR", "Unable to write log.", e);
} finally {
release();
}
Expand Down
33 changes: 0 additions & 33 deletions penna-core/src/main/java/penna/core/minilog/MiniLogger.java

This file was deleted.

5 changes: 3 additions & 2 deletions penna-core/src/main/java/penna/core/sink/CoreSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import penna.api.models.LogField;
import penna.core.internals.DirectJson;
import penna.core.internals.StackTraceBloomFilter;
import penna.core.minilog.MiniLogger;
import penna.core.models.LogConfig;
import penna.core.models.PennaLogEvent;
import penna.core.slf4j.PennaMDCAdapter;
Expand All @@ -19,6 +18,8 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;

import static penna.api.audit.Logger.report;

public final class CoreSink implements Sink, Closeable {
private static final byte[] SUPPRESSED = "suppressed".getBytes();
private static final byte[] STACKTRACE = "stacktrace".getBytes();
Expand Down Expand Up @@ -65,7 +66,7 @@ public CoreSink(WritableByteChannel channel) {
if (MDC.getMDCAdapter() instanceof PennaMDCAdapter adapter) {
mdcAdapter = adapter;
} else {
MiniLogger.error("Not using PennaMDCAdapter for some reason! MDC will be off");
report("ERROR", "Not using PennaMDCAdapter for some reason! MDC will be off");
mdcAdapter = null;
}
jsonGenerator = new DirectJson(channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import penna.api.config.Manager;
import penna.core.internals.ManagerHolder;

import static penna.api.audit.Logger.report;

public final class PennaServiceProvider implements SLF4JServiceProvider {

/**
Expand Down Expand Up @@ -50,5 +52,6 @@ public void initialize() {
this.loggerFactory = pennaLoggerFactory;
markerFactory = new BasicMarkerFactory();
mdcAdapter = new PennaMDCAdapter();
report("INFO", "Penna initialized successfully");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import static penna.api.audit.Logger.report;
import static penna.api.audit.Logger.reportError;

/**
* Implementation of the {@link Provider} interface that extends the {@link Manager}
* by adding configuration from a yaml file source.
Expand Down Expand Up @@ -57,9 +60,9 @@ public boolean register(Manager manager) {
if (configMap.watch()) {
startWorker();
}

return true;
} catch (Exception ex) {
} catch (Exception ignored) {
report("INFO", "yaml config not registered");
return false;
}
}
Expand All @@ -79,7 +82,9 @@ private void startWorker() {
}
key.reset();
}
} catch (Exception ignored) {}
} catch (Exception exception) {
reportError("WARN", "yaml config file watcher got error, closing", exception);
}
});
}

Expand All @@ -105,8 +110,8 @@ private void refresh() throws IOException {
public void init() {
try {
refresh();
} catch (Exception ignored) {
// TODO log
} catch (Exception exception) {
reportError("WARN", "yaml config init got error", exception);
}
}
}

0 comments on commit a70391d

Please sign in to comment.