package sc;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* MyCustomFormatter formats the LogRecord as follows: date level localized
* message with parameters
*/
public class Log extends Formatter
{
private static final String PROPERTIES_FILE = Harlequin.BASE_PATH + "/harlequin.properties";
private static Logger logger = null;
private static JTextArea textarea = null;
private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm ");
public Log()
{
super();
}
public String format(LogRecord record)
{
// Create a StringBuffer to contain the formatted record
// start with the date.
StringBuffer sb = new StringBuffer();
// Get the date from the LogRecord and add it to the buffer
Date date = new Date(record.getMillis());
sb.append(DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT).format(date));
sb.append(" ");
// Get the level name and add it to the buffer
sb.append(record.getLevel().getName());
sb.append(" ");
// Get the formatted message (includes localization
// and substitution of paramters) and add it to the buffer
sb.append(formatMessage(record));
sb.append("\n");
return sb.toString();
}
private static void checkInitalised()
{
if (logger != null)
return;
System.setProperty("java.util.logging.config.file", PROPERTIES_FILE);
LogManager logManager = LogManager.getLogManager();
try
{
logManager.readConfiguration();
}
catch (IOException e){}
logger = Logger.getLogger("");
textarea = new JTextArea();
textarea.setLineWrap(true);
textarea.setEditable(false);
textarea.setRows(1);
}
private static void guilog(String msg)
{
textarea.insert(sdf.format(Calendar.getInstance().getTime()) + msg + '\n', 0);
textarea.repaint();
}
public static void info(String msg)
{
checkInitalised();
guilog(msg);
logger.info(msg);
}
public static void error(String msg)
{
checkInitalised();
guilog(msg);
logger.severe(msg);
}
public static JScrollPane getComponent()
{
checkInitalised();
JScrollPane scrollpane = new JScrollPane(textarea);
return scrollpane;
}
}