Skip to content

Commit

Permalink
elf4j version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Apr 13, 2024
1 parent db9a21a commit e1668c6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ application would use runtime-scope for this provider as a dependency:
```

Note: Only one logging provider such as this should be in effect at run-time. If multiple providers end up in the final
build of an application, somehow, then the `elf4j.logger.factory.fqcn` system property will have to be used to select
build of an application, somehow, then the `elf4j.service.provider.fqcn` system property will have to be used to select
the desired provider.

```
Expand Down
29 changes: 27 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-slf4j</artifactId>
<version>4.0.0</version>
<version>4.1.0</version>

<packaging>jar</packaging>
<name>elf4j-slf4j</name>
Expand Down Expand Up @@ -68,7 +68,7 @@
<dependency>
<groupId>io.github.elf4j</groupId>
<artifactId>elf4j</artifactId>
<version>4.0.0</version>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -136,6 +136,31 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version>
<executions>
<execution>
<id>spotless-apply-id</id>
<phase>process-sources</phase>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
<configuration>
<java>
<palantirJavaFormat>
<version>2.43.0</version>
<style>PALANTIR</style> <!-- or AOSP/GOOGLE (optional) -->
<formatJavadoc>true
</formatJavadoc> <!-- defaults to false (optional, requires at least Palantir 2.39.0) -->
</palantirJavaFormat>
<formatAnnotations/>
</java>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public CallerBoundaryImmutableLoggingEventBuilder(Logger logger, Level level, St

@Override
public void setCallerBoundary(String fqcn) {
//noop
// noop
}
}
27 changes: 15 additions & 12 deletions src/main/java/elf4j/slf4j/Slf4jLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@

package elf4j.slf4j;

import static elf4j.Level.*;

import elf4j.Level;
import elf4j.Logger;
import elf4j.util.NoopLogger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import javax.annotation.concurrent.Immutable;
import lombok.NonNull;
import lombok.ToString;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggingEventBuilder;

import javax.annotation.concurrent.Immutable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import static elf4j.Level.*;

@Immutable
@ToString
class Slf4jLogger implements Logger {
Expand All @@ -48,8 +47,11 @@ class Slf4jLogger implements Logger {
private static final String CALLER_BOUNDARY_FQCN = Slf4jLogger.class.getName();
private static final EnumMap<Level, org.slf4j.event.Level> LEVEL_MAP = setLevelMap();
private static final EnumMap<Level, Map<String, Slf4jLogger>> LOGGER_CACHE = initLoggerCache();

@NonNull private final String name;

@NonNull private final Level level;

@NonNull private final org.slf4j.Logger delegateLogger;

private Slf4jLogger(@NonNull String name, @NonNull Level level) {
Expand All @@ -71,7 +73,7 @@ private static Slf4jLogger getLogger(String name) {
return getLogger(name, DEFAULT_LEVEL);
}

private static EnumMap<Level, Map<String, Slf4jLogger>> initLoggerCache() {
private static @NonNull EnumMap<Level, Map<String, Slf4jLogger>> initLoggerCache() {
EnumMap<Level, Map<String, Slf4jLogger>> loggerCache = new EnumMap<>(Level.class);
EnumSet.allOf(Level.class).forEach(level -> loggerCache.put(level, new ConcurrentHashMap<>()));
return loggerCache;
Expand All @@ -95,7 +97,7 @@ private static StackTraceElement serviceClient() {
+ Arrays.toString(stackTrace));
}

private static EnumMap<Level, org.slf4j.event.Level> setLevelMap() {
private static @NonNull EnumMap<Level, org.slf4j.event.Level> setLevelMap() {
EnumMap<Level, org.slf4j.event.Level> levelMap = new EnumMap<>(Level.class);
levelMap.put(TRACE, org.slf4j.event.Level.TRACE);
levelMap.put(DEBUG, org.slf4j.event.Level.DEBUG);
Expand Down Expand Up @@ -177,9 +179,10 @@ private void slf4jLog(Throwable t, Object message, Object... args) {
if (!isEnabled()) {
return;
}
LoggingEventBuilder loggingEventBuilder = new CallerBoundaryImmutableLoggingEventBuilder(delegateLogger,
translate(this.level),
CALLER_BOUNDARY_FQCN).setMessage(Objects.toString(supply(message))).setCause(t);
LoggingEventBuilder loggingEventBuilder = new CallerBoundaryImmutableLoggingEventBuilder(
delegateLogger, translate(this.level), CALLER_BOUNDARY_FQCN)
.setMessage(Objects.toString(supply(message)))
.setCause(t);
if (args != null) {
for (Object arg : args) {
loggingEventBuilder = loggingEventBuilder.addArgument(supply(arg));
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/elf4j/slf4j/Slf4jLoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import elf4j.Logger;
import elf4j.spi.LogServiceProvider;

/**
*
*/
/** */
public class Slf4jLoggerFactory implements LogServiceProvider {
@Override
public Logger logger() {
Expand Down
48 changes: 28 additions & 20 deletions src/test/java/elf4j/slf4j/Slf4jLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@

package elf4j.slf4j;

import static org.junit.jupiter.api.Assertions.*;

import elf4j.Level;
import elf4j.Logger;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class Slf4jLoggerTest {
public static final Logger LOGGER = Logger.instance();
Expand Down Expand Up @@ -64,7 +63,7 @@ void object() {

@Test
void supplier() {
LOGGER.atTrace().log((Supplier) () -> "supplier message");
LOGGER.atTrace().log(() -> "supplier message");
}

@Test
Expand All @@ -75,10 +74,11 @@ void messageAndArgs() {
@Test
void messageAndSuppliers() {
LOGGER.atWarn()
.log("message supplier arg1 {}, arg2 {}, arg3 {}",
(Supplier) () -> "a11111",
"a22222",
(Supplier) () -> Arrays.stream(new Object[] { "a33333" }).collect(Collectors.toList()));
.log(
"message supplier arg1 {}, arg2 {}, arg3 {}",
() -> "a11111",
() -> "a22222",
() -> Arrays.stream(new Object[] {"a33333"}).collect(Collectors.toList()));
}

@Test
Expand All @@ -93,7 +93,7 @@ void throwableAndMessage() {

@Test
void throwableAndSupplier() {
LOGGER.atError().log(new Exception("ex message"), (Supplier) () -> "supplier log message");
LOGGER.atError().log(new Exception("ex message"), () -> "supplier log message");
}

@Test
Expand All @@ -104,11 +104,13 @@ void throwableAndMessageAndArgs() {
@Test
void throwableAndMessageAndSupplierArgs() {
LOGGER.atError()
.log(new Exception("ex message"),
.log(
new Exception("ex message"),
"log message with supplier arg1 {}, arg2 {}, arg3 {}",
"a11111",
"a22222",
(Supplier) () -> Arrays.stream(new Object[] { "a33333" }).collect(Collectors.toList()));
(Supplier)
() -> Arrays.stream(new Object[] {"a33333"}).collect(Collectors.toList()));
}
}

Expand All @@ -134,14 +136,17 @@ void messagesArgsAndGuards() {
assertEquals(logger.getName(), ((Slf4jLogger) debug).getName());
assertEquals(Level.DEBUG, debug.getLevel());
if (debug.isEnabled()) {
debug.log("a {} guarded by a {}, so {} is created {} DEBUG level is {}",
debug.log(
"a {} guarded by a {}, so {} is created {} DEBUG level is {}",
"long message",
"level check",
"no message object",
"unless",
"enabled by the configuration of the logging provider");
}
debug.log((Supplier) () -> "alternative to the level guard, using a supplier function should achieve the same goal, pending quality of the logging provider");
debug.log(
() ->
"alternative to the level guard, using a supplier function should achieve the same goal, pending quality of the logging provider");
}

@Test
Expand All @@ -153,25 +158,28 @@ void throwableAndMessageAndArgs() {
error.log(ex);
error.log(ex, "level set omitted but we know the level is Level.ERROR");
error.atWarn()
.log(ex,
.log(
ex,
"the log level switched to WARN on the fly. that is, {} returns a {} and {} Logger {}",
"atWarn()",
"different",
"immutable",
"instance");
error.atError()
.log(ex,
.log(
ex,
"here the {} call is {} because the {} instance is {}, and the instance's log level has and will always be Level.ERROR",
"atError()",
"unnecessary",
"error logger",
"immutable");
error.log(ex,
error.log(
ex,
"now at Level.ERROR, together with the exception stack trace, logging some items expensive to compute: item1 {}, item2 {}, item3 {}, item4 {}, ...",
"i11111",
"i22222",
"i33333",
(Supplier) () -> Arrays.stream(new Object[] { "i44444" }).collect(Collectors.toList()));
(Supplier) () -> Arrays.stream(new Object[] {"i44444"}).collect(Collectors.toList()));
}
}
}
}

0 comments on commit e1668c6

Please sign in to comment.