Repository navigation
System
System of SpaceLogger based on LogChannels and LogChannelProcessors.
The LogChannel class is a fundamental building block in the SpaceLogger system, responsible for handling the asynchronous logging of messages. Each LogChannel operates on its own thread, processing log messages queued for delivery.
-
Thread-Safe Logging: Uses a
ConcurrentLinkedQueueto handle log messages in a thread-safe manner. -
Asynchronous Execution: Processes messages asynchronously using a single-threaded
ExecutorService. -
Message Prioritization: Supports message prioritization with the help of the
LogPriorityenumeration. -
Resource Management: Implements the
AutoCloseableinterface to ensure graceful shutdown of resources.
-
Message Queueing: Log messages are added to a queue using the
push()method. -
Processing: The
startIfNotRunning()method ensures a worker thread processes queued messages. -
Custom Handling: The
processMsg()method is abstract, allowing customization of how each message is handled (e.g., writing to a file, sending to a remote server). -
Graceful Shutdown: The
close()method stops the worker thread and releases resources.
- Initializes the
LogChannelwith a unique name for its worker thread.
- Adds a log message to the queue.
- Starts the worker thread if it is not already running.
- Stops the worker thread gracefully.
- Shuts down the worker thread and releases resources.
- Returns a list of log messages marked with the
ALLOCpriority.
- Retrieves the current thread processing the log channel.
The LogChannel is designed to work seamlessly with the LogChannelProcessor, which manages multiple log channels in the SpaceLogger system. This allows a modular and flexible logging system where channels can handle logging in different ways (e.g., console output, file logging, or network transmission).
// Creating a custom LogChannel
public class FileLogChannel extends LogChannel {
public FileLogChannel(String channelName) {
super(channelName);
}
@Override
protected void processMsg(LogMessage<?> logMsg) {
// Custom logic for writing log messages to a file
System.out.println("Processing log: " + logMsg);
}
}
// Initializing and using the LogChannel
LogChannel channel = new FileLogChannel("FileLogger");
channel.push(new LogMessage<>("Test message", LogPriority.TRASH));
channel.close();The LogChannelProcessor class in the SpaceLogger system acts as a central manager for a collection of LogChannel instances, providing the ability to coordinate their behavior.
- A
LogChannelProcessormanages multipleLogChannelinstances. - Provides utility methods to start, close, and push messages to all associated channels.
- Designed to work as part of the larger logging framework.
-
Processor Management
- Maintains a global registry of
LogChannelProcessorinstances accessible viaUUID. - Ensures only one processor exists per
UUIDusing a thread-safeConcurrentHashMap.
- Maintains a global registry of
-
Channel Management
- Supports adding multiple
LogChannelinstances to a single processor. - Facilitates starting and closing all managed channels.
- Broadcasts log messages to all associated channels.
- Supports adding multiple
-
Global Control
- Ability to stop all processors globally through a static utility method.
- Retrieves an existing processor or creates a new one for the given
UUID. - Ensures thread-safety via
ConcurrentHashMap.
- Iterates over all active processors and stops all their associated channels.
- Starts all associated
LogChannelinstances. - Intended for initializing all channels for active use.
- Closes all associated
LogChannelinstances. - Ensures proper shutdown of channels.
- Broadcasts a log message to all associated
LogChannelinstances.
import java.util.UUID;
import io.github.cakilgan.spacelogger.io.LogChannelProcessor;
import io.github.cakilgan.spacelogger.io.LogChannel;
// Create or retrieve a processor
UUID processorId = UUID.randomUUID();
LogChannelProcessor processor = LogChannelProcessor.getOrNew(processorId);
// Add channels
LogChannel channel1 = new YourCustomLogChannelImplementation("Channel1");
LogChannel channel2 = new YourCustomLogChannelImplementation("Channel2");
processor.CHANNELS.add(channel1);
processor.CHANNELS.add(channel2);
// Start all channels
processor.startAll();
// Push a message to all channels
LogMessage<String> message = new LogMessage<>("Hello, SpaceLogger!");
processor.pushAll(message);
// Stop all channels when done
processor.closeAll();WARNING: you must close all channels before exiting main thread for a clean exit.