Class ServerLogger

java.lang.Object
com.ns.tcpframework.logger.ServerLogger
All Implemented Interfaces:
Runnable

public class ServerLogger extends Object implements Runnable
A singleton logger implementation that processes log messages asynchronously.

The ServerLogger runs on a separate virtual thread and processes log entries from a queue. It can send logs to the server console, to connected clients via SSE (Server-Sent Events), or to both destinations based on the log's configuration.

This class implements the Runnable interface and processes logs in a non-blocking manner using a BlockingQueue.

  • Method Details

    • initialize

      public static void initialize(SSEHandler sseHandler, Loglevel loglevel)
      Initializes the singleton ServerLogger instance and starts its processing thread.

      This method should be called once during application startup. If called multiple times, subsequent calls will be ignored.

      Parameters:
      sseHandler - The SSE handler for broadcasting logs to connected clients.
      loglevel - The minimum log level for server console output.
    • getInstance

      public static ServerLogger getInstance()
      Returns the singleton instance of the ServerLogger.
      Returns:
      The ServerLogger instance.
      Throws:
      IllegalStateException - if the logger has not been initialized via initialize(SSEHandler, Loglevel).
    • setSseHandler

      public void setSseHandler(SSEHandler sseHandler)
      Sets or updates the SSE handler for broadcasting logs to clients.

      This method can be used to update the SSE handler after initialization if needed, for example, when the handler is created or becomes available later in the application lifecycle.

      Parameters:
      sseHandler - The new SSE handler instance.
    • setLogLevel

      public void setLogLevel(Loglevel loglevel)
      Updates the minimum log level for server console output.

      Only log messages with a level equal to or higher than this level will be printed to the server console. This does not affect logs sent to clients via SSE.

      Parameters:
      loglevel - The new minimum log level.
    • run

      public void run()
      Main processing loop for the logger thread.

      This method runs continuously in a virtual thread, taking log entries from the queue and processing them. It blocks when the queue is empty and waits for new log entries. The loop continues until shutdown() is called or the thread is interrupted.

      Specified by:
      run in interface Runnable
    • log

      public void log(Loglevel logLevel, String message, LogDestination destination)
      Adds a log entry to the processing queue.

      This method is non-blocking and adds the log to the queue for asynchronous processing. The log will be processed by the logger thread and sent to the appropriate destination(s).

      Parameters:
      logLevel - The severity level of the log message.
      message - The content of the log message.
      destination - The destination(s) where the log should be sent (SERVER, CLIENT, or EVERYWHERE).
    • shutdown

      public void shutdown()
      Gracefully shuts down the logger by stopping the processing loop.

      This method sets the running flag to false, which causes the main processing loop in run() to exit after processing the current log entry. Any remaining logs in the queue will not be processed after shutdown.