Skip to content

Commit

Permalink
elf4j spi version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Mar 20, 2023
1 parent 4ceaa7e commit 8923d83
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 54 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ make a native logging service provider implementation of the [ELF4J](https://git
Java) SPI. See [elf4j-impl](https://github.com/elf4j/elf4j-impl) for descriptions.

While directly implementing the [ELF4J](https://github.com/elf4j/elf4j) API, this is also a stand-alone Java logging
engine. It is designed to be adaptable to service other logging APIs, for example, as
a [logging engine for SLF4J](https://github.com/elf4j/slf4j-elf4j).
engine. It is designed to be adaptable to service other logging APIs, for example, as a logging engine
for [SLF4J](https://github.com/elf4j/slf4j-elf4j) or Java Platform Logging
API ([JPL](https://github.com/elf4j/jpl-elf4j)) (System.Logger).
4 changes: 2 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-impl-core</artifactId>
<version>0.6.4</version>
<version>0.7.0</version>
<packaging>jar</packaging>
<name>elf4j-impl-core</name>
<description>Native logging service provider implementation of ELF4J (Easy Logging Facade For Java)
Expand Down Expand Up @@ -68,7 +68,7 @@
<dependency>
<groupId>io.github.elf4j</groupId>
<artifactId>elf4j</artifactId>
<version>2.0.6</version>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
52 changes: 9 additions & 43 deletions src/main/java/elf4j/impl/core/NativeLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
* Instances of this class are thread-safe, and can be used as class, instance, or local variables. It is recommended,
* however, to use class variables for instances that are returned by the static factory method
* {@link Logger#instance()}, as those instances are more expensive to create. Other instances returned by the
* fluent-style factory methods, such as {@link Logger#atError()}, are inexpensive to create and can be used (and
* discarded) in-line or as convenient.
* however, to use class variables for instances returned by the static factory method {@link Logger#instance()}, as
* those instances are more expensive to create. Other instances returned by the fluent-style factory methods, such as
* {@link Logger#atLevel}, are inexpensive to create and can be used (and discarded) in-line or as convenient.
*/
@ThreadSafe
@Value
public class NativeLogger implements Logger {
private static final Map<Level, Map<String, NativeLogger>> NATIVE_LOGGERS =
EnumSet.allOf(Level.class).stream().collect(Collectors.toMap(Function.identity(), l -> new HashMap<>()));
EnumSet.allOf(Level.class).stream().collect(toMap(Function.identity(), l -> new HashMap<>()));

/**
* Name of this logger's "owner class" - the logging service client class that first requested for this logger
Expand Down Expand Up @@ -82,33 +82,9 @@ public NativeLogger(@NonNull String ownerClassName, @NonNull Level level, @NonNu
}

@Override
public NativeLogger atDebug() {
return atLevel(Level.DEBUG);
}

@Override
public NativeLogger atError() {
return atLevel(Level.ERROR);
}

@Override
public NativeLogger atInfo() {
return atLevel(Level.INFO);
}

@Override
public NativeLogger atTrace() {
return atLevel(Level.TRACE);
}

@Override
public NativeLogger atWarn() {
return atLevel(Level.WARN);
}

@Override
public @NonNull Level getLevel() {
return this.level;
public NativeLogger atLevel(Level level) {
return this.level == level ? this : NATIVE_LOGGERS.get(level)
.computeIfAbsent(this.ownerClassName, k -> new NativeLogger(k, level, this.logService));
}

@Override
Expand Down Expand Up @@ -141,16 +117,6 @@ public void log(Throwable t, String message, Object... args) {
this.service(t, message, args);
}

/**
* @param level of the returned logger instance
* @return logger instance of the same owner class name, with the specified level
*/
public NativeLogger atLevel(Level level) {
return this.level == level ? this : NATIVE_LOGGERS.get(level)
.computeIfAbsent(this.ownerClassName,
k -> new NativeLogger(this.ownerClassName, level, this.logService));
}

private void service(Throwable exception, Object message, Object[] args) {
this.logService.log(this, NativeLogger.class, exception, message, args);
}
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/elf4j/impl/core/NativeLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void init() {
class atLevels {
@Test
void instanceForDifferentLevel() {
NativeLogger info = nativeLogger.atInfo();
NativeLogger warn = info.atWarn();
NativeLogger info = (NativeLogger) nativeLogger.atInfo();
NativeLogger warn = (NativeLogger) info.atWarn();

assertNotSame(warn, info);
assertEquals(info.getOwnerClassName(), warn.getOwnerClassName());
Expand All @@ -67,11 +67,11 @@ void instanceForDifferentLevel() {

@Test
void instanceForSameLevel() {
NativeLogger trace = nativeLogger.atTrace();
NativeLogger debug = nativeLogger.atDebug();
NativeLogger info = nativeLogger.atInfo();
NativeLogger warn = nativeLogger.atWarn();
NativeLogger error = nativeLogger.atError();
NativeLogger trace = (NativeLogger) nativeLogger.atTrace();
NativeLogger debug = (NativeLogger) nativeLogger.atDebug();
NativeLogger info = (NativeLogger) nativeLogger.atInfo();
NativeLogger warn = (NativeLogger) nativeLogger.atWarn();
NativeLogger error = (NativeLogger) nativeLogger.atError();

assertSame(trace, trace.atTrace());
assertSame(debug, debug.atDebug());
Expand Down

0 comments on commit 8923d83

Please sign in to comment.