设为首页 加入收藏

TOP

logback的使用和原理(二)
2023-07-25 21:35:41 】 浏览:64
Tags:logback
gerBinder.getSingleton().getLoggerFactory(); case 4: return NOP_FALLBACK_FACTORY; default: throw new IllegalStateException("Unreachable code"); } }

StaticLoggerBinder 类中,

private static StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
private LoggerContext defaultLoggerContext = new LoggerContext();
static {
        SINGLETON.init();
    }
void init() {
        try {
            try {
                // 自动配置,autoConfig 点进去
                (new ContextInitializer(this.defaultLoggerContext)).autoConfig();
            } catch (JoranException var2) {
                Util.report("Failed to auto configure default logger context", var2);
            }

            if (!StatusUtil.contextHasStatusListener(this.defaultLoggerContext)) {
                StatusPrinter.printInCaseOfErrorsOrWarnings(this.defaultLoggerContext);
            }

            this.contextSelectorBinder.init(this.defaultLoggerContext, KEY);
            this.initialized = true;
        } catch (Exception var3) {
            Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", var3);
        }

    }

ContextInitializer 类中,

// 此处的静态常量,表示的是logback的配置文件名
public static final String AUTOCONFIG_FILE = "logback.xml";
public static final String TEST_AUTOCONFIG_FILE = "logback-test.xml";
public static final String CONFIG_FILE_PROPERTY = "logback.configurationFile";
final LoggerContext loggerContext;

// 
public void autoConfig() throws JoranException {
        StatusListenerConfigHelper.installIfAsked(this.loggerContext);
    	// 没有任何配置文件时url为null
        URL url = this.findURLOfDefaultConfigurationFile(true);
        if (url != null) {
            // 读取自定义的配置文件,logback.xml文件配置生效
            this.configureByResource(url);
        } else {
            Configurator c = (Configurator)EnvUtil.loadFromServiceLoader(Configurator.class);
            if (c != null) {
                try {
                    c.setContext(this.loggerContext);
                    c.configure(this.loggerContext);
                } catch (Exception var4) {
                    throw new LogbackException(String.format("Failed to initialize Configurator: %s using ServiceLoader", c != null ? c.getClass().getCanonicalName() : "null"), var4);
                }
            } else {
                // 这一段代码保证了,没有任何配置文件时,也可以进行基础的自动配置,进行日志输出
                BasicConfigurator basicConfigurator = new BasicConfigurator();
                basicConfigurator.setContext(this.loggerContext);
                basicConfigurator.configure(this.loggerContext);
            }
        }

    }

3 日志滚动配置

需要注意在linux下执行的路径问题

<configuration>
    
	<!--    加入监听器,可以在监听器中设置 ${LOG_PATH}和${PROJECT_NAME} 的值,从而实现指定日志输出路径的效果 -->
    <!-- 也可以使用 ${user.dir:-.}/logs/xxxx.log, user.dir 表示执行运行jar包命令时所在的路径 -->
	<contextListener class="org.example.com.smy.CustomLogContextListener" />
    <appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/${PROJECT_NAME}/logs/LogsPathTest.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名,.gz 表示开启文件压缩-->
            <FileNamePattern>${LOG_PATH}/${PROJECT_NAME}/logs/LogsPathTest.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
            <!--日志文件保留天数-->
            <MaxHistory>15</MaxHistory>
            <!--日志文件最大的大小-->
            <MaxFileSize>2MB</MaxFileSize>
        </rollingPolicy>
        <encoder>
            <Pattern>%date{yyyy-MM-dd HH:mm:ss}\t%level\t%log
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇老者Java,奋战一线 下一篇Tomcat 入门实战(3)--Https 配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目