Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop logback to use maven-slf4j-provider #34

Open
rmannibucau opened this issue Jul 12, 2018 · 1 comment
Open

Drop logback to use maven-slf4j-provider #34

rmannibucau opened this issue Jul 12, 2018 · 1 comment

Comments

@rmannibucau
Copy link

rmannibucau commented Jul 12, 2018

If not done several extensions don't work with mvnsh, a better solution would be to support both depending the build but it is harder than staying aligned on mvn defaults.

Potential implementation:

  public class Slf4jLoggingSystem implements LoggingSystem {
    private static final List<Level> ALL_LEVELS = ImmutableList.of(
            Level.ALL,
            Level.TRACE,
            Level.DEBUG,
            Level.INFO,
            Level.WARN,
            Level.ERROR,
            Level.OFF
    );
    private static final List<LevelComponent> LEVELS = ALL_LEVELS.stream()
            .map(Level::name)
            .map(LevelComponentImpl::new)
            .collect(toList());
    private final ConcurrentMap<String, LoggerComponent> loggers = new ConcurrentHashMap<>();

    @Override
    public LevelComponent getLevel(final String name) {
      checkNotNull(name);
      return LEVELS.stream().filter(it -> it.getName().equalsIgnoreCase(name)).findFirst()
              .orElseThrow(() -> new IllegalArgumentException("No level " + name));
    }

    @Override
    public Collection<? extends LevelComponent> getLevels() {
      return LEVELS;
    }

    @Override
    public LoggerComponent getLogger(final String s) {
      return loggers.computeIfAbsent(s, k -> {
        final Logger logger = LoggerFactory.getLogger(k);
        return new LoggerComponentImpl(logger, findLevel(logger));
      });
    }

    private LevelComponent findLevel(final Logger logger) {
      if (logger.isTraceEnabled()) {
        return getLevel("TRACE");
      }
      if (logger.isDebugEnabled()) {
        return getLevel("DEBUG");
      }
      if (logger.isInfoEnabled()) {
        return getLevel("INFO");
      }
      if (logger.isWarnEnabled()) {
        return getLevel("WARN");
      }
      if (logger.isErrorEnabled()) {
        return getLevel("ERROR");
      }
      return getLevel("OFF");
    }

    @Override
    public Collection<String> getLoggerNames() {
      return loggers.keySet();
    }

    @Override
    public Collection<? extends LoggingComponent> getComponents() {
      return emptyList();
    }

    private static class LevelComponentImpl implements LevelComponent {
      private final String level;

      private LevelComponentImpl(final String name) {
        this.level = name.toUpperCase(ROOT);
      }

      @Override
      public String getName() {
        return level;
      }
    }

    private static class LoggerComponentImpl implements LoggerComponent {
      private final Logger logger;
      private final LevelComponent level;

      private LoggerComponentImpl(final Logger logger, final LevelComponent level) {
        this.logger = logger;
        this.level = level;
      }

      @Override
      public String getName() {
        return logger.getName();
      }

      @Override
      public LevelComponent getLevel() {
        return level;
      }

      @Override
      public void setLevel(final LevelComponent levelComponent) {
        // no-op
      }

      @Override
      public void setLevel(final String s) {
        // no-op
      }

      @Override
      public boolean isRoot() {
        return "".equals(getName()) || "ROOT".equals(getName());
      }
    }
  }
@jdillon
Copy link
Owner

jdillon commented Jul 16, 2018

Will have a look. Originally mvn didn't have any sane logging and only in recent history has it finally adopted slf4j so there are some conflicts with base-gshell and mvn here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants