Skip to content

Instantly share code, notes, and snippets.

@ALRubinger
Created February 22, 2014 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ALRubinger/9147586 to your computer and use it in GitHub Desktop.
Save ALRubinger/9147586 to your computer and use it in GitHub Desktop.
Blocking Logger implementation that should have gotten me expelled from University
/**
* @(#)LogFile.java
*
* Copyright (c) 2001 Andrew Lee Rubinger
*
* @author Andrew Lee Rubinger
* @version 1.0
*/
package com.alrubinger.util;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.Timestamp;
/**
* This class defines a custom logging system
* which may be used within the scope of an
* entire application, and is not limited to
* Servlet classes.
*/
public class LogFile
{
/**
* The PrintWriter Object used
*/
protected PrintWriter writer;
/**
* The directory in which to place the
* log file
*/
protected String directory = "logs";
/**
* The prefix of the filename
*/
protected String prefix = "debug";
/**
* The suffix of the filename
*/
protected String suffix = "log";
/**
* The DateStamp used to indicate
* the date of the file
*/
protected String dateStamp;
/**
* Default constructor
*/
public LogFile()
{
setDateStamp();
openStream(directory,prefix,suffix);
}
/**
* Constructor setting specified
* directory, prefix, and suffix
* attributes of the filename.
*/
public LogFile(String newDirectory,String newPrefix,String newSuffix)
{
// Override default values to new ones
directory = newDirectory;
prefix = newPrefix;
suffix = newSuffix;
setDateStamp();
setDirectory(newDirectory);
setPrefix(newPrefix);
setSuffix(newSuffix);
openStream(directory,prefix,suffix);
}
// Mutator Methods ---------------------|||
/**
* Sets the prefix to the specifed value
*
* @param newPrefix - The new prefix value
*/
protected void setPrefix(String newPrefix)
{
prefix = newPrefix;
}
/**
* Sets the suffix to the specified value
*
* @param newSuffix - The new suffix value
*/
protected void setSuffix(String newSuffix)
{
suffix = newSuffix;
}
/**
* Sets the directory to the specified value
*
* @param newDirectory - The new directory value
*/
protected void setDirectory(String newDirectory)
{
directory = newDirectory;
}
protected String setDateStamp()
{
dateStamp = formatCurrentDate();
return dateStamp;
}
// Accessor Methods --------------------|||
/**
* Returns the prefix
*
* @return prefix - The prefix used for log
* file names
*/
protected String getPrefix()
{
return prefix;
}
/**
* Returns the suffix
*
* @return suffix - The suffix used
* for log file names
*/
protected String getSuffix()
{
return suffix;
}
/**
* Returns the current date and time in
* format YYYY:MM:DD HH:MM:SS
*
* @return currentDateTime - The current
* date and time in format
* YYYY:MM:DD HH:MM:SS
*/
protected String formatCurrentDateTime()
{
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
String currentDateTime = currentTime.toString().substring(0,19);
return currentDateTime;
}
/**
* Returns the current date in
* format YYYY-MM-DD
*
* @return currentDateString - The Current Date
* in YYYY-MM-DD
*/
protected String formatCurrentDate()
{
return formatCurrentDateTime().substring(0,10);
}
/**
* Returns the current date in
* format HH:MM:SS
*
* @return currentDateString - The Current Time
* in HH:MM:SS
*/
protected String formatCurrentTime()
{
return formatCurrentDateTime().substring(11,19);
}
/**
* Returns the date which is currently
* being used by the LogFile. Can be
* compared to the current date
* to ensure they match up
*/
protected String getCurrentDateStamp()
{
return dateStamp;
}
/**
* Prints the specified String
* to the log file along with
* timestamp
*
* @param logEntry - The String to log
*/
public synchronized void log(String logEntry)
{
// The date has changed; go to new file
if(formatCurrentDate()!=getCurrentDateStamp())
{
closeStream();
setDateStamp();
openStream(directory,prefix,suffix);
}
// Log the message along with timestamp
writer.print(formatCurrentTime());
writer.print(" - ");
writer.println(logEntry);
}
/**
* Prints the full stack trace of
* an exception to the log
*
* @param e The Exception whose stack trace
* should be printed
*/
public synchronized void logStackTrace(Exception e)
{
e.printStackTrace(writer);
}
/**
* Attempts to close the PrintWriter
* output stream.
*/
private void closeStream()
{
if(writer==null) return;
writer.flush();
writer.close();
writer = null;
}
/**
* Attempts to open the PrintWriter
* stream to the filename set by the prefix,
* suffix, and current date
*/
private void openStream(String thisDirectory,String thisPrefix,String thisSuffix)
{
File newDirectory = new File(thisDirectory);
if(!newDirectory.isAbsolute())
{
newDirectory = new File(System.getProperty("catalina.home") +
File.separator + directory);
}
newDirectory.mkdirs();
try
{
String filePath = newDirectory.getAbsolutePath() + File.separator +
prefix + "_log_" + dateStamp + "." + suffix;
// Set the PrintWriter to append to the end of the file and auto-flush
writer = new PrintWriter(new FileWriter(filePath,true),true);
}
catch(IOException ioe)
{
writer = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment