Open
Conversation
Author
|
I figured out a simpler way that requires less code if you want to get the dependency to SLF4j: package com.netflix.archaius.api.log;
import java.util.ServiceLoader;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLoggerFactory;
public interface LoggerService {
public static class InternalLoggerFactory {
/*
* Simply prefix Internal in front of LoggerFactory:
* So instead of:
* Logger logger = LoggerFactory.getLogger(MyClass.class);
* It should be:
* Logger logger = InternalLoggerFactory.getLogger(MyClass.class);
*/
public static Logger getLogger(Class<?> clazz) {
return getILoggerFactory().getLogger(clazz.getName());
}
public static ILoggerFactory getILoggerFactory() {
return LazyHolder.INSTANCE;
}
//We should not load unless we are requested to. This avoids accidental initialization. @agentgt
//See https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
private static class LazyHolder {
private static final ILoggerFactory INSTANCE = InternalLoggerFactory.createFactory();
}
private static ILoggerFactory createFactory() {
/*
* You could also use a system property or static variable for resolution as well.
* The following looks for a file located here:
* /META-INF/services/LoggerService.getPackage().getName()
* With the name of the class as a single line that implements LoggerService.
*/
ServiceLoader<LoggerService> loader = ServiceLoader.load(LoggerService.class);
for (LoggerService ls : loader) {
return ls.createLoggerFactory();
}
return new NOPLoggerFactory();
}
}
public ILoggerFactory createLoggerFactory();
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See original issue. I'm of the high opinion that at least core configuration should not require logging otherwise you can't configure logging!!!
This change adds a private logging facade for archaius. I have set the default to NoOp but one could easily write a SLF4J and have it be the default. We could also use the ServiceLoader mechanism to load up a default logging implementation (I actually helped @mattrjacobs setup something similar for Hystrix plugins).
Of course the possibly simpler and perhaps better option is to not do any logging in
archaius2-coreand fail fast and configuration to ignore errors... etc.Ignore the checked in pom file (I needed it because I'm mirroring in a corp environment with mercurial as a forked version and I can't edit the commit... yet). I will squash/edit the commit and remove it if there is interest in this addition.