Skip to content
Çakılgan edited this page Dec 2, 2024 · 3 revisions

System of SpaceLogger based on LogChannels and LogChannelProcessors.

LogChannel - Core Component of SpaceLogger

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.

Key Features

  • Thread-Safe Logging: Uses a ConcurrentLinkedQueue to 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 LogPriority enumeration.
  • Resource Management: Implements the AutoCloseable interface to ensure graceful shutdown of resources.

How It Works

  1. Message Queueing: Log messages are added to a queue using the push() method.
  2. Processing: The startIfNotRunning() method ensures a worker thread processes queued messages.
  3. 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).
  4. Graceful Shutdown: The close() method stops the worker thread and releases resources.

Constructor

LogChannel(String channelName)

  • Initializes the LogChannel with a unique name for its worker thread.

Methods

push(LogMessage<?> msg)

  • Adds a log message to the queue.

startIfNotRunning()

  • Starts the worker thread if it is not already running.

stop()

  • Stops the worker thread gracefully.

close()

  • Shuts down the worker thread and releases resources.

getAllocated()

  • Returns a list of log messages marked with the ALLOC priority.

getCurrentThread()

  • Retrieves the current thread processing the log channel.

Integration with SpaceLogger

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).

Example Usage

// 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();

LogChannelProcessor

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.

Overview

  • A LogChannelProcessor manages multiple LogChannel instances.
  • Provides utility methods to start, close, and push messages to all associated channels.
  • Designed to work as part of the larger logging framework.

Features

  1. Processor Management

    • Maintains a global registry of LogChannelProcessor instances accessible via UUID.
    • Ensures only one processor exists per UUID using a thread-safe ConcurrentHashMap.
  2. Channel Management

    • Supports adding multiple LogChannel instances to a single processor.
    • Facilitates starting and closing all managed channels.
    • Broadcasts log messages to all associated channels.
  3. Global Control

    • Ability to stop all processors globally through a static utility method.

Methods

Static Methods

LogChannelProcessor getOrNew(UUID uuid)

  • Retrieves an existing processor or creates a new one for the given UUID.
  • Ensures thread-safety via ConcurrentHashMap.

void forceAllProcessorsToStop()

  • Iterates over all active processors and stops all their associated channels.

Instance Methods

void startAll()

  • Starts all associated LogChannel instances.
  • Intended for initializing all channels for active use.

void closeAll()

  • Closes all associated LogChannel instances.
  • Ensures proper shutdown of channels.

void pushAll(LogMessage<?> msg)

  • Broadcasts a log message to all associated LogChannel instances.

Usage Example

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.