gpt4 book ai didi

基于 Java 的 Logback 配置

转载 作者:行者123 更新时间:2023-11-30 10:06:52 34 4
gpt4 key购买 nike

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

公共(public)类 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,它现在按预期工作。

Not deleteing the thread, as it may be helpul for people looking for java based configuration for the corresponding xml configuration.

最佳答案

@Configuration 丢失,指示一个类声明一个或多个@Bean 方法,并且可以由 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求。

关于基于 Java 的 Logback 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467849/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com