Skip to content

Commit

Permalink
elf4j version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Mar 27, 2023
1 parent e4fa051 commit 905a90c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 42 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Easy Logging Facade for Java (ELF4J)
Copyright (c) 2022 Qingtian Wang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# elf4j-jul

An adapter to use [java.util.logging](https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html) as
service provider and runtime logging engine for the [ELF4J](https://github.com/elf4j/elf4j) (Easy Logging Facade for Java) API
service provider and runtime logging engine for the [ELF4J](https://github.com/elf4j/elf4j) (Easy Logging Facade for
Java) API

## User Story

Expand Down
32 changes: 26 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ MIT License
~
~ Copyright (c) 2022 Qingtian Wang
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
~
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-jul</artifactId>
<version>5.0.0</version>
<version>5.0.1</version>

<packaging>jar</packaging>
<name>elf4j-jul</name>
Expand Down Expand Up @@ -45,11 +70,6 @@
<artifactId>elf4j</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
59 changes: 28 additions & 31 deletions src/main/java/elf4j/jul/JulLogger.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Easy Logging Facade for Java (ELF4J)
* Copyright (c) 2022 Qingtian Wang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package elf4j.jul;
Expand All @@ -29,8 +30,8 @@
import elf4j.util.NoopLogger;
import lombok.NonNull;
import lombok.ToString;
import net.jcip.annotations.Immutable;

import javax.annotation.concurrent.Immutable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
Expand All @@ -41,12 +42,11 @@
@Immutable
@ToString
class JulLogger implements Logger {
private static final char CLOSE_BRACE = '}';
private static final Level DEFAULT_LEVEL = INFO;
private static final Class<JulLogger> SERVICE_INTERFACE_CLASS = JulLogger.class;
private static final Class<Logger> SERVICE_ACCESS_CLASS = Logger.class;
private static final EnumMap<Level, java.util.logging.Level> LEVEL_MAP = setLevelMap();
private static final EnumMap<Level, Map<String, JulLogger>> LOGGER_CACHE = initLoggerCache();
private static final char OPEN_BRACE = '{';
private final boolean enabled;
@NonNull private final Level level;
@NonNull private final String name;
@NonNull private final java.util.logging.Logger delegate;
Expand All @@ -55,21 +55,20 @@ private JulLogger(@NonNull String name, @NonNull Level level) {
this.name = name;
this.level = level;
this.delegate = java.util.logging.Logger.getLogger(name);
this.enabled = this.delegate.isLoggable(LEVEL_MAP.get(this.level));
}

static JulLogger instance() {
return getLogger(CallStack.mostRecentCallerOf(Logger.class).getClassName());
}

private static JulLogger getLogger(String name) {
return getLogger(name, DEFAULT_LEVEL);
return getLogger(CallStack.directCallerOf(SERVICE_ACCESS_CLASS).getClassName());
}

private static JulLogger getLogger(@NonNull String name, @NonNull Level level) {
return LOGGER_CACHE.get(level).computeIfAbsent(name, k -> new JulLogger(k, level));
}

private static JulLogger getLogger(String name) {
return getLogger(name, DEFAULT_LEVEL);
}

private static EnumMap<Level, Map<String, JulLogger>> initLoggerCache() {
EnumMap<Level, Map<String, JulLogger>> loggerCache = new EnumMap<>(Level.class);
EnumSet.allOf(Level.class).forEach(level -> loggerCache.put(level, new ConcurrentHashMap<>()));
Expand All @@ -84,7 +83,7 @@ private static String replaceWithJulPlaceholders(String message) {
char current = chars[i];
stringBuilder.append(current);
char next = i + 1 == chars.length ? Character.MIN_VALUE : chars[i + 1];
if (current == OPEN_BRACE && next == CLOSE_BRACE) {
if (current == '{' && next == '}') {
stringBuilder.append(placeholderIndex++);
}
}
Expand All @@ -101,14 +100,14 @@ private static EnumMap<Level, java.util.logging.Level> setLevelMap() {
return levelMap;
}

private static Object supply(Object o) {
return o instanceof Supplier<?> ? ((Supplier<?>) o).get() : o;
}

private static Object @NonNull [] supply(Object[] objects) {
return Arrays.stream(objects).map(JulLogger::supply).toArray();
}

private static Object supply(Object o) {
return o instanceof Supplier<?> ? ((Supplier<?>) o).get() : o;
}

@Override
public Logger atLevel(Level level) {
if (this.level == level) {
Expand All @@ -124,7 +123,7 @@ public Logger atLevel(Level level) {

@Override
public boolean isEnabled() {
return this.enabled;
return this.delegate.isLoggable(LEVEL_MAP.get(this.level));
}

@Override
Expand Down Expand Up @@ -179,13 +178,12 @@ public void log(Throwable t, String message, Object... args) {
delegate.log(extendedLogRecord);
}

String getName() {
@NonNull String getName() {
return name;
}

private static class CallStack {

static StackTraceElement mostRecentCallerOf(@NonNull Class<?> calleeClass) {
private static StackTraceElement directCallerOf(@NonNull Class<?> calleeClass) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String calleeClassName = calleeClass.getName();
for (int i = 0; i < stackTrace.length; i++) {
Expand All @@ -206,44 +204,43 @@ static StackTraceElement mostRecentCallerOf(@NonNull Class<?> calleeClass) {
private static class ExtendedLogRecord extends LogRecord {
private String callerClassName;
private String callerMethodName;
private boolean needToInferCaller;
private boolean retrieveCaller;

public ExtendedLogRecord(java.util.logging.Level level, String msg) {
super(level, msg);
needToInferCaller = true;
retrieveCaller = true;
}

@Override
public String getSourceClassName() {
if (needToInferCaller) {
interCaller();
if (retrieveCaller) {
retrieveCaller();
}
return callerClassName;
}

@Override
public void setSourceClassName(String sourceClassName) {
callerClassName = sourceClassName;
needToInferCaller = false;
retrieveCaller = false;
}

@Override
public String getSourceMethodName() {
if (needToInferCaller) {
interCaller();
if (retrieveCaller) {
retrieveCaller();
}
return callerMethodName;
}

@Override
public void setSourceMethodName(String sourceMethodName) {
callerMethodName = sourceMethodName;
needToInferCaller = false;
retrieveCaller = false;
}

private void interCaller() {
needToInferCaller = false;
StackTraceElement caller = CallStack.mostRecentCallerOf(JulLogger.class);
private void retrieveCaller() {
StackTraceElement caller = CallStack.directCallerOf(SERVICE_INTERFACE_CLASS);
setSourceClassName(caller.getClassName());
setSourceMethodName(caller.getMethodName());
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/elf4j/jul/JulLoggerFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Easy Logging Facade for Java (ELF4J)
* Copyright (c) 2022 Qingtian Wang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package elf4j.jul;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/services/elf4j.spi.LoggerFactory
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# Copyright (c) 2022 Easy Logging Facade for Java (ELF4J)
# Copyright (c) 2022 Qingtian Wang
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,5 +21,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#

elf4j.jul.JulLoggerFactory
3 changes: 2 additions & 1 deletion src/test/java/elf4j/jul/JulLoggerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Easy Logging Facade for Java (ELF4J)
* Copyright (c) 2022 Qingtian Wang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package elf4j.jul;
Expand Down

0 comments on commit 905a90c

Please sign in to comment.