An asynchronous Java logging example that fans each log event out to console, file, and database handlers. It demonstrates a small logging API, immutable log events, bounded queues, and dedicated worker threads; file and database output are currently placeholders that print to standard output.
Provide a simple API for application code to emit DEBUG, INFO, WARN, and ERROR messages without performing all output work inline. Each event carries its level, message, logger name, and timestamp, and is dispatched independently to three handlers. The queues are bounded to limit memory use, and the application can stop accepting events and wait for workers to drain queued events during shutdown.
- Single Responsibility (SRP):
CompositeLoggerexposes the logging API and creates events;LogWorkerqueues and dispatches them; each sink class formats and emits events for one destination. - Open/Closed (OCP): The
Loggerinterface gives callers a stable logging contract. The current fan-out implementation has three concrete sinks; adding another sink currently requires updatingLogWorkerto add its queue, worker, and dispatch loop. - Interface Segregation (ISP):
Loggeris a small caller-facing contract with only the four supported severity methods. - Dependency Inversion (DIP): Callers can depend on the
Loggerabstraction rather than directly onCompositeLogger.
- Composite:
CompositeLoggeracts as one logger to its caller while forwarding each event to multiple output handlers. The handler set is currently wired directly inLogWorker, rather than represented by a common component interface. - Producer–consumer / asynchronous dispatch: Logging calls produce
LogTaskevents; separate worker threads consume per-sink queues so each destination is processed independently.
Logger— caller-facing API for the supported log levels.LogLevel— enum ofINFO,WARN,DEBUG, andERROR.LogTask— immutable record containing level, message, logger name, andInstanttimestamp.CompositeLogger— associates a logger name with calls and submits events to the shared worker.CompositeLogger.LogWorker— owns three bounded queues and one worker thread per sink; also coordinates shutdown and queue draining.ConsoleLogger,FileLogger,DbLogger— destination-specific handlers. At present, all three print labeled output to standard output; file and database persistence are not implemented.Application— example entry point that emits a message at each supported level and shuts down logging.
- Each destination has its own
ArrayBlockingQueue<LogTask>with capacity 1,000.ArrayBlockingQueuesafely transfers events between application threads and the corresponding worker thread. - Three dedicated threads poll their queues and process events independently, so a slow sink does not directly block the other sink workers.
- The shared
acceptingLogsflag is anAtomicBoolean.submitchecks it before enqueueing, and each worker checks it while deciding whether to keep processing. Shutdown atomically sets it tofalseand joins all three threads; workers continue until their queues are empty. submituses non-blockingoffercalls and currently ignores their return values. If a queue is full, that sink silently drops the event. Submission to the three queues is also not atomic, so an event can reach some sinks and be dropped by another.- The worker is shared statically across
CompositeLoggerinstances and should be shut down once, after logging is finished. Calls made after shutdown are not rejected explicitly.
- Java 21 or later
- Maven 3.8 or later
From the project root:
mvn clean package
java -cp target/classes com.rsr007.loghandler.ApplicationThe sample application emits one message at each supported level: INFO, WARN, DEBUG, and ERROR.
Create a CompositeLogger with the class that is producing the messages, then call the desired level method:
import com.rsr007.loghandler.core.Logger;
import com.rsr007.loghandler.impl.CompositeLogger;
public class Service {
private static final Logger logger = new CompositeLogger(Service.class);
public void process() {
logger.info("Processing started");
logger.warn("Using fallback configuration");
logger.debug("Detailed diagnostic information");
logger.error("Processing failed");
// Call once when the application is finished logging.
CompositeLogger.shutdown();
}
}src/main/java/com/rsr007/loghandler/
├── Application.java
├── core/ # Logger API, levels, and log event record
└── impl/ # Composite logger, worker, and output handlers
- Group:
me.rsr007.loghandler - Artifact:
log-handler - Version:
1.0.0