Package com.databricks.jdbc.log
Class JulLogger
- java.lang.Object
-
- com.databricks.jdbc.log.JulLogger
-
- All Implemented Interfaces:
JdbcLogger
public class JulLogger extends Object implements JdbcLogger
TheJulLoggerclass provides an implementation of theJdbcLoggerinterface 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 associatedThrowableobjects.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 Summary
Fields Modifier and Type Field Description static StringDATABRICKS_LOG_FILEstatic StringDRIVER_CLASS_PREFIXprotected static booleanisLoggerInitializedstatic StringJAVA_UTIL_LOGGING_CONFIG_FILEprotected Loggerloggerstatic StringPARENT_CLASS_PREFIXstatic StringSTDOUT
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddebug(String message)voiddebug(String format, Object... arguments)voiderror(String message)voiderror(String format, Object... arguments)voiderror(Throwable throwable, String message)voiderror(Throwable throwable, String format, Object... arguments)protected static String[]getCaller()Retrieves the class name and method name of the caller that initiated the logging request.protected static StringgetLogPattern(String logDir)Generates the log file pattern based on the provided log directory.voidinfo(String message)voidinfo(String format, Object... arguments)static voidinitLogger(Level level, String logDir, int logFileSizeBytes, int logFileCount)Initializes the logger with the specified configuration.voidtrace(String message)voidtrace(String format, Object... arguments)voidwarn(String message)voidwarn(String format, Object... arguments)
-
-
-
Field Detail
-
STDOUT
public static final String STDOUT
- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
JAVA_UTIL_LOGGING_CONFIG_FILE
public static final String JAVA_UTIL_LOGGING_CONFIG_FILE
- See Also:
- Constant Field Values
-
logger
protected Logger logger
-
isLoggerInitialized
protected static volatile boolean isLoggerInitialized
-
-
Constructor Detail
-
JulLogger
public JulLogger(String name)
Constructs a newJulLoggerobject with the specified name.
-
-
Method Detail
-
trace
public void trace(String message)
- Specified by:
tracein interfaceJdbcLogger
-
trace
public void trace(String format, Object... arguments)
- Specified by:
tracein interfaceJdbcLogger
-
debug
public void debug(String message)
- Specified by:
debugin interfaceJdbcLogger
-
debug
public void debug(String format, Object... arguments)
- Specified by:
debugin interfaceJdbcLogger
-
info
public void info(String message)
- Specified by:
infoin interfaceJdbcLogger
-
info
public void info(String format, Object... arguments)
- Specified by:
infoin interfaceJdbcLogger
-
warn
public void warn(String message)
- Specified by:
warnin interfaceJdbcLogger
-
warn
public void warn(String format, Object... arguments)
- Specified by:
warnin interfaceJdbcLogger
-
error
public void error(String message)
- Specified by:
errorin interfaceJdbcLogger
-
error
public void error(String format, Object... arguments)
- Specified by:
errorin interfaceJdbcLogger
-
error
public void error(Throwable throwable, String message)
- Specified by:
errorin interfaceJdbcLogger
-
error
public void error(Throwable throwable, String format, Object... arguments)
- Specified by:
errorin interfaceJdbcLogger
-
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 levellogDir- the directory for log files orSTDOUTfor console outputlogFileSizeBytes- the maximum size of a single log file in byteslogFileCount- 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:
- 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).
- 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.
-
-