找不到PatternLayoutEncoder类型的bean

时间:2018-07-31 07:13:32

标签: spring-boot logback spring-logback

我在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

1 个答案:

答案 0 :(得分:0)

命名的方法就是问题所在。在像 /* Create Data Array For Email */ $email_data = array( 'first_name'=>'John', 'last_name'=>'Doe', 'email'=>'john@doe.com', 'password'=>'temp', ); /* Send Email */ Mail::to($user->email)->send(new Welcome($email_data)); 中将方法重命名为patternLayoutEncoder之后,就发现了该bean并正确加载了该文件。