我通过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的配置的人们提供帮助。
答案 0 :(得分:1)
@缺少配置, 指示一个类声明了一个或多个@Bean方法,并且可以由Spring容器进行处理,以在运行时为这些bean生成bean定义和服务请求。