欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot的logging.group日志分組方法源碼流程解析

 更新時間:2023年12月04日 09:11:55   作者:codecraft  
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下springboot的logging.group

LoggersEndpoint

org/springframework/boot/actuate/logging/LoggersEndpoint.java

@Endpoint(id = "loggers")
public class LoggersEndpoint {
    private final LoggingSystem loggingSystem;
    private final LoggerGroups loggerGroups;
    /**
     * Create a new {@link LoggersEndpoint} instance.
     * @param loggingSystem the logging system to expose
     * @param loggerGroups the logger group to expose
     */
    public LoggersEndpoint(LoggingSystem loggingSystem, LoggerGroups loggerGroups) {
        Assert.notNull(loggingSystem, "LoggingSystem must not be null");
        Assert.notNull(loggerGroups, "LoggerGroups must not be null");
        this.loggingSystem = loggingSystem;
        this.loggerGroups = loggerGroups;
    }
    @ReadOperation
    public Map<String, Object> loggers() {
        Collection<LoggerConfiguration> configurations = this.loggingSystem.getLoggerConfigurations();
        if (configurations == null) {
            return Collections.emptyMap();
        }
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("levels", getLevels());
        result.put("loggers", getLoggers(configurations));
        result.put("groups", getGroups());
        return result;
    }
    private Map<String, LoggerLevels> getGroups() {
        Map<String, LoggerLevels> groups = new LinkedHashMap<>();
        this.loggerGroups.forEach((group) -> groups.put(group.getName(),
                new GroupLoggerLevels(group.getConfiguredLevel(), group.getMembers())));
        return groups;
    }
    @ReadOperation
    public LoggerLevels loggerLevels(@Selector String name) {
        Assert.notNull(name, "Name must not be null");
        LoggerGroup group = this.loggerGroups.get(name);
        if (group != null) {
            return new GroupLoggerLevels(group.getConfiguredLevel(), group.getMembers());
        }
        LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration(name);
        return (configuration != null) ? new SingleLoggerLevels(configuration) : null;
    }
    @WriteOperation
    public void configureLogLevel(@Selector String name, @Nullable LogLevel configuredLevel) {
        Assert.notNull(name, "Name must not be empty");
        LoggerGroup group = this.loggerGroups.get(name);
        if (group != null && group.hasMembers()) {
            group.configureLogLevel(configuredLevel, this.loggingSystem::setLogLevel);
            return;
        }
        this.loggingSystem.setLogLevel(name, configuredLevel);
    }
    //......
}
LoggersEndpoint提供了loggers、loggerLevels、configureLogLevel方法

LoggerGroups

org/springframework/boot/logging/LoggerGroups.java

public final class LoggerGroups implements Iterable<LoggerGroup> {
    private final Map<String, LoggerGroup> groups = new ConcurrentHashMap<>();
    public LoggerGroups() {
    }
    public LoggerGroups(Map<String, List<String>> namesAndMembers) {
        putAll(namesAndMembers);
    }
    public void putAll(Map<String, List<String>> namesAndMembers) {
        namesAndMembers.forEach(this::put);
    }
    private void put(String name, List<String> members) {
        put(new LoggerGroup(name, members));
    }
    private void put(LoggerGroup loggerGroup) {
        this.groups.put(loggerGroup.getName(), loggerGroup);
    }
    public LoggerGroup get(String name) {
        return this.groups.get(name);
    }
    @Override
    public Iterator<LoggerGroup> iterator() {
        return this.groups.values().iterator();
    }
}
LoggerGroups實現(xiàn)了Iterable接口,其泛型為LoggerGroup

LoggerGroup

org/springframework/boot/logging/LoggerGroup.java

public final class LoggerGroup {
    private final String name;
    private final List<String> members;
    private LogLevel configuredLevel;
    LoggerGroup(String name, List<String> members) {
        this.name = name;
        this.members = Collections.unmodifiableList(new ArrayList<>(members));
    }
    public String getName() {
        return this.name;
    }
    public List<String> getMembers() {
        return this.members;
    }
    public boolean hasMembers() {
        return !this.members.isEmpty();
    }
    public LogLevel getConfiguredLevel() {
        return this.configuredLevel;
    }
    public void configureLogLevel(LogLevel level, BiConsumer<String, LogLevel> configurer) {
        this.configuredLevel = level;
        this.members.forEach((name) -> configurer.accept(name, level));
    }
}
LoggerGroup定義了name、members、configuredLevel屬性,其configureLogLevel會遍歷members,通過configurer(LoggingSystem接口定義了setLogLevel方法)去變更level

LogbackLoggingSystem

org/springframework/boot/logging/logback/LogbackLoggingSystem.java

@Override
    public void setLogLevel(String loggerName, LogLevel level) {
        ch.qos.logback.classic.Logger logger = getLogger(loggerName);
        if (logger != null) {
            logger.setLevel(LEVELS.convertSystemToNative(level));
        }
    }
LogbackLoggingSystem的setLogLevel委托給了logger.setLevel

setLevel

ch/qos/logback/classic/Logger.java

public synchronized void setLevel(Level newLevel) {
        if (level == newLevel) {
            // nothing to do;
            return;
        }
        if (newLevel == null && isRootLogger()) {
            throw new IllegalArgumentException("The level of the root logger cannot be set to null");
        }

        level = newLevel;
        if (newLevel == null) {
            effectiveLevelInt = parent.effectiveLevelInt;
            newLevel = parent.getEffectiveLevel();
        } else {
            effectiveLevelInt = newLevel.levelInt;
        }

        if (childrenList != null) {
            int len = childrenList.size();
            for (int i = 0; i < len; i++) {
                Logger child = (Logger) childrenList.get(i);
                // tell child to handle parent levelInt change
                child.handleParentLevelChange(effectiveLevelInt);
            }
        }
        // inform listeners
        loggerContext.fireOnLevelChange(this, newLevel);
    }
setLevel先判斷當(dāng)前l(fā)evel是否需要變更,不需要直接返回,之后變更effectiveLevelInt為newLevel.levelInt,若該logger有childrenList,則觸發(fā)child.handleParentLevelChange(effectiveLevelInt),最后執(zhí)行l(wèi)oggerContext.fireOnLevelChange(this, newLevel)。

LoggingApplicationListener

org/springframework/boot/context/logging/LoggingApplicationListener.java

public class LoggingApplicationListener implements GenericApplicationListener {
    private static final ConfigurationPropertyName LOGGING_LEVEL = ConfigurationPropertyName.of("logging.level");
    private static final ConfigurationPropertyName LOGGING_GROUP = ConfigurationPropertyName.of("logging.group");
    private static final Bindable<Map<String, LogLevel>> STRING_LOGLEVEL_MAP = Bindable.mapOf(String.class,
            LogLevel.class);
    private static final Bindable<Map<String, List<String>>> STRING_STRINGS_MAP = Bindable
            .of(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class).asMap());
    /**
     * The default order for the LoggingApplicationListener.
     */
    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 20;
    /**
     * The name of the Spring property that contains a reference to the logging
     * configuration to load.
     */
    public static final String CONFIG_PROPERTY = "logging.config";
    /**
     * The name of the Spring property that controls the registration of a shutdown hook
     * to shut down the logging system when the JVM exits.
     * @see LoggingSystem#getShutdownHandler
     */
    public static final String REGISTER_SHUTDOWN_HOOK_PROPERTY = "logging.register-shutdown-hook";
    /**
     * The name of the {@link LoggingSystem} bean.
     */
    public static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem";
    /**
     * The name of the {@link LogFile} bean.
     * @since 2.2.0
     */
    public static final String LOG_FILE_BEAN_NAME = "springBootLogFile";
    /**
     * The name of the{@link LoggerGroups} bean.
     * @since 2.2.0
     */
    public static final String LOGGER_GROUPS_BEAN_NAME = "springBootLoggerGroups";
    private static final Map<String, List<String>> DEFAULT_GROUP_LOGGERS;
    static {
        MultiValueMap<String, String> loggers = new LinkedMultiValueMap<>();
        loggers.add("web", "org.springframework.core.codec");
        loggers.add("web", "org.springframework.http");
        loggers.add("web", "org.springframework.web");
        loggers.add("web", "org.springframework.boot.actuate.endpoint.web");
        loggers.add("web", "org.springframework.boot.web.servlet.ServletContextInitializerBeans");
        loggers.add("sql", "org.springframework.jdbc.core");
        loggers.add("sql", "org.hibernate.SQL");
        loggers.add("sql", "org.jooq.tools.LoggerListener");
        DEFAULT_GROUP_LOGGERS = Collections.unmodifiableMap(loggers);
    }
    private static final Map<LogLevel, List<String>> SPRING_BOOT_LOGGING_LOGGERS;
    static {
        MultiValueMap<LogLevel, String> loggers = new LinkedMultiValueMap<>();
        loggers.add(LogLevel.DEBUG, "sql");
        loggers.add(LogLevel.DEBUG, "web");
        loggers.add(LogLevel.DEBUG, "org.springframework.boot");
        loggers.add(LogLevel.TRACE, "org.springframework");
        loggers.add(LogLevel.TRACE, "org.apache.tomcat");
        loggers.add(LogLevel.TRACE, "org.apache.catalina");
        loggers.add(LogLevel.TRACE, "org.eclipse.jetty");
        loggers.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl");
        SPRING_BOOT_LOGGING_LOGGERS = Collections.unmodifiableMap(loggers);
    }
    //......
}
LoggingApplicationListener繼承了GenericApplicationListener,它定義了DEFAULT_GROUP_LOGGERS,默認(rèn)定義了web、sql兩個LoggerGroup

示例

