Last active
August 29, 2015 14:06
-
-
Save Sythelux/271fa8dfe5798d037ff2 to your computer and use it in GitHub Desktop.
Frame that can hook into java.util.logging and show the logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Dimension; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.sql.Date; | |
import java.text.SimpleDateFormat; | |
import java.util.logging.Formatter; | |
import java.util.logging.Handler; | |
import java.util.logging.LogRecord; | |
import java.util.logging.Logger; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
public class PrintFrame extends JFrame { | |
private static final long serialVersionUID = 1L; | |
JPanel jp = new JPanel(); | |
JTextArea jta = new JTextArea(); | |
Handler h = new Handler() { | |
Formatter formatter = new Formatter() { | |
SimpleDateFormat sdf = new SimpleDateFormat("[dd.MM.yyy HH:mm:ss]"); | |
@Override | |
public String format(LogRecord record) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(sdf.format(new Date(record.getMillis()))); | |
sb.append("["); | |
sb.append(record.getLevel().getName()); | |
sb.append("]"); | |
sb.append(formatMessage(record)); | |
sb.append("\n"); | |
return sb.toString(); | |
} | |
}; | |
@Override | |
public void publish(LogRecord record) { | |
if (!isLoggable(record)) return; | |
jta.append(formatter.format(record)); | |
jta.setCaretPosition(jta.getText().length() - 1);// total | |
// gecheated | |
} | |
@Override | |
public void flush() {} | |
@Override | |
public void close() throws SecurityException {} | |
}; | |
public PrintFrame() { | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setSize(new Dimension(800, 480)); | |
setLocationRelativeTo(null); | |
jta.setAutoscrolls(true); | |
jta.setEditable(false); | |
jta.setLineWrap(true); | |
jta.setWrapStyleWord(true); | |
JScrollPane pane = new JScrollPane(jta); | |
pane.setAutoscrolls(true); | |
jp.setLayout(new BorderLayout()); | |
jp.add(pane, BorderLayout.CENTER); | |
setContentPane(jp); | |
} | |
public Handler getHandler() { | |
return h; | |
} | |
public void addButton() { | |
setVisible(false); | |
JButton jb = new JButton("Wääh, Syd mach mal. Das geht nich!"); | |
jb.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
JOptionPane.showMessageDialog(null, "Vielleicht solltest du den Syd einfach im Skype sagen, dass was nicht geht."); | |
System.exit(1); | |
} | |
}); | |
jp.add(jb, BorderLayout.SOUTH); | |
setVisible(true); | |
} | |
public static void main(String[] args) { | |
Logger log = Logger.getLogger("some name"); | |
PrintFrame pf = new PrintFrame(); | |
log.addHandler(pf.getHandler()); | |
pf.setVisible(true); | |
log.info("some Message"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment