A smart Logback appender that dynamically displays logs based on request completion status. When a request completes successfully, only INFO-level logs are shown. When errors occur, all logs (DEBUG, INFO, WARN, ERROR) are displayed to aid in debugging.
Since this library is not yet published to Maven Central, install it locally first:
# clone github repo
cd conditional-buffer-appender
mvn clean installAdd to your pom.xml:
<dependency>
<groupId>com.mork.cookie</groupId>
<artifactId>conditional-buffer-appender</artifactId>
<version>1.0.0</version>
</dependency>Add to your logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONDITIONAL_BUFFER" class="com.mork.cookie.logback.ConditionalBufferAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<maxBufferSize>1000</maxBufferSize>
<bufferTimeoutMinutes>10</bufferTimeoutMinutes>
<cleanupIntervalMinutes>5</cleanupIntervalMinutes>
</appender>
<root level="DEBUG">
<appender-ref ref="CONDITIONAL_BUFFER"/>
</root>
</configuration>Add to your main application class or configuration:
@Import(ConditionalLoggingConfiguration.class)
@SpringBootApplication
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}For Micronaut applications, the configuration is automatically discovered. Just ensure the library is on the classpath and the configuration will be loaded automatically.
@RestController
public class YourController {
private static final ConditionalLogger logger = new ConditionalLogger(YourController.class);
@GetMapping("/api/example")
public String example() {
logger.debug("Starting request processing");
logger.info("Processing API request");
try {
// Your business logic
processBusinessLogic();
logger.info("Request completed successfully");
return "Success";
} catch (Exception e) {
logger.error("Error occurred: {}", e.getMessage(), e);
return "Error";
}
}
}@Controller
public class YourController {
private static final ConditionalLogger logger = new ConditionalLogger(YourController.class);
@Get("/api/example")
public String example() {
logger.debug("Starting request processing");
logger.info("Processing API request");
try {
// Your business logic
processBusinessLogic();
logger.info("Request completed successfully");
return "Success";
} catch (Exception e) {
logger.error("Error occurred: {}", e.getMessage(), e);
return "Error";
}
}
}- Request Start: Filter generates unique request ID and sets up logging context
- During Processing: All log events are buffered per request
- Error Detection: Any ERROR-level log marks the request as failed
- Request End:
- Success: Only INFO logs are displayed
- Error: All buffered logs are displayed
ConditionalBufferAppender: Main Logback appender that buffers and conditionally displays logsConditionalLogger: Drop-in replacement for SLF4J Logger with same APIRequestLoggingFilter: Servlet filter that manages request lifecycle (Spring Boot)micronaut.RequestLoggingFilter: HTTP filter that manages request lifecycle (Micronaut)RequestLoggingContext: Thread-local context for request ID and error statespring.ConditionalLoggingConfiguration: Spring Boot auto-configurationmicronaut.ConditionalLoggingConfiguration: Micronaut auto-configuration
| Property | Default | Description |
|---|---|---|
maxBufferSize |
1000 | Maximum log events per request buffer |
bufferTimeoutMinutes |
10 | Buffer cleanup timeout in minutes |
cleanupIntervalMinutes |
5 | Cleanup task interval in minutes |
Apache License 2.0