Holger's
Java API

com.antelmann.util.logging
Class LogFile

java.lang.Object
  extended by com.antelmann.util.logging.AbstractLogWriter<String>
      extended by com.antelmann.util.logging.LogFile
All Implemented Interfaces:
Filter<LogEntry>, LogWriter, Closeable

public class LogFile
extends AbstractLogWriter<String>
implements Closeable

LogFile provides great convenience for logging information from a program into a file. By default, every call to a write method is flushed immediately, so the content of a LogFile always accurately corresponds to how far a logging program actually ran; i.e. the logging as a LogWriter is done synchronously. The object holds a write-lock on the file it writes to (through a java.io.FileWriter). IOExceptions are replaced with LogException, which should make it easier to write the code for the logging program - with the usual drawbacks of ensuring proper Exception handling. For most cases, however, this should make things easier.

Author:
Holger Antelmann
See Also:
FileLog, LogException, Logger, LogEntry, DBLineLogFormatter

Field Summary
 
Fields inherited from class com.antelmann.util.logging.AbstractLogWriter
filter
 
Fields inherited from interface com.antelmann.util.Filter
EXCLUDE_ALL_FILTER, INCLUDE_ALL_FILTER
 
Constructor Summary
LogFile(File file)
          uses the simple format
LogFile(File file, boolean useSimpleFormat)
           
LogFile(File file, LogEntryFormatter<String> formatter)
           
LogFile(File file, LogEntryFormatter<String> formatter, boolean autoFlush, boolean useDailyPattern)
          constructs a LogFile based on the given file and formatter.
LogFile(String fileName)
          This constructor simply calls LogFile(new File(fileName))
 
Method Summary
 void close()
          closes the underlying writer.
 long elapsedTime()
          returns the elapsed time since LogFile initialization or since the last call to resetTime().
protected  void finalize()
           
 void flush()
          only needed if getAlwaysFlush() returns false
 boolean getAlwaysFlush()
          true by default
 File getFile()
           
 LogEntryFormatter<String> getLogFormatter()
           
 boolean isLogAtClose()
           
 void reInitialize(File file, LogEntryFormatter<String> formatter, boolean autoFlush, boolean useDailyPattern)
          This method initializes the LogFile to a new file.
 void resetTime()
          resets the time of the embedded Stopwatch that gets initialized with the constructor
 void setAlwaysFlush(boolean flag)
           
 void setLogAtClose(boolean logAtClose)
           
 void setLogFormatter(LogEntryFormatter<String> formatter)
          formatter must not be null
 String toString()
           
 boolean usesDailyPattern()
           
protected  void write(String text)
          This is the only method actually writing to the file; all other 'write*' methods call this method
 void writeElapsedTime()
          writes a LogFile entry with the elapsed time since initialization or since the last call to resetTime().
 void writeln()
           
 void writeln(Object o)
          note that this function doesn't write serialized objects, but it calls toString() on the object and writes the String to the file
 void writeln(String text)
           
 void writeLogPattern(String pattern)
          This method writes a one-line version String of the entry into the file.
 
Methods inherited from class com.antelmann.util.logging.AbstractLogWriter
accept, getLogFilter, setLogFilter, write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LogFile

public LogFile(String fileName)
        throws IOException
This constructor simply calls LogFile(new File(fileName))

Throws:
IOException

LogFile

public LogFile(File file)
        throws IOException
uses the simple format

Throws:
IOException

LogFile

public LogFile(File file,
               boolean useSimpleFormat)
        throws IOException
Parameters:
useSimpleFormat - if true, a StringLineFormatter is used; otherwise it's a DBLineLogFormatter
Throws:
IOException

LogFile

public LogFile(File file,
               LogEntryFormatter<String> formatter)
        throws IOException
Throws:
IOException

LogFile

public LogFile(File file,
               LogEntryFormatter<String> formatter,
               boolean autoFlush,
               boolean useDailyPattern)
        throws IOException
constructs a LogFile based on the given file and formatter. if useDailyPattern is true, the

Throws:
IOException
Method Detail

reInitialize

public void reInitialize(File file,
                         LogEntryFormatter<String> formatter,
                         boolean autoFlush,
                         boolean useDailyPattern)
                  throws IOException
This method initializes the LogFile to a new file.

Throws:
IOException

usesDailyPattern

public boolean usesDailyPattern()

resetTime

public void resetTime()
resets the time of the embedded Stopwatch that gets initialized with the constructor

See Also:
Stopwatch

elapsedTime

public long elapsedTime()
returns the elapsed time since LogFile initialization or since the last call to resetTime().


writeLogPattern

public void writeLogPattern(String pattern)
                     throws LogException
This method writes a one-line version String of the entry into the file.

Specified by:
writeLogPattern in class AbstractLogWriter<String>
Throws:
LogException
See Also:
DBLineLogFormatter

getLogFormatter

public LogEntryFormatter<String> getLogFormatter()
Overrides:
getLogFormatter in class AbstractLogWriter<String>

setLogFormatter

public void setLogFormatter(LogEntryFormatter<String> formatter)
Description copied from class: AbstractLogWriter
formatter must not be null

Overrides:
setLogFormatter in class AbstractLogWriter<String>

writeElapsedTime

public void writeElapsedTime()
                      throws LogException
writes a LogFile entry with the elapsed time since initialization or since the last call to resetTime().

Throws:
LogException

writeln

public void writeln()
             throws IOException
Throws:
IOException

writeln

public void writeln(String text)
             throws IOException
Throws:
IOException

write

protected void write(String text)
              throws IOException
This is the only method actually writing to the file; all other 'write*' methods call this method

Throws:
IOException

flush

public void flush()
           throws IOException
only needed if getAlwaysFlush() returns false

Throws:
IOException

setAlwaysFlush

public void setAlwaysFlush(boolean flag)

getAlwaysFlush

public boolean getAlwaysFlush()
true by default


writeln

public void writeln(Object o)
             throws IOException
note that this function doesn't write serialized objects, but it calls toString() on the object and writes the String to the file

Throws:
IOException

close

public void close()
           throws IOException
closes the underlying writer. A note that the file was closed will be written into the log.

Specified by:
close in interface Closeable
Throws:
IOException

isLogAtClose

public boolean isLogAtClose()

setLogAtClose

public void setLogAtClose(boolean logAtClose)

getFile

public File getFile()

toString

public String toString()
Overrides:
toString in class Object

finalize

protected void finalize()
                 throws Exception
Overrides:
finalize in class Object
Throws:
Exception


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads