2

我通过 bean 被基于 java 的 logback 配置卡住了。正如所观察到的,配置甚至没有加载。如果有人能指导我解决它,那就太好了。附上下面的代码。

公共类 JsonLogAppender{

@Bean("loggerContext")
public LoggerContext loggerContext() {
    return (LoggerContext) LoggerFactory.getILoggerFactory();
}

@Bean
@DependsOn("loggerContext")
public static Logger rootLogger(LoggerContext loggerContext) {
    Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.setLevel(Level.INFO);
    rootLogger.addAppender(consoleAppender(loggerContext, rootLogger));
    rootLogger.addAppender(tcpSocketAppender(loggerContext, rootLogger));
    return rootLogger;
}

@Bean
protected static ConsoleAppender<ILoggingEvent> consoleAppender(LoggerContext loggerContext, Logger rootLogger) {
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(loggerContext);
    consoleAppender.setName(CONSOLE_APPENDER_NAME);
    consoleAppender.setEncoder(patternLayoutEncoder(loggerContext));
    consoleAppender.start();
    return consoleAppender;
}

@DependsOn("rootLogger")
public static PatternLayoutEncoder patternLayoutEncoder(LoggerContext loggerContext) {
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(PATTERN);
    encoder.setCharset(Charset.forName(CHARSET));
    encoder.start();
    return encoder;
}

@Bean
@Primary
protected static LogstashTcpSocketAppender tcpSocketAppender(LoggerContext loggerContext, Logger rootLogger) {
    LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
    appender.setName(TCP_APPENDER_NAME);
    appender.setContext(loggerContext);
    appender.addDestination(TCP_DESTINATION);
    appender.setEncoder(compositeJsonEncoder(loggerContext, rootLogger));
    return appender;
}

@Bean
protected static LoggingEventCompositeJsonEncoder compositeJsonEncoder(LoggerContext loggerContext, Logger rootLogger){
    LoggingEventCompositeJsonEncoder encoder = new LoggingEventCompositeJsonEncoder();
    encoder.setContext(loggerContext);
    encoder.getProviders().addProvider(new MessageJsonProvider());
    encoder.getProviders().addProvider(new StackTraceJsonProvider());
    encoder.getProviders().addProvider(new LogstashMarkersJsonProvider());
    encoder.getProviders().addProvider(new ThreadNameJsonProvider());
    encoder.getProviders().addProvider(new LoggerNameJsonProvider());
    encoder.getProviders().addProvider(new LogLevelJsonProvider());
    encoder.getProviders().addProvider(new ContextJsonProvider<>());
    encoder.getProviders().addProvider(new MdcJsonProvider());
    encoder.getProviders().addProvider(withPattern(PATTERN, new LoggingEventPatternJsonProvider(), loggerContext));
    encoder.start();
    return encoder;

}

private static AbstractPatternJsonProvider<ILoggingEvent> withPattern(
        String pattern,
        AbstractPatternJsonProvider<ILoggingEvent> provider,
        LoggerContext context
        ) {

    provider.setContext(context);
    provider.setPattern(pattern);
    return provider;
}

}

我只寻找基于 java 的配置而不是基于 XML 的配置(我有使用 XML 的解决方案,它工作正常)。

更新 解决了这个问题,我错过了添加@Configuration,它现在按预期工作。

不删除线程,因为它可能有助于人们为相应的 xml 配置寻找基于 java 的配置。

4

1 回答 1

1

@Configuration 缺失,表示一个类声明了一个或多个@Bean 方法,可能会被Spring 容器处理以在运行时为这些bean 生成bean 定义和服务请求。

于 2019-01-31T20:02:53.807 回答