Skip to content

Commit

Permalink
Update logger context (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent authored Jul 23, 2023
1 parent b1cdf5c commit 3555b66
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface CoreLogger {
@NotNull
String getName();

@NotNull
LoggerContext getLoggerContext();

/**
* Returns the given condition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public String getName() {
return core.getName();
}

@Override
public @NotNull LoggerContext getLoggerContext() {
return core.getLoggerContext();
}

@Override
@NotNull
public Condition condition() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tersesystems.echopraxia.spi;

import com.tersesystems.echopraxia.api.Field;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public interface LoggerContext {

@NotNull
List<Field> getLoggerFields();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.tersesystems.echopraxia.fake;

import static com.tersesystems.echopraxia.spi.Utilities.joinFields;

import com.tersesystems.echopraxia.api.*;
import com.tersesystems.echopraxia.spi.CoreLogger;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
Expand All @@ -14,7 +18,7 @@

public class FakeCoreLogger implements CoreLogger {

private final FakeLoggingContext context;
private final FakeLoggerContext context;
private final Condition condition;
private final Executor executor;
private final String fqcn;
Expand All @@ -23,15 +27,15 @@ public class FakeCoreLogger implements CoreLogger {

public FakeCoreLogger(String fqcn) {
this.fqcn = fqcn;
this.context = FakeLoggingContext.empty(this);
this.context = FakeLoggerContext.empty();
this.condition = Condition.always();
this.executor = ForkJoinPool.commonPool();
this.tlsSupplier = () -> (Runnable) () -> {};
}

public FakeCoreLogger(
String fqcn,
FakeLoggingContext context,
FakeLoggerContext context,
Condition condition,
Executor executor,
Supplier<Runnable> tlsSupplier) {
Expand All @@ -49,25 +53,39 @@ public FakeCoreLogger(

@Override
public boolean isEnabled(@NotNull Level level) {
return this.condition.test(level, context);
return this.condition.test(
level, new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList));
}

@Override
public boolean isEnabled(@NotNull Level level, @NotNull Condition condition) {
return this.condition.and(condition).test(level, context);
return this.condition
.and(condition)
.test(
level, new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList));
}

@Override
public boolean isEnabled(@NotNull Level level, @NotNull Supplier<List<Field>> extraFields) {
return this.condition.test(level, context.withFields(extraFields));
return this.condition.test(
level,
new FakeLoggingContext(
this, () -> context.withFields(extraFields).getLoggerFields(), Collections::emptyList));
}

@Override
public boolean isEnabled(
@NotNull Level level,
@NotNull Condition condition,
@NotNull Supplier<List<Field>> extraFields) {
return this.condition.and(condition).test(level, context.withFields(extraFields));
return this.condition
.and(condition)
.test(
level,
new FakeLoggingContext(
this,
() -> context.withFields(extraFields).getLoggerFields(),
Collections::emptyList));
}

@Override
Expand All @@ -80,11 +98,17 @@ public boolean isEnabled(
return fqcn;
}

@NotNull
public LoggerContext getLoggerContext() {
return context;
}

@Override
public @NotNull <FB> CoreLogger withFields(
@NotNull Function<FB, FieldBuilderResult> f, @NotNull FB builder) {
FakeLoggingContext ctx =
new FakeLoggingContext(this, context::getLoggerFields, () -> convert(f.apply(builder)));
FakeLoggerContext ctx =
new FakeLoggerContext(
joinFields(() -> context.getLoggerFields(), () -> convert(f.apply(builder))));
return new FakeCoreLogger(fqcn, ctx, this.condition.and(condition), executor, tlsSupplier);
}

Expand Down Expand Up @@ -122,17 +146,21 @@ private List<Field> convert(FieldBuilderResult input) {

@Override
public void log(@NotNull Level level, @Nullable String message) {
if (isEnabledFor(level) && this.condition.test(level, context)) {
List<Field> fields = context.getFields();
FakeLoggingContext memo =
new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList);
if (isEnabledFor(level) && this.condition.test(level, memo)) {
List<Field> fields = memo.getFields();
System.out.printf("" + message + " level %s fields %s\n", level, fields);
}
}

@Override
public void log(
@NotNull Level level, @NotNull Supplier<List<Field>> extraFields, @Nullable String message) {
if (isEnabledFor(level) && this.condition.test(level, context)) {
List<Field> fields = context.withFields(extraFields).getFields();
FakeLoggingContext ctx =
new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList);
if (isEnabledFor(level) && this.condition.test(level, ctx)) {
List<Field> fields = ctx.withFields(extraFields).getFields();
System.out.printf("" + message + " level %s fields %s\n", level, fields);
}
}
Expand All @@ -146,9 +174,9 @@ public <FB> void log(
List<Field> args = convert(f.apply(builder));
if (isEnabledFor(level)) {
FakeLoggingContext memo =
new FakeLoggingContext(this, context::getLoggerFields, context::getArgumentFields);
new FakeLoggingContext(this, context::getLoggerFields, () -> f.apply(builder).fields());
if (this.condition.test(level, memo)) {
List<Field> fields = context.getFields();
List<Field> fields = memo.getFields();
System.out.printf("" + message + " level %s fields %s args %s\n", level, fields, args);
}
}
Expand All @@ -164,19 +192,20 @@ public <FB> void log(
List<Field> args = convert(f.apply(builder));
if (isEnabledFor(level)) {
FakeLoggingContext memo =
new FakeLoggingContext(
FakeCoreLogger.this, context::getLoggerFields, context::getArgumentFields);
new FakeLoggingContext(this, context::getLoggerFields, () -> f.apply(builder).fields());
if (this.condition.test(level, memo)) {
List<Field> fields = context.getFields();
List<Field> fields = memo.getFields();
System.out.printf("" + message + " level %s fields %s args %s\n", level, fields, args);
}
}
}

@Override
public void log(@NotNull Level level, @NotNull Condition condition, @Nullable String message) {
if (isEnabledFor(level) && this.condition.and(condition).test(level, context)) {
List<Field> fields = context.getLoggerFields();
FakeLoggingContext memo =
new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList);
if (isEnabledFor(level) && this.condition.and(condition).test(level, memo)) {
List<Field> fields = memo.getLoggerFields();
System.out.printf("" + message + " level %s fields %s\n", level, fields);
}
}
Expand All @@ -187,7 +216,9 @@ public void log(
@NotNull Supplier<List<Field>> extraFields,
@NotNull Condition condition,
@Nullable String message) {
if (isEnabledFor(level) && this.condition.and(condition).test(level, context)) {
FakeLoggingContext memo =
new FakeLoggingContext(this, context::getLoggerFields, Collections::emptyList);
if (isEnabledFor(level) && this.condition.and(condition).test(level, memo)) {
List<Field> fields = context.withFields(extraFields).getLoggerFields();
System.out.printf("" + message + " level %s fields %s\n", level, fields);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.tersesystems.echopraxia.fake;

import static com.tersesystems.echopraxia.spi.Utilities.joinFields;
import static com.tersesystems.echopraxia.spi.Utilities.memoize;

import com.tersesystems.echopraxia.api.Field;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;

public class FakeLoggerContext implements LoggerContext {
private final Supplier<List<Field>> fieldsSupplier;

public static FakeLoggerContext empty() {
return new FakeLoggerContext(Collections::emptyList);
}

public FakeLoggerContext(Supplier<List<Field>> fieldsSupplier) {
this.fieldsSupplier = memoize(fieldsSupplier);
}

@Override
public @NotNull List<Field> getLoggerFields() {
return fieldsSupplier.get();
}

public LoggerContext withFields(Supplier<List<Field>> extraFields) {
return new FakeLoggerContext(joinFields(fieldsSupplier, extraFields));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tersesystems.echopraxia.api.*;
import com.tersesystems.echopraxia.spi.CoreLogger;
import com.tersesystems.echopraxia.spi.EchopraxiaService;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +64,11 @@ public String getName() {
return logger.getName();
}

@Override
public @NotNull LoggerContext getLoggerContext() {
return context;
}

@Override
public @NotNull Condition condition() {
return this.condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import static com.tersesystems.echopraxia.spi.Utilities.joinFields;

import com.tersesystems.echopraxia.api.Field;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;

public class JULLoggerContext {
public class JULLoggerContext implements LoggerContext {
protected final Supplier<List<Field>> fieldsSupplier;

private static final JULLoggerContext EMPTY = new JULLoggerContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tersesystems.echopraxia.log4j.layout.EchopraxiaFieldsMessage;
import com.tersesystems.echopraxia.spi.CoreLogger;
import com.tersesystems.echopraxia.spi.EchopraxiaService;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -69,6 +70,11 @@ public String getName() {
return logger.getName();
}

@Override
public @NotNull LoggerContext getLoggerContext() {
return context;
}

@Override
public @NotNull Condition condition() {
return this.condition;
Expand Down Expand Up @@ -676,7 +682,7 @@ public String toString() {
return "Log4JCoreLogger[" + logger.getName() + "]";
}

protected static class Context {
protected static class Context implements LoggerContext {
protected final Supplier<List<Field>> fieldsSupplier;
protected final Marker marker;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ void clearThreadContext() {
ThreadContext.clearAll();
}

@Test
void testGetLoggerContext() {
Log4JCoreLogger core =
(Log4JCoreLogger) CoreLoggerFactory.getLogger(Logger.class.getName(), ContextTest.class);
var logger =
LoggerFactory.getLogger(core, FieldBuilder.instance())
.withFields(fb -> fb.string("herp", "derp"));
var coreWithFields = logger.core();
var fields = coreWithFields.getLoggerContext().getLoggerFields();

Field field = fields.get(0);
assertThat(field.name()).isEqualTo("herp");
assertThat(field.value().asString().raw()).isEqualTo("derp");
}

@Test
void testMarkers() {
Marker securityMarker = MarkerManager.getMarker("SECURITY");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.tersesystems.echopraxia.logback;

import com.tersesystems.echopraxia.api.Field;
import com.tersesystems.echopraxia.spi.LoggerContext;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Marker;

/** The logback context associated with the logger across multiple logging events. */
public interface LogbackLoggerContext {
public interface LogbackLoggerContext extends LoggerContext {

@NotNull
List<Field> getLoggerFields();

List<Marker> getMarkers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public String getName() {
return fqcn;
}

@Override
@NotNull
public com.tersesystems.echopraxia.spi.LoggerContext getLoggerContext() {
return context;
}

// Logstash specific, not part of CoreLogger API
public CoreLogger withMarkers(Marker... markers) {
final LogstashMarkerContext contextWithMarkers =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.tersesystems.echopraxia.api.Field;
import com.tersesystems.echopraxia.api.FieldBuilder;
import com.tersesystems.echopraxia.api.Value;
import com.tersesystems.echopraxia.spi.CoreLogger;
import com.tersesystems.echopraxia.spi.CoreLoggerFactory;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
Expand All @@ -35,6 +37,20 @@ void clearMDC() {
MDC.clear();
}

@Test
void testGetLoggerContext() {
CoreLogger core = CoreLoggerFactory.getLogger(Logger.class.getName(), ContextTest.class);
var logger =
LoggerFactory.getLogger(core, FieldBuilder.instance())
.withFields(fb -> fb.string("herp", "derp"));
var coreWithFields = logger.core();
var fields = coreWithFields.getLoggerContext().getLoggerFields();

Field field = fields.get(0);
assertThat(field.name()).isEqualTo("herp");
assertThat(field.value().asString().raw()).isEqualTo("derp");
}

@Test
void testMarkers() {

Expand Down

0 comments on commit 3555b66

Please sign in to comment.