{
    "levels": [
        "OFF",
        "ERROR",
        "WARN",
        "INFO",
        "DEBUG",
        "TRACE"
    ],
    "loggers": {
        "ROOT": {
            "configuredLevel": "INFO",
            "effectiveLevel": "INFO"
        },
        "Validator": {
            "configuredLevel": null,
            "effectiveLevel": "INFO"
        }
    },
    "groups": {
        "web": {
            "configuredLevel": null,
            "members": [
                "org.springframework.core.codec",
                "org.springframework.http",
                "org.springframework.web",
                "org.springframework.boot.actuate.endpoint.web",
                "org.springframework.boot.web.servlet.ServletContextInitializerBeans"
            ]
        },
        "sql": {
            "configuredLevel": null,
            "members": [
                "org.springframework.jdbc.core",
                "org.hibernate.SQL",
                "org.jooq.tools.LoggerListener"
            ]
        }
    }
}

小結(jié)

springboot的LoggersEndpoint提供了loggers、loggerLevels、configureLogLevel方法;LoggingApplicationListener繼承了GenericApplicationListener,它定義了DEFAULT_GROUP_LOGGERS,默認(rèn)定義了web、sql兩個LoggerGroup;configureLogLevel方法可以傳group名,也可以傳具體的logger名,如果是group,則會一次變更其所有members的level。

以上就是springboot的logging.group日志分組方法源碼流程解析的詳細(xì)內(nèi)容,更多關(guān)于springboot logging.group的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java如何把文件夾打成壓縮包并導(dǎo)出

    Java如何把文件夾打成壓縮包并導(dǎo)出

    這篇文章主要介紹了Java如何把文件夾打成壓縮包并導(dǎo)出,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot獲取不到用戶真實IP的解決方法

    SpringBoot獲取不到用戶真實IP的解決方法

    最近遇到個問題,項目部署后發(fā)現(xiàn)服務(wù)端無法獲取到客戶端真實的IP地址,本文就來介紹一下這個問題的解決方法,感興趣的可以了解一下
    2023-08-08
  • 詳解SpringBoot中5種類型參數(shù)傳遞和json數(shù)據(jù)傳參的操作

    詳解SpringBoot中5種類型參數(shù)傳遞和json數(shù)據(jù)傳參的操作

    當(dāng)涉及到參數(shù)傳遞時,Spring?Boot遵循HTTP協(xié)議,并支持多種參數(shù)傳遞方式,這些參數(shù)傳遞方式可以根據(jù)請求的不同部分進(jìn)行分類,
    2023-12-12
  • Java this、final等關(guān)鍵字總結(jié)

    Java this、final等關(guān)鍵字總結(jié)

    這篇文章主要對java中this、final等關(guān)鍵字進(jìn)行了總結(jié),需要的朋友可以參考下
    2017-04-04
  • Java 跳出遞歸循環(huán)問題解決辦法

    Java 跳出遞歸循環(huán)問題解決辦法

    這篇文章主要介紹了 Java 跳出遞歸循環(huán)問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Sentinel流控規(guī)則實現(xiàn)限流保護(hù)詳解

    Sentinel流控規(guī)則實現(xiàn)限流保護(hù)詳解

    這篇文章主要介紹了Sentinel流控規(guī)則實現(xiàn)限流保護(hù),Sentinel是一個分布式系統(tǒng)的流量控制組件,它可以實現(xiàn)限流,流控,降級等功能,提高系統(tǒng)的穩(wěn)定性和可靠性,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • Java中ArrayList類的使用方法

    Java中ArrayList類的使用方法

    ArrayList就是傳說中的動態(tài)數(shù)組,用MSDN中的說法,就是Array的復(fù)雜版本,下面來簡單介紹下
    2013-12-12
  • Java消息隊列中的Kafka如何保證冪等性

    Java消息隊列中的Kafka如何保證冪等性

    這篇文章主要介紹了Java消息隊列中的Kafka如何保證冪等性,Kafka是一種消息隊列,主要用來處理大量數(shù)據(jù)狀態(tài)下的消息隊列,一般用來做日志的處理,既然是消息隊列,那么Kafka也就擁有消息隊列的相應(yīng)的特性了,需要的朋友可以參考下
    2023-07-07
  • 解決創(chuàng)建springboot后啟動報錯:Failed?to?bind?properties?under‘spring.datasource‘

    解決創(chuàng)建springboot后啟動報錯:Failed?to?bind?properties?under‘spri

    在Spring?Boot項目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡潔但不支持層次結(jié)構(gòu),而yml格式支持層次性,可讀性更好,在yml文件中,要注意細(xì)節(jié),比如冒號后面需要空格
    2024-10-10
  • Java實現(xiàn)暴力匹配算法

    Java實現(xiàn)暴力匹配算法

    暴力匹配算法是一種簡單的字符串匹配算法,本文主要介紹了Java實現(xiàn)暴力匹配算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評論