org.abstracthorizon.danube.support.logging.util
Class LogFileRotatorImpl

java.lang.Object
  extended by org.abstracthorizon.danube.support.logging.util.LogFileRotatorImpl
All Implemented Interfaces:
LogFileRotator

public class LogFileRotatorImpl
extends Object
implements LogFileRotator

Simple implementation of LogFileRotator interface. This implementation knows how to keep certain number of generations of log files, to rotate logs when size exceeds or age of file (age is calculated since file is created by an instance of this class) or at one predefined moment during the day.

Each time this class is instantiated log rotation is enforced. That is done in the lazy manner - first call to logFile() method will do so.

Author:
Daniel Sendula

Field Summary
protected  OutputStream cachedOutputStream
          Cached output stream
protected  long lastAccessed
          When is this object lass accessed through logFile() method.
protected  File logFile
          Log file
protected  long logFileCreated
          When is log file created
protected  org.slf4j.Logger logger
          Logger
protected  File logPath
          Log file path
protected  long nextRotate
          When is scheduled next rotation based on the time of the day or 0 if none
 
Constructor Summary
LogFileRotatorImpl()
          Constructor
LogFileRotatorImpl(File logFile)
          Constructor
 
Method Summary
protected  void check()
          Checks if file needs to be rotated
protected  File createFileName(int g)
          Creates a file name from given generation number.
protected  void createLogFile()
          Creates log file
 int getBufferLen()
          Returns file buffer len
 int getCheckDelayMillis()
          Returns check delay in milliseconds
 File getLogDirectory()
          Returns the direcotry of log file.
 File getLogFile()
          Returns file handle of the log file.
 String getLogFileName()
          Returns last part of log file's path
 long getMaxAge()
          Returns maximum age of the file.
 long getMaxSize()
          Returns maximum size of the file.
 int getNumberOfGenerations()
          Returns defined number of generations
 Calendar getTimeOfDay()
          Returns time of day when rotation is going to happen.
 OutputStream logFile()
          This method first checks if we have accessed it too quickly since last time according to the checkDelayMillis field.
 void rotate()
          Rotates the log files
 void setBufferLen(int bufferLen)
          Sets file buffer len
 void setCheckDelayMillis(int checkDelayMillis)
          Sets check delay in milliseconds
 void setLogDirectory(File logPath)
          Sets log directory.
 void setLogFile(File logFile)
          Sets file handle of the log file
 void setLogFileName(String name)
          Sets last part of a path of log file.
 void setMaxAge(long maxAge)
          Sets maximum age of the file
 void setMaxSize(long maxSize)
          Sets maximum size of the file
 void setNumberOfGenerations(int numberOfGeneration)
          Sets defined number of generations
 void setTimeOfDay(Calendar timeOfDay)
          Sets time of day when rotation is going to happen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.slf4j.Logger logger
Logger


cachedOutputStream

protected OutputStream cachedOutputStream
Cached output stream


logFile

protected File logFile
Log file


logPath

protected File logPath
Log file path


lastAccessed

protected long lastAccessed
When is this object lass accessed through logFile() method.


nextRotate

protected long nextRotate
When is scheduled next rotation based on the time of the day or 0 if none


logFileCreated

protected long logFileCreated
When is log file created

Constructor Detail

LogFileRotatorImpl

public LogFileRotatorImpl()
Constructor


LogFileRotatorImpl

public LogFileRotatorImpl(File logFile)
Constructor

Parameters:
logFile - log file handle
Method Detail

logFile

public OutputStream logFile()
                     throws IOException
This method first checks if we have accessed it too quickly since last time according to the checkDelayMillis field. If so check should log be rotated or not wont be done.

Specified by:
logFile in interface LogFileRotator
Returns:
output stream
Throws:
IOException - io exception

getLogFile

public File getLogFile()
Returns file handle of the log file.

Returns:
file handle of the log file

setLogFile

public void setLogFile(File logFile)
Sets file handle of the log file

Parameters:
logFile - file handle of the log file

getLogDirectory

public File getLogDirectory()
Returns the direcotry of log file. .

Returns:
the direcotry of log file
See Also:
setLogDirectory(File)

setLogDirectory

public void setLogDirectory(File logPath)
Sets log directory. This is only convenience setter to be used in conjuction with setLogFileName(String) instead of setLogFile(File). There is no need using these two (this and setLogFileName(String) in log file is set through setLogFile(File). Settin only log directory will assume log file name as "access.log". If you want to set log directory and use different name use setLogFileName(String) to set the name after you have set the directory using this method

Parameters:
logPath - log file directory

getLogFileName

public String getLogFileName()
Returns last part of log file's path

Returns:
last part of log file's path

setLogFileName

public void setLogFileName(String name)
Sets last part of a path of log file. Seet setLogDirectory(File) for more explanations.

Parameters:
name -

getMaxAge

public long getMaxAge()
Returns maximum age of the file.

Returns:
maximum age of the file
See Also:
maxAge

setMaxAge

public void setMaxAge(long maxAge)
Sets maximum age of the file

Parameters:
maxAge - maximum age of the file
See Also:
maxAge

getMaxSize

public long getMaxSize()
Returns maximum size of the file.

Returns:
maximum size of the file
See Also:
maxSize

setMaxSize

public void setMaxSize(long maxSize)
Sets maximum size of the file

Parameters:
maxAge - maximum size of the file
See Also:
maxSize

getNumberOfGenerations

public int getNumberOfGenerations()
Returns defined number of generations

Returns:
defined number of generations
See Also:
numberOfGenerations

setNumberOfGenerations

public void setNumberOfGenerations(int numberOfGeneration)
Sets defined number of generations

Parameters:
numberOfGeneration - defined number of generations
See Also:
numberOfGenerations

getTimeOfDay

public Calendar getTimeOfDay()
Returns time of day when rotation is going to happen.

Returns:
time of day when rotation is going to happen
See Also:
timeOfDay

setTimeOfDay

public void setTimeOfDay(Calendar timeOfDay)
Sets time of day when rotation is going to happen

Parameters:
timeOfDay - time of day when rotation is going to happen
See Also:
timeOfDay

getCheckDelayMillis

public int getCheckDelayMillis()
Returns check delay in milliseconds

Returns:
check delay in milliseconds
See Also:
checkDelayMillis

setCheckDelayMillis

public void setCheckDelayMillis(int checkDelayMillis)
Sets check delay in milliseconds

Parameters:
checkDelayMillis - check delay in milliseconds
See Also:
checkDelayMillis

getBufferLen

public int getBufferLen()
Returns file buffer len

Returns:
file buffer len
See Also:
bufferLen

setBufferLen

public void setBufferLen(int bufferLen)
Sets file buffer len

Parameters:
bufferLen - file buffer len
See Also:
bufferLen

check

protected void check()
              throws IOException
Checks if file needs to be rotated

Throws:
IOException - io exception

rotate

public void rotate()
            throws IOException
Rotates the log files

Specified by:
rotate in interface LogFileRotator
Throws:
IOException - io exception

createFileName

protected File createFileName(int g)
Creates a file name from given generation number. If 0 is supplied for generation number, then logFile i sreturned

Parameters:
g - generation number
Returns:
a file name from given generation number

createLogFile

protected void createLogFile()
                      throws IOException
Creates log file

Throws:
IOException - io exception


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.