Class JulLogger

java.lang.Object
com.databricks.jdbc.log.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 Details

    • STDOUT

      public static final String STDOUT
      See Also:
    • PARENT_CLASS_PREFIX

      public static final String PARENT_CLASS_PREFIX
    • DRIVER_CLASS_PREFIX

      public static final String DRIVER_CLASS_PREFIX
    • DATABRICKS_LOG_FILE

      public static final String DATABRICKS_LOG_FILE
      See Also:
    • JAVA_UTIL_LOGGING_CONFIG_FILE

      public static final String JAVA_UTIL_LOGGING_CONFIG_FILE
      See Also:
    • logger

      protected Logger logger
    • isLoggerInitialized

      protected static volatile boolean isLoggerInitialized
  • Constructor Details

    • JulLogger

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

    • trace

      public void trace(String message)
      Specified by:
      trace in interface JdbcLogger
    • trace

      public void trace(String format, Object... arguments)
      Specified by:
      trace in interface JdbcLogger
    • debug

      public void debug(String message)
      Specified by:
      debug in interface JdbcLogger
    • debug

      public void debug(String format, Object... arguments)
      Specified by:
      debug in interface JdbcLogger
    • info

      public void info(String message)
      Specified by:
      info in interface JdbcLogger
    • info

      public void info(String format, Object... arguments)
      Specified by:
      info in interface JdbcLogger
    • warn

      public void warn(String message)
      Specified by:
      warn in interface JdbcLogger
    • warn

      public void warn(String format, Object... arguments)
      Specified by:
      warn in interface JdbcLogger
    • error

      public void error(String message)
      Specified by:
      error in interface JdbcLogger
    • error

      public void error(String format, Object... arguments)
      Specified by:
      error in interface JdbcLogger
    • error

      public void error(Throwable throwable, String message)
      Specified by:
      error in interface JdbcLogger
    • error

      public void error(Throwable throwable, String format, Object... arguments)
      Specified by:
      error in interface JdbcLogger
    • 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.