It's a good thing to do only because you'll realise why you should use one of the many and excellent logging frameworks. After you have fiddled around with this for a week, go back and use log4j.
However, until then, and to get you started, here is one I prepared earlier just because I wanted to get my head round what the complexity was. This is incomplete for your purposes and I recommend you use it just to decide what you want to do rather than base your implementation on it. It should give you a reasonable idea of what is necessary and it provides basic single threaded logging...
/*
* Created on Jun 11, 2005
*/
package com.hupa.util;
import java.io.*;
import java.util.Date;
/**
* @author simonpalmer
*/
public class MessageWriter implements ILog
{
public class e_LogLevel
{
public static final int e_log_error = 1;
public static final int e_log_warn = 2;
public static final int e_log_info = 4;
public static final int e_log_debug = 8;
}
public int m_iLogLevel = e_LogLevel.e_log_error;
public String m_strLogFile = new String();
public String m_strLogPath = new String();
public boolean m_bConsoleOut = true;
PrintStream m_ps;
public boolean m_bLogOpen = false;
private static Date dt = new Date();
/**
* Output info level message
* @param strMess
*/
public void info(String strMess)
{
if ((m_iLogLevel & e_LogLevel.e_log_info) == e_LogLevel.e_log_info)
{
dt.setTime(System.currentTimeMillis());
String strOut = dt.toString() + " inf: " + strMess;
if (m_bConsoleOut) System.out.println(strOut);
if (m_bLogOpen) m_ps.println(strOut);
}
}
public boolean bInfo(){return ((m_iLogLevel & e_LogLevel.e_log_info) == e_LogLevel.e_log_info);}
/**
* Output debug level message
* @param strMess
*/
public void debug(String strMess)
{
if ((m_iLogLevel & e_LogLevel.e_log_debug) == e_LogLevel.e_log_debug)
{
dt.setTime(System.currentTimeMillis());
String strOut = dt.toString() + " dbg: " + strMess;
if (m_bConsoleOut) System.out.println(strOut);
if (m_bLogOpen) m_ps.println(strOut);
}
}
public boolean bDebug(){return ((m_iLogLevel & e_LogLevel.e_log_debug) == e_LogLevel.e_log_debug);}
/**
* Output warning level message
* @param strMess
*/
public void warn(String strMess)
{
if ((m_iLogLevel & e_LogLevel.e_log_warn) == e_LogLevel.e_log_warn)
{
dt.setTime(System.currentTimeMillis());
String strOut = dt.toString() + " warn: " + strMess;
if (m_bConsoleOut) System.out.println(strOut);
if (m_bLogOpen) m_ps.println(strOut);
}
}
public boolean bWarn(){return ((m_iLogLevel & e_LogLevel.e_log_warn) == e_LogLevel.e_log_warn);}
/**
* Output error level message
* @param strMess
*/
public void error(String strMess)
{
if ((m_iLogLevel & e_LogLevel.e_log_error) == e_LogLevel.e_log_error)
{
dt.setTime(System.currentTimeMillis());
String strOut = dt.toString() + " err: " + strMess;
if (m_bConsoleOut) System.out.println(strOut);
if (m_bLogOpen) m_ps.println(strOut);
}
}
public boolean bError(){return ((m_iLogLevel & e_LogLevel.e_log_error) == e_LogLevel.e_log_error);}
/**
* construst the log file name
* @return String, full file path and name
*/
public String GetLogFileName()
{
return m_strLogPath + m_strLogFile + ".log";
}
/**
* Open the log file prescribed by the settings
* @return boolean, success
*/
public boolean OpenLog()
{
try
{
m_ps = new PrintStream(new FileOutputStream(GetLogFileName()));
m_bLogOpen = true;
}
catch (FileNotFoundException e)
{
// this means that the folder doesn't exist
if (MakeFolder(m_strLogPath))
{
m_bLogOpen = true;
try
{
m_ps = new PrintStream(new FileOutputStream(GetLogFileName()));
}
catch (IOException e1)
{
e.printStackTrace();
m_bLogOpen = false;
}
}
}
return m_bLogOpen;
}
public static boolean MakeFolder(String strFolder)
{
try
{
java.io.File f = new File(strFolder);
if (!f.mkdirs())
{
return false;
}
}
catch (SecurityException e)
{
e.printStackTrace();
return false;
}
return true;
}
/**
* Close the log file
* @return boolean, success
*/
public boolean CloseLog()
{
if (m_ps != null)
{
m_ps.flush();
m_ps.close();
}
m_bLogOpen = false;
return m_bLogOpen;
}
public void setConsoleOut(boolean b)
{
m_bConsoleOut = b;
}
public void setLogLevel(int i)
{
m_iLogLevel = i;
}
}
and here's the interface
public interface ILog
{
abstract public void debug(String message);
abstract public void info(String message);
abstract public void warn(String message);
abstract public void error(String message);
abstract public void setLogLevel(int i);
abstract public void setConsoleOut(boolean b);
abstract public boolean CloseLog();
abstract public boolean OpenLog();
abstract public boolean bDebug();
}