0

我在 Spring Boot 2 应用程序中使用 LogBack,并且正在尝试配置控制台附加程序,但它给了我以下错误:

Description:

Parameter 1 of method consoleAppender in com.thalasoft.user.rest.config.LogWeb required a bean of type 'ch.qos.logback.classic.encoder.PatternLayoutEncoder' that could not be found.

Action:

Consider defining a bean of type 'ch.qos.logback.classic.encoder.PatternLayoutEncoder' in your configuration.

但是我确实指定了这样一个bean:

@Configuration
public class LogWeb {

    private final static String PATTERN = "%date %-5level [%thread] %logger{36} %m%n %rEx";

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

    @Bean
    public static PatternLayoutEncoder encoder (LoggerContext ctx) {
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(ctx);
        encoder.setPattern(PATTERN);
        return encoder;
    }

    @Bean (initMethod = "start", destroyMethod = "stop")
    public static ConsoleAppender<ILoggingEvent> consoleAppender (LoggerContext loggerContext, PatternLayoutEncoder encoder) {
        ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>();
        consoleAppender.setContext(loggerContext);
        consoleAppender.setEncoder(encoder);
        return consoleAppender;
    }

    @Bean (initMethod = "start", destroyMethod = "stop")
    public static FileAppender<ILoggingEvent> fileAppender(LoggerContext loggerContext, PatternLayoutEncoder encoder) throws IOException {
        RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
        fileAppender.setContext(loggerContext);
        fileAppender.setEncoder(encoder);
        fileAppender.setFile("build.log");
        return fileAppender;
    }

    @Bean
    public Logger registerApplicationLogger(LoggerContext loggerContext, ConsoleAppender<ILoggingEvent> consoleAppender, FileAppender<ILoggingEvent> fileAppender) throws IOException {
        Logger logger = loggerContext.getLogger("com.thalasoft.user.rest");
        logger.setLevel(Level.DEBUG);
        logger.addAppender(consoleAppender);
        logger.addAppender(fileAppender);
        return logger;
    }

    @Bean
    public Logger registerSpringLogger(LoggerContext loggerContext, ConsoleAppender<ILoggingEvent> consoleAppender, FileAppender<ILoggingEvent> fileAppender) throws IOException {
        Logger logger = loggerContext.getLogger("org.springframework");
        logger.setLevel(Level.DEBUG);
        logger.addAppender(consoleAppender);
        logger.addAppender(fileAppender);
        return logger;
    }

}

我正在使用Spring Boot 2.0.3.

4

1 回答 1

0

命名的方法是问题所在。将方法重命名为patternLayoutEncoderas in public PatternLayoutEncoder patternLayoutEncoder(LoggerContext ctx) {bean后发现并加载正常。

于 2018-08-02T15:26:40.403 回答