Class JulLogger

  • All Implemented Interfaces:
    JdbcLogger

    public class JulLogger
    extends Object
    implements JdbcLogger
    The JulLogger class provides an implementation of the JdbcLogger interface using the Java Util Logging (JUL) framework. It supports logging messages at different levels such as trace, debug, info, warn, and error, both with and without associated Throwable objects.

    This class also includes a static method to initialize the logger with custom configurations such as log level, log directory, log file size, and log file count. It supports logging to both the console and file system based on the provided configuration.

    Log messages include the name of the class and method from where the logging request was made, providing a clear context for the log messages. This is achieved by analyzing the stack trace to find the caller information.

    • Field Detail

      • PARENT_CLASS_PREFIX

        public static final String PARENT_CLASS_PREFIX
      • DRIVER_CLASS_PREFIX

        public static final String DRIVER_CLASS_PREFIX
      • logger

        protected Logger logger
      • isLoggerInitialized

        protected static volatile boolean isLoggerInitialized
    • Constructor Detail

      • JulLogger

        public JulLogger​(String name)
        Constructs a new JulLogger object with the specified name.
    • Method Detail

      • initLogger

        public static void initLogger​(Level level,
                                      String logDir,
                                      int logFileSizeBytes,
                                      int logFileCount)
                               throws IOException
        Initializes the logger with the specified configuration. This method is synchronized to prevent concurrent modifications to the logger configuration.
        Parameters:
        level - the log level
        logDir - the directory for log files or STDOUT for console output
        logFileSizeBytes - the maximum size of a single log file in bytes
        logFileCount - the number of log files to rotate
        Throws:
        IOException - if an I/O error occurs
      • getCaller

        protected static String[] getCaller()
        Retrieves the class name and method name of the caller that initiated the logging request. This method navigates the stack trace to find the first method outside the known logging methods, providing the context from where the log was called. This is particularly useful for including in log messages to identify the source of the log entry.

        The method uses a two-step filtering process on the stack trace:

        1. It first drops stack trace elements until it finds one whose method name is a known logging method (e.g., trace, debug, info, warn, error).
        2. Then, it continues to drop elements until it finds the first method not in the set of logging methods, which is considered the caller.
      • getLogPattern

        protected static String getLogPattern​(String logDir)
        Generates the log file pattern based on the provided log directory. If the log directory is specified as "STDOUT", logging will be directed to the console. Otherwise, it ensures the directory exists and resolves the log file path within it.