Class ZLog
public class ZLog extends Object
The class provides a framework for a logging mechanism that includes the following features:
- Management an hierarchy of log folders and files under a root log folder.
- Automatic creation of log folders for every calendar day and log files for every hour of the day.
- Optional appending of timestamps and headers to each log line.
- Various log methods with and without formatting.
For details about the framework logging system see Spiderwiz Logging System.
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description void
cleanup()
Flushes and closes the current log file.void
commit(Date time, String header, String line, boolean keepSame)
Writes a line to the current log file.void
flush()
Flushes the current log file.String
getRootPath()
Returns the root pathname assigned to the logger.void
init(String label)
Initializes the logger.ZLog
log(String line, boolean keepSame)
Logs one line in a log file with the current time as a timestamp.ZLog
logEvent(String fmt, Object... args)
Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp, flushes the file and also writes the line to the console.ZLog
logException(Throwable ex)
CallslogEvent()
to log an event with the given exception as text, then follows it in the file and on the console by the stack trace of the exception.ZLog
logf(String fmt, Object... args)
Formats a line using the given format string and arguments and writes it to a log file with the current time as a timestamp.ZLog
logNow(String fmt, Object... args)
Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp and flushes the file.void
setRootPath(String rootPath)
Sets a new root pathname to the logger.
-
Constructor Details
-
ZLog
Constructs an appending logger.The log folder managed by the logger is determined by the given root folder name and a property taken from the given configuration file. The label of the property that defines the log sub-folder name is provided when
init()
is called.If the log folder already contains hourly files for an hour that is logged by this logger then new logs are appended to them.
- Parameters:
rootFolder
- the root folder name.config
- the configuration file containing the property that determines the log sub-folder name.
-
ZLog
Constructs an appending or overwriting logger.The log folder managed by the logger is determined by the given root folder name and a property taken from the given configuration object. The label of the property that defines the log sub-folder name is provided when
init()
is called.The
append
parameter determines whether this is an appending or overwriting logger. If the log folder already contains hourly files for an hour that is logged by this logger, then an appending logger appends to them while an overwriting logger overwrites them.- Parameters:
rootFolder
- the root folder name.config
- the configuration object containing the property that determines the log sub-folder name.append
- true for an appending logger, false for an overwriting logger.
-
-
Method Details
-
init
Initializes the logger.The
label
parameter identifies the property in the configuration file attached by the classconstructor
that specifies the log sub-folder name.- Parameters:
label
- identifies the property in the configuration file attached to this logger that specifies the log sub-folder name.
-
commit
Writes a line to the current log file.Writes the
line
string to the current log file. Iftime
is notnull
then the method:- Precedes the line written to the log file with a timestamp in the format
hh:mm:ss:SSS
whose value is specified by the parameter. - Writes the line to a file under the current log sub-folder named
am
hh orpm
hh, reflecting the given time. - If
keepSame
parameter isfalse
, then iftime
spans into a new hour since the lastcommit
creates a new file (and also a new folder if the date has changed), or appends to an existing file if a file with that names exists and this is an appending logger.
- Parameters:
time
- if not null the line is preceded by this timestamp and written to a file whose name is determined by this time.header
- if not null the timestamp (if given) is followed by this header.line
- the line to write to the file.keepSame
- if true writes to the same file as the previous commit even though the hour has changed since then.
- Precedes the line written to the log file with a timestamp in the format
-
log
Logs one line in a log file with the current time as a timestamp.- Parameters:
line
- the line to log.keepSame
- if true writes to the same file as the previous commit even though the hour has changed since then.- Returns:
- this object
- See Also:
commit()
-
logf
Formats a line using the given format string and arguments and writes it to a log file with the current time as a timestamp.- Parameters:
fmt
- the given format string.args
- the given arguments.- Returns:
- this object
- See Also:
log()
-
logNow
Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp and flushes the file.- Parameters:
fmt
- the given format string.args
- the given arguments.- Returns:
- this object
- See Also:
logf()
-
logEvent
Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp, flushes the file and also writes the line to the console.- Parameters:
fmt
- the given format string.args
- the given arguments.- Returns:
- this object
- See Also:
logNow()
-
logException
CallslogEvent()
to log an event with the given exception as text, then follows it in the file and on the console by the stack trace of the exception.- Parameters:
ex
- the given exception.- Returns:
- this object
- See Also:
logEvent()
-
flush
public void flush()Flushes the current log file. -
cleanup
public void cleanup()Flushes and closes the current log file. -
getRootPath
Returns the root pathname assigned to the logger.A root path is assigned by the class
consructor
or bysetRootPath()
.- Returns:
- the root pathname assigned to the logger.
- See Also:
setRootPath()
-
setRootPath
Sets a new root pathname to the logger.- Parameters:
rootPath
- root pathname to set.- See Also:
getRootPath()
-