Skip to content

Commit

Permalink
text-logger-slf4j: some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Jun 1, 2022
1 parent 635dd77 commit 7a240bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
15 changes: 12 additions & 3 deletions text-logger-slf4j/build.gradle.kts
Expand Up @@ -5,9 +5,7 @@ plugins {
dependencies {
api(projects.adventureApi)
api(libs.slf4j)
implementation(libs.slf4j.ext) {
exclude(group = "org.slf4j", module = "slf4j-api")
}
implementation(libs.slf4j.ext)
testImplementation(libs.slf4j.simple)
}

Expand All @@ -16,3 +14,14 @@ sourceSets.main {
alternateVersions(9)
}
}

eclipse {
// Make sure slf4j doesn't end up on the module path until we are actually a module
classpath.file.whenMerged {
(this as org.gradle.plugins.ide.eclipse.model.Classpath).entries.forEach { entry ->
if (entry is org.gradle.plugins.ide.eclipse.model.Library) {
entry.entryAttributes["module"] = false
}
}
}
}
Expand Up @@ -29,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.Marker;

import static java.util.Objects.requireNonNull;

/**
* An extended type of Logger capable of logging formatted components to the console.
*
Expand All @@ -42,12 +44,14 @@ public interface ComponentLogger extends Logger {
/**
* Get a logger instance with the name of the calling class.
*
* <p>This method is caller-sensitive and should not be wrapped. See
*
* <p>This logger is produced by implementations of the {@link ComponentLoggerProvider}.</p>
*
* @return a logger with the name of the calling class
* @since 4.11.0
*/
static ComponentLogger logger() {
static @NotNull ComponentLogger logger() {
return logger(CallerClassFinder.callingClassName());
}

Expand All @@ -60,8 +64,8 @@ static ComponentLogger logger() {
* @return a logger with the provided name
* @since 4.11.0
*/
static ComponentLogger logger(final String name) {
return Handler.logger(name);
static @NotNull ComponentLogger logger(final @NotNull String name) {
return Handler.logger(requireNonNull(name, "name"));
}

/**
Expand All @@ -73,7 +77,7 @@ static ComponentLogger logger(final String name) {
* @return a logger with the name of the calling class
* @since 4.11.0
*/
static ComponentLogger logger(final Class<?> clazz) {
static @NotNull ComponentLogger logger(final @NotNull Class<?> clazz) {
return logger(clazz.getName());
}

Expand Down
Expand Up @@ -59,7 +59,7 @@ interface LoggerHelper {
* @return a plain serializer
* @since 4.11.0
*/
Function<Component, String> plainSerializer();
@NotNull Function<Component, String> plainSerializer();

/**
* Create a component logger based on one which delegates to an underlying plain {@link Logger} implementation.
Expand Down
Expand Up @@ -24,6 +24,8 @@
package net.kyori.adventure.text.logger.slf4j;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.flattener.ComponentFlattener;
Expand All @@ -48,10 +50,16 @@ static ComponentLogger logger(final String name) {
}

static final class DefaultProvider implements ComponentLoggerProvider {
private final Map<String, ComponentLogger> loggers = new ConcurrentHashMap<>();
@Override
public @NotNull ComponentLogger logger(final @NotNull LoggerHelper helper, final @NotNull String name) {
final ComponentLogger initial = this.loggers.get(name);
if (initial != null) return initial;

final Logger backing = LoggerFactory.getLogger(name);
return helper.delegating(backing, helper.plainSerializer());
final ComponentLogger created = helper.delegating(backing, helper.plainSerializer());
final ComponentLogger existing = this.loggers.putIfAbsent(name, created);
return existing == null ? created : existing;
}
}

Expand Down

0 comments on commit 7a240bb

Please sign in to comment.