强制slf4j使用logback[英] Force slf4j to use logback

本文是小编为大家收集整理的关于强制slf4j使用logback的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

无论如何是否有迫使SLF4J使用特定的记录提供商(在我的情况下为记录)?就像他们的文档中一样:

在类路径上发现了多个绑定

SLF4J API一次与一个且只有一个基础记录框架结合.如果类路径上存在多个绑定,则SLF4J会发出警告,列出这些绑定的位置. 当类路径上有多个绑定时,请选择您希望使用的一种且仅使用一种绑定,然后删除其他绑定.例如,如果您同时具有SLF4J-> simple-1.6.6.jar和slf4j-nop-1.6.6.jar在类路径上,则希望使用NOP(> no-operation)绑定,然后删除SLF4J-- Simple-1.6.6.Jar从班级路径.如果无法删除超纤维绑定,SLF4J仍将使用一个记录框架/实现绑定.从1.6.6版本开始,SLF4J将命名框架/实现类实际绑定到.

请注意SLF4J发出的警告只是一个警告.

就我而言,我有log4j.jar,slf4j-log4j12.jar,log4j-over-slf4j.jar和classpath中的所有记录罐.我知道将slf4j-log4j12.jar和log4j-over-slf4j.jar放在一起是错误的,但是我的项目很大,并且并不总是很容易找到和排除Maven依赖性.在这种情况下,SLF4J甚至没有打印任何警告,因为我们仅使用登录配置.我花了一天的时间才能理解这个罐子地狱.

我想要的是强制SLF4J通过JVM参数使用记录,因此它可以打印警告,我可以在将来排除罐子.

推荐答案

除了清理您的班级路径外,无法强迫SLF4J与给定的实现结合.

其他推荐答案

通常,您自己的代码在classPath的开头.因此,这样做的一种方法是创建自己的org.slf4j.impl.staticloggerbinder类:

package org.slf4j.impl;

import org.slf4j.ILoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;

/**
 * Force tests to use JDK14 for logging.
 */
@SuppressWarnings("UnusedDeclaration")
public class StaticLoggerBinder implements LoggerFactoryBinder {
    private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();

    public static String REQUESTED_API_VERSION = "1.6";

    public static final StaticLoggerBinder getSingleton() {
      return SINGLETON;
    }

    private StaticLoggerBinder() {
    }

    @Override
    public ILoggerFactory getLoggerFactory() {
        return new JDK14LoggerFactory();
    }

    @Override
    public String getLoggerFactoryClassStr() {
        return "org.slf4j.impl.JDK14LoggerFactory";
    }
}

其他推荐答案

我相信,如果您将记录罐放在类Path的开头(或在包含SLF4J INGH的其他罐子之前),则将是使用的.

但是,如果SLF4J支持一个指定包含IMP的JAR的JVM arg,并且在classPath中查看的Overrode会很好,因此您不必处理分类class Pather中的Jars以确保您的罐子以确保您要使用.

本文地址:https://www.itbaoku.cn/post/1574862.html

问题描述

Is there anyway to force slf4j to use specific logging provider (logback in my case)? As in their docs:

Multiple bindings were found on the class path

SLF4J API is desinged to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings. When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have both slf4j->simple-1.6.6.jar and slf4j-nop-1.6.6.jar on the class path and you wish to use the nop (>no-operation) binding, then remove slf4j-simple-1.6.6.jar from the class path. If it is not possible to remove the superflous bindings, SLF4J will still bind with one logging framework/implementation. As of version 1.6.6, SLF4J will name the framework/implementation class it is actually bound to.

NOTE The warning emitted by SLF4J is just that, a warning.

In my case i have log4j.jar, slf4j-log4j12.jar, log4j-over-slf4j.jar and all logback jars in classpath. I know that it is error to have slf4j-log4j12.jar and log4j-over-slf4j.jar together, but my project is very big, and it is not always simple to find and exclude maven dependency. In this case slf4j even did not printed any warning, because we use only logback configs. It took me a day to understand this jar hell.

All i want is to force slf4j use logback via JVM argument, for example, so it can print warnings and i can exclude jars in future.

推荐答案

Other than cleaning up your class path, there is no way to force SLF4J to bind with a given implementation.

其他推荐答案

Generally your own code is at the beginning of the classpath. Because of this, one way to do it is to create your own org.slf4j.impl.StaticLoggerBinder class:

package org.slf4j.impl;

import org.slf4j.ILoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;

/**
 * Force tests to use JDK14 for logging.
 */
@SuppressWarnings("UnusedDeclaration")
public class StaticLoggerBinder implements LoggerFactoryBinder {
    private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();

    public static String REQUESTED_API_VERSION = "1.6";

    public static final StaticLoggerBinder getSingleton() {
      return SINGLETON;
    }

    private StaticLoggerBinder() {
    }

    @Override
    public ILoggerFactory getLoggerFactory() {
        return new JDK14LoggerFactory();
    }

    @Override
    public String getLoggerFactoryClassStr() {
        return "org.slf4j.impl.JDK14LoggerFactory";
    }
}

其他推荐答案

I believe if you put the logback JAR at the start of your classpath (or before the other JARs containing an slf4j impl), it'll be the one that is used.

However, it would be nice if slf4j supported a JVM arg that specified the JAR containing the impl, and overrode looking in the classpath, so you wouldn't have to deal with sorting the JARs in the classpath to ensure the one you want is used.