|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.log.LogWriter
public class LogWriter
Provides very basic data logging facilities.
import ...Tracking.Log; // Log objects fully specified errlog = new Log("logDir", "errlog", true, false); diaglog = new Log("logDir/diaglog", false, true); // a Log object with defaults log = new Log("logDir/mylog"); // scalar and list versions of write method log.write("message"); log.write(String[] {"field1","field2","field3"}); diaglog.flush(); errlog.close();Each message logged defines a series of fields and are written as a record to a text file. Records are separated by the system-dependent line terminator. Fields are separated by the value specified in the LOG_FIELD_SEPARATOR property. The first few fields of every record are added automatically. These are the date, the time, and optionally, the thread which is logging the message.
autoFlush
parameter is a boolean switch.
Turned on, every write
call will
flush immediately to the file.
Turned off, no flushing will be done. You may at any time invoke the
flush
method to do it manually, or wait until program
termination. The immediate-flush may be desirable, for instance, when
attempting to diagnose a problem where the program is long-running and/or
log messages occur infrequently.verbose
parameter is also a boolean switch.
Turned on, a standard phrase
(the value of the LOG_START_TEXT property)
is written to the log file when a LogWriter object is created.
Similarly, at the termination of the program (when close
is
called) the value of the LOG_END_TEXT property is written.
These phrases serves to demarcate session boundaries for ease
in reviewing the log file. The absence of the LOG_END_TEXT marker,
for instance, might indicate abnormal program termination.
Turned off, the demarcation phrases are not written to the log file.
Additionally, the log file is not even created until the first
write
is invoked. Thus, for an error log, for example, if no
errors occur during a program invocation, the log file will not be created
(or not updated if one already exists).
InputOptions properties used by this class |
|||
Property | Type | Default | Specifies... |
---|---|---|---|
LOG_FILE_EXT | String | .log | Extension for log file name. |
LOG_FIELD_SEPARATOR | String | | (a vertical bar) | Field separator in each record of the log file. |
LOG_START_TEXT | String | === Starting log session === | Initial text written when LogWriter object is created. |
LOG_END_TEXT | String | === Ending log session === | Final text written when LogWriter object is created. |
LOG_DATE_FMT | String | yyyy-MM-dd | Format string (per SimpleDateFormat )
for date field of each log record. |
LOG_TIME_FMT | String | HH:mm:ss | Format string (per SimpleDateFormat )
for time field of each log record. |
SHOW_THREAD | boolean | true |
Show or hide thread name with each message. |
Nested Class Summary | |
---|---|
static class |
LogWriter.Test
A standalone test class to exercise the LogWriter class. |
Field Summary | |
---|---|
static int |
DEFAULT_DATA_COLUMN
The default column index for the actual data. |
static String |
DEFAULT_LOG_FIELD_SEPARATOR
Default log file field separator. |
static String |
DEFAULT_LOG_FILE_EXT
Default log file extension. |
static String |
DEFAULT_NON_CONFLICTING_STRING
In case text to store in log contains field separator, change to this character. |
static String |
VERSION
Current version of this class. |
Constructor Summary | |
---|---|
LogWriter(PrintWriter printWriter)
Creates a LogWriter object from the specified
PrintWriter
with verbose mode active. |
|
LogWriter(PrintWriter printWriter,
boolean verbose)
Creates a LogWriter object from the specified
PrintWriter . |
|
LogWriter(String logName)
Creates a LogWriter object;
verbose mode is active and auto-flushing disabled. |
|
LogWriter(String logName,
boolean autoFlush,
boolean verbose)
Creates a LogWriter object, verifies the log directory exists
(or creates it if needed), and initializes the log file. |
|
LogWriter(String logName,
InputOptions settings)
Creates a LogWriter object,
allowing full customization via the InputOptions object. |
Method Summary | |
---|---|
void |
close()
Close the log stream. |
void |
flush()
Flush the log stream. |
void |
write(String msg)
Write a log record with a single data field. |
void |
write(String[] msgList)
Write a log record with multiple data fields. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_LOG_FILE_EXT
public static final String DEFAULT_LOG_FIELD_SEPARATOR
public static final String DEFAULT_NON_CONFLICTING_STRING
public static final int DEFAULT_DATA_COLUMN
public static final String VERSION
Constructor Detail |
---|
public LogWriter(String logName, boolean autoFlush, boolean verbose) throws IOException
LogWriter
object, verifies the log directory exists
(or creates it if needed), and initializes the log file.
If verbose
is true
, the file is initialized
with the value of the LOG_START_TEXT property.
If this property is empty, nothing is initially written to
the log file, but it is still created as an empty file.
If verbose
is false
,
the initial phrase is not written and
the file creation is delayed until the first write
.
logName
- base name of log file including path but no extensionautoFlush
- boolean indicating to flush
after every write
verbose
- boolean directing opening and closing behavior
IOException
- if there's a problem with the path or filepublic LogWriter(String logName) throws IOException
LogWriter
object;
verbose mode is active and auto-flushing disabled.
logName
- base name of log file including path but no extension
IOException
- if there's a problem with the path or filefor more
public LogWriter(String logName, InputOptions settings) throws IOException
LogWriter
object,
allowing full customization via the InputOptions object.
logName
- base name of log file including path but no extensionsettings
- InputOptions object to customize the log output
IOException
- if there's a problem with the path or filefor more
public LogWriter(PrintWriter printWriter, boolean verbose) throws IOException
LogWriter
object from the specified
PrintWriter
.
As this constructor is given an existing PrintWriter
,
the verbose flag does not control the just-in-time
file creation behavior.
printWriter
- LogWriter object to output messagesverbose
- boolean directing opening and closing behavior
IOException
- if an I/O error occursfor more
public LogWriter(PrintWriter printWriter) throws IOException
LogWriter
object from the specified
PrintWriter
with verbose mode active.
As this constructor is given an existing PrintWriter
,
the verbose flag does not control the just-in-time
file creation behavior.
printWriter
- LogWriter object to output messages
IOException
- if an I/O error occursfor more
Method Detail |
---|
public void flush()
public void close() throws IOException
IOException
- if an I/O error occurspublic void write(String msg) throws IOException
DEFAULT_LOG_FIELD_SEPARATOR
string separates fields
in each record.
(You may elect to change this field separator string
via the LOG_FIELD_SEPARATOR property.)
If your data contains the field separator you have chosen,
it will automatically be changed to
DEFAULT_NON_CONFLICTING_STRING
so as to maintain the
ability to properly distinguish fields of the record.
msg
- message to output to log file
IOException
- if there's a problem writing to the filepublic void write(String[] msgList) throws IOException
DEFAULT_LOG_FIELD_SEPARATOR
string
(unless you have changed it).
msgList
- list of messages to output to log file
IOException
- if there's a problem writing to the filewrite(String)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |