8000 Nacos LoggerContext Using Slf4j LoggerFactory · Issue #9970 · alibaba/nacos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Nacos LoggerContext Using Slf4j LoggerFactory #9970

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

Closed
ruansheng8 opened this issue Feb 18, 2023 · 9 comments
Closed

Nacos LoggerContext Using Slf4j LoggerFactory #9970

ruansheng8 opened this issue Feb 18, 2023 · 9 comments
Assignees
Labels
kind/discussion Category issues related to discussion

Comments

@ruansheng8
Copy link
Contributor

Describe the bug

在使用Spring Boot 3.0 版本时 , logback-classic 版本为1.4.5 已经不再提供StaticLoggerBinder这个类 , 导致nacos-logback.xml没法正常加载.

LogbackNacosLogging.java :

    private LoggerContext loadConfigurationOnStart() {
        String location = getLocation(NACOS_LOGBACK_LOCATION);
        try {
            LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
        // 省略...
        } catch (Exception e) {
            throw new IllegalStateException("Could not initialize Logback Nacos logging from " + location, e);
        }
    }

其中LoggerContext 使用 org.slf4j.impl.StaticLoggerBinder 类进行获取 , 但是以 slf4j-api 2.x 为目标的 logback 1.3 及更高版本等后端不随 org.slf4j.impl.StaticLoggerBinder 一起提供。

https://www.slf4j.org/codes.html#StaticLoggerBinde 原文:

This warning message is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

Note that slf4j-api versions 2.0.x and later use the [ServiceLoader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html) mechanism. Backends such as logback 1.3 and later which target slf4j-api 2.x, do not ship with org.slf4j.impl.StaticLoggerBinder. If you place a logging backend which targets slf4j-api 2.0.x, you need slf4j-api-2.x.jar on the classpath. See also [relevant faq](https://www.slf4j.org/faq.html#changesInVersion18) entry.

例如Spring Boot 3.0 默认管理的logback-classic-1.2.9已经不再自带StaticLoggerBinder

这里直接基于原有的逻辑不变 , 增加一个使用LoggerFactory方式获取即可

        LoggerContext loggerContext;
        try {
            // such as logback 1.3 and later which target slf4j-api 2.x, do not ship with StaticLoggerBinder
            loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
        } catch (Exception e) {
            loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        }
@ruansheng8
Copy link
Contributor Author

Related alibaba/spring-cloud-alibaba#3133

@KomachiSion
Copy link
Collaborator

relatied with #9860

@KomachiSion KomachiSion added the kind/discussion Category issues related to discussion label Feb 20, 2023
@KomachiSion
Copy link
Collaborator

If change to new api, whether the old version can't be work?

@KomachiSion
Copy link
Collaborator

@hujun-w-2 discuss together.

@ruansheng8
Copy link
Contributor Author
ruansheng8 commented Feb 20, 2023

If change to new api, whether the old version can't be work?

Older versions are still usable, and LoggerFactory will be attempted to work only when the acquisition through StaticLoggerBinder fails. Fixed in #9971.

@KomachiSion
Copy link
Collaborator

It seems #9934 can fix and use spi to extension is better.

@ruansheng8
Copy link
Contributor Author
ruansheng8 commented Feb 22, 2023

It seems #9934 can fix and use spi to extension is better.

Okay, I will close it.

@saysea
Copy link
saysea commented Feb 23, 2023

自己打个包吗?

@KomachiSion
Copy link
Collaborator

logback-adapter will release with 2.2.1 together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/discussion Category issues related to discussion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0