Skip to content

Commit

Permalink
bosk-logback library
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Jul 6, 2024
1 parent bd914cd commit 959f2ca
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 17 deletions.
14 changes: 14 additions & 0 deletions bosk-core/src/main/java/works/bosk/logging/MdcKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package works.bosk.logging;

/**
* Keys to use for SLF4J's Mapped Diagnostic Context.
* <p>
* Evolution note: we're going to want to get organized in how we generate MDC.
* For now, only bosk-mongo uses these.
*/
public final class MdcKeys {
public static final String BOSK_NAME = "bosk.name";
public static final String BOSK_INSTANCE_ID = "bosk.instanceID";
public static final String EVENT = "bosk.MongoDriver.event";
public static final String TRANSACTION = "bosk.MongoDriver.transaction";
}
4 changes: 4 additions & 0 deletions bosk-logback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## bosk-logback

This is the subproject for the published `bosk-logback` library,
containing extra support for the Logback implementation of SLF4J.
20 changes: 20 additions & 0 deletions bosk-logback/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

plugins {
id 'bosk.development'
id 'bosk.maven-publish'
id 'com.github.spotbugs' version '5.1.5'
}

java {
sourceCompatibility = '17'
targetCompatibility = '17'
}

repositories {
mavenCentral()
}

dependencies {
api project(":bosk-core")
implementation "ch.qos.logback:logback-classic:1.4.14"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package works.bosk.drivers.mongo;
package works.bosk.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
Expand All @@ -13,10 +13,12 @@
import works.bosk.Bosk;
import works.bosk.DriverFactory;
import works.bosk.StateTreeNode;
import works.bosk.logging.MdcKeys;

import static ch.qos.logback.core.spi.FilterReply.DENY;
import static ch.qos.logback.core.spi.FilterReply.NEUTRAL;
import static java.util.stream.Collectors.toMap;
import static works.bosk.logging.MdcKeys.BOSK_INSTANCE_ID;

public class BoskLogFilter extends Filter<ILoggingEvent> {
private static final ConcurrentHashMap<String, LogController> controllersByBoskID = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -62,7 +64,7 @@ public static <R extends StateTreeNode> DriverFactory<R> withController(LogContr

@Override
public FilterReply decide(ILoggingEvent event) {
String boskID = MDC.get(MdcKeys.BOSK_INSTANCE_ID);
String boskID = MDC.get(BOSK_INSTANCE_ID);
if (boskID == null) {
return NEUTRAL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import works.bosk.Identifier;
import works.bosk.logging.MdcKeys;

import static java.lang.Thread.currentThread;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.MDC;
import works.bosk.Identifier;
import works.bosk.logging.MdcKeys;

final class MappedDiagnosticContext {

Expand Down
12 changes: 0 additions & 12 deletions bosk-mongo/src/main/java/works/bosk/drivers/mongo/MdcKeys.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import works.bosk.logging.MdcKeys;

/**
* A wrapper for {@link MongoCollection} that manages a thread-local
Expand Down
1 change: 1 addition & 0 deletions lib-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
// It's a mild kind of circular dependency we can probably live with for now.
implementation project(":bosk-core")
implementation project(":bosk-jackson")
implementation project(":bosk-logback")
implementation project(":bosk-mongo")

// The bosk.development plugin brings these in as test dependencies,
Expand Down
4 changes: 2 additions & 2 deletions lib-testing/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="works.bosk.drivers.mongo.BoskLogFilter"/>
<filter class="works.bosk.logback.BoskLogFilter"/>
<encoder>
<pattern>%d %-5level [%thread] [%X{bosk.name}]%X{bosk.MongoDriver.transaction}%X{bosk.MongoDriver.event} %logger{25}: %msg%n</pattern>
</encoder>
Expand All @@ -19,7 +19,7 @@
<logger name="works.bosk.drivers.DriverConformanceTest" level="DEBUG"/>
<logger name="works.bosk.drivers.mongo.PandoFormatDriver" level="TRACE"/>
<logger name="works.bosk.drivers.mongo.MainDriver" level="TRACE"/>
<logger name="works.bosk.drivers.mongo.BoskLogFilter" level="DEBUG"/>
<logger name="works.bosk.logback.BoskLogFilter" level="DEBUG"/>
-->

</configuration>
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

rootProject.name = 'bosk'
include 'bosk-annotations', 'bosk-core', 'bosk-jackson', 'bosk-mongo', 'bosk-spring-boot-3', 'bosk-testing', 'lib-testing', 'example-hello'
include 'bosk-annotations', 'bosk-core', 'bosk-jackson', 'bosk-logback', 'bosk-mongo', 'bosk-spring-boot-3', 'bosk-testing', 'lib-testing', 'example-hello'

0 comments on commit 959f2ca

Please sign in to comment.