package helpers;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import javax.swing.JTextArea;
/**
* MyCustomHandler outputs contents to a specified file
*/
public class TextBoxLogHandler extends Handler {
JTextArea area;
ArrayList<String> messages = new ArrayList<String>();
public TextBoxLogHandler(JTextArea textboxtolog) {
super();
area = textboxtolog;
}
/* (non-API documentation)
* @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
*/
public synchronized void publish(LogRecord record) {
// ensure that this log record should be logged by this Handler
if (!isLoggable(record))
return;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
dateFormat.format(date);
// Output the formatted data to the file
Formatter f = getFormatter();
messages.add(f.format(record));
if (messages.size() > 20)
{
messages.remove(0);
}
String s ="";
for (String mes : messages)
{
s+=mes;
}
area.setText(s);
}
/* (non-API documentation)
* @see java.util.logging.Handler#flush()
*/
public void flush() {
}
/* (non-API documentation)
* @see java.util.logging.Handler#close()
*/
public void close() throws SecurityException {
}
}