public class LoggerUtils
extends java.lang.Object
This static class provides various utility methods for writing
information to a log file using a
Logger
.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DATE_TIME_FORMAT
Date and time format for display.
|
static java.text.SimpleDateFormat |
DATE_TIME_FORMATTER |
Modifier | Constructor and Description |
---|---|
protected |
LoggerUtils()
Allow overrides but not instantiation.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getFormattedCurrentDateAndTime()
Get formatted version of current date and time.
|
static void |
logArray(Logger logger,
int level,
java.lang.String arrayLabel,
java.lang.Object[] array)
Debugging support -- Print contents of an array.
|
static void |
logByteArray(Logger logger,
int logLevelTitle,
int logLevelData,
java.lang.String arrayLabel,
byte[] bytes,
int nBytes,
int pctPrintable)
Logs an array of bytes in string or hex dump format.
|
static <K,V> void |
logMap(Logger logger,
int level,
java.lang.String mapLabel,
java.util.Map<K,V> map)
Log contents of a map (HashMap, TreeMap, etc.).
|
public static final java.lang.String DATE_TIME_FORMAT
public static final java.text.SimpleDateFormat DATE_TIME_FORMATTER
public static void logByteArray(Logger logger, int logLevelTitle, int logLevelData, java.lang.String arrayLabel, byte[] bytes, int nBytes, int pctPrintable)
logger
- The logger.logLevelTitle
- The Logger level for the title/summary.
See LoggerConstants.logLevelData
- The Logger level for the data.
See LoggerConstants.arrayLabel
- A label for the array in the log output.bytes
- The array of bytes to log.nBytes
- The number of bytes to log.pctPrintable
- The percent used to decide if the bytes are
written in plain text format or hex dump
format.
If less than "pctPrintable" of the bytes are unprintable, the bytes are logged as text. Unprintable characters are replaced by "[hex]" where "hex" is the hexademical value of the character. When "pctPrintable" or more of the characters are unprintable, the characters are logged in a side-by-side format. The left side displays the data in string format with unprintable characters replaced by a ".". The right side displays the data in hex format.
The method is adapted from code written by Carl Forde.
public static <K,V> void logMap(Logger logger, int level, java.lang.String mapLabel, java.util.Map<K,V> map)
logger
- The logger.level
- Logging level.mapLabel
- Label for map.map
- The map to log.
N.B. This method assumes both the keys and values have toString() methods.
public static void logArray(Logger logger, int level, java.lang.String arrayLabel, java.lang.Object[] array)
logger
- The logger.level
- Logging level.arrayLabel
- Label for array.array
- The array.
N.B. This method assumes the array values have toString() methods.
public static java.lang.String getFormattedCurrentDateAndTime()