0

I want to customize logback append method so it can take some parameters from application.yaml file.(I am not going detail about the business.)

Here is my configuration class:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class)

@ConfigurationProperties(prefix = "newlogging")
public class LoggingConfig {
    private int queueSize;
}

And here is my application.yaml part:

newlogging:
    queue-size: 10

And this is my logback appender that I tried to autowired this configuration class.

@Service
@Slf4j
@DependsOn("loggingConfig")
public class LoggingServiceImpl extends AppenderBase<ILoggingEvent> implements LoggingService {
  
    @Autowired
    LoggingConfig loggingConfig;

    @Override
    public void start() {
        super.start();
    }

    @Override
    public void append(ILoggingEvent event) {
       int queueSize = loggingConfig.getQueueSize();
    }
}

When I tried to run the code and add a log, it enters into append method but it throws NullPointerException because loggingConfig is null.

Note: I tried to autowired loggingConfig in another services it works as expected.

I am not much familiar with bean order in spring but I guess spring creates logging appender before configuration class. How can I make this appender class to load after configuration class is loaded?

Thanks for any help.

Harun Acar
  • 109
  • 1
  • 13

0 Answers0