Module de.carne

Class LogBuffer


  • public class LogBuffer
    extends java.util.logging.Handler
    Handler implementation used to add/remove Handler instances programmatically during application runtime. This class keeps a buffer of published LogRecords to make them available to added Handler instances (e.g. to display log messages issued during application startup in UI).
    • Constructor Summary

      Constructors 
      Constructor Description
      LogBuffer()
      Constructs a new LogBuffer instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void addHandler​(Log log, java.util.logging.Handler handler, boolean republishBuffer)
      Adds a Handler to this LogBuffer instance for LogRecord consuming.
      void addHandler​(java.util.logging.Handler handler, boolean republishBuffer)
      Adds a Handler to this LogBuffer instance for LogRecord consuming.
      static void addHandler​(java.util.logging.Logger logger, java.util.logging.Handler handler, boolean republishBuffer)
      Adds a Handler to this LogBuffer instance for LogRecord consuming.
      void close()  
      static void exportTo​(Log log, java.io.File file, boolean append)
      Exports the buffered LogRecords to a File.
      void exportTo​(java.io.File file, boolean append)
      Exports the buffered LogRecords to a File.
      static void exportTo​(java.util.logging.Logger logger, java.io.File file, boolean append)
      Exports the buffered LogRecords to a File.
      void flush()  
      static void flush​(Log log)
      Performs a flush operation on this LogBuffer instance.
      static void flush​(java.util.logging.Logger logger)
      Performs a flush operation on this LogBuffer instance.
      static @Nullable LogBuffer get​(Log log)
      Gets the LogBuffer attached to the submitted Log (if any).
      static @Nullable LogBuffer get​(java.util.logging.Logger logger)
      Gets the LogBuffer attached to the submitted Logger (if any).
      static <T extends java.util.logging.Handler>
      @Nullable T
      getHandler​(Log log, java.lang.Class<T> handlerType)
      Gets a previously registered Handler of a specific type.
      <T extends java.util.logging.Handler>
      @Nullable T
      getHandler​(java.lang.Class<T> handlerType)
      Gets a previously registered Handler of a specific type.
      static <T extends java.util.logging.Handler>
      @Nullable T
      getHandler​(java.util.logging.Logger logger, java.lang.Class<T> handlerType)
      Gets a previously registered Handler of a specific type.
      void publish​(@Nullable java.util.logging.LogRecord logRecord)  
      static void removeHandler​(Log log, java.util.logging.Handler handler)
      Removes a previously added Handler from this LogBuffer instance.
      void removeHandler​(java.util.logging.Handler handler)
      Removes a previously added Handler from this LogBuffer instance.
      static void removeHandler​(java.util.logging.Logger logger, java.util.logging.Handler handler)
      Removes a previously added Handler from this LogBuffer instance.
      • Methods inherited from class java.util.logging.Handler

        getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LogBuffer

        public LogBuffer()
        Constructs a new LogBuffer instance.
    • Method Detail

      • get

        public static @Nullable LogBuffer get​(Log log)
        Gets the LogBuffer attached to the submitted Log (if any).
        Parameters:
        log - the Log to get the LogBuffer for.
        Returns:
        the found LogBuffer or null if none has been configured.
      • get

        public static @Nullable LogBuffer get​(java.util.logging.Logger logger)
        Gets the LogBuffer attached to the submitted Logger (if any).
        Parameters:
        logger - the Logger to get the LogBuffer for.
        Returns:
        the found LogBuffer or null if none has been configured.
      • addHandler

        public static void addHandler​(Log log,
                                      java.util.logging.Handler handler,
                                      boolean republishBuffer)
        Adds a Handler to this LogBuffer instance for LogRecord consuming.

        If the submitted Log has no LogBuffer attached the call is ignored.

        Parameters:
        log - the Log identifying the LogBuffer to add to.
        handler - the Handler to add.
        republishBuffer - whether to republish buffered LogRecords to the Handler.
        See Also:
        get(Log), addHandler(Handler, boolean)
      • addHandler

        public static void addHandler​(java.util.logging.Logger logger,
                                      java.util.logging.Handler handler,
                                      boolean republishBuffer)
        Adds a Handler to this LogBuffer instance for LogRecord consuming.

        If the submitted Logger has no LogBuffer attached the call is ignored.

        Parameters:
        logger - the Logger identifying the LogBuffer to add to.
        handler - the Handler to add.
        republishBuffer - whether to republish buffered LogRecords to the Handler.
        See Also:
        get(Logger), addHandler(Handler, boolean)
      • addHandler

        public void addHandler​(java.util.logging.Handler handler,
                               boolean republishBuffer)
        Adds a Handler to this LogBuffer instance for LogRecord consuming.

        Any already buffered LogRecord is sent to the Handler during this operation.

        Parameters:
        handler - the Handler to add.
        republishBuffer - whether to republish buffered LogRecords to the Handler.
      • getHandler

        public static <T extends java.util.logging.Handler> @Nullable T getHandler​(Log log,
                                                                                   java.lang.Class<T> handlerType)
        Gets a previously registered Handler of a specific type. If the submitted Log has no LogBuffer attached the call is ignored.
        Type Parameters:
        T - the actual type of the requested Handler.
        Parameters:
        log - the Log identifying the LogBuffer to search.
        handlerType - the type of Handler to get.
        Returns:
        the found Handler or null.
      • getHandler

        public static <T extends java.util.logging.Handler> @Nullable T getHandler​(java.util.logging.Logger logger,
                                                                                   java.lang.Class<T> handlerType)
        Gets a previously registered Handler of a specific type. If the submitted Logger has no LogBuffer attached the call is ignored.
        Type Parameters:
        T - the actual type of the requested Handler.
        Parameters:
        logger - the Logger identifying the LogBuffer to search.
        handlerType - the type of Handler to get.
        Returns:
        the found Handler or null.
      • getHandler

        public <T extends java.util.logging.Handler> @Nullable T getHandler​(java.lang.Class<T> handlerType)
        Gets a previously registered Handler of a specific type.
        Type Parameters:
        T - the actual type of the requested Handler.
        Parameters:
        handlerType - the type of Handler to get.
        Returns:
        the found Handler or null.
      • removeHandler

        public void removeHandler​(java.util.logging.Handler handler)
        Removes a previously added Handler from this LogBuffer instance.

        After removal the Handler will no longer receive any LogRecords.

        Parameters:
        handler - the Handler to remove.
        See Also:
        addHandler(Handler, boolean)
      • exportTo

        public static void exportTo​(Log log,
                                    java.io.File file,
                                    boolean append)
                             throws java.io.IOException
        Exports the buffered LogRecords to a File.

        If the submitted Log has no LogBuffer attached the call is ignored.

        Parameters:
        log - the Log identifying the LogBuffer to export from.
        file - the File to export to.
        append - whether to append (true) in case of an existing file or not (false).
        Throws:
        java.io.IOException - if an I/O error occurs during export.
      • exportTo

        public static void exportTo​(java.util.logging.Logger logger,
                                    java.io.File file,
                                    boolean append)
                             throws java.io.IOException
        Exports the buffered LogRecords to a File.

        If the submitted Logger has no LogBuffer attached the call is ignored.

        Parameters:
        logger - the Logger identifying the LogBuffer to export from.
        file - the File to export to.
        append - whether to append (true) in case of an existing file or not (false).
        Throws:
        java.io.IOException - if an I/O error occurs during export.
      • exportTo

        public void exportTo​(java.io.File file,
                             boolean append)
                      throws java.io.IOException
        Exports the buffered LogRecords to a File.
        Parameters:
        file - the File to export to.
        append - whether to append (true) in case of an existing file or not (false).
        Throws:
        java.io.IOException - if an I/O error occurs during export.
      • flush

        public static void flush​(Log log)
        Performs a flush operation on this LogBuffer instance.

        The flush request is forwarded to any currently registered Handler and any buffered LogRecord will be discarded. If the submitted Logger has no LogBuffer attached the call is ignored.

        Parameters:
        log - The Log identifying the LogBuffer to flush.
        See Also:
        get(Log)
      • flush

        public static void flush​(java.util.logging.Logger logger)
        Performs a flush operation on this LogBuffer instance.

        The flush request is forwarded to any currently registered Handler and any buffered LogRecord will be discarded. If the submitted Logger has no LogBuffer attached the call is ignored.

        Parameters:
        logger - the Logger identifying the LogBuffer to flush.
        See Also:
        get(Logger)
      • publish

        public void publish​(@Nullable java.util.logging.LogRecord logRecord)
        Specified by:
        publish in class java.util.logging.Handler
      • flush

        public void flush()
        Specified by:
        flush in class java.util.logging.Handler
      • close

        public void close()
        Specified by:
        close in class java.util.logging.Handler