Created
February 16, 2018 00:46
-
-
Save gb96/3e1c4541b2e1c7bdb36104e67a967f8f to your computer and use it in GitHub Desktop.
Example of using java.util.logging (JUL) to write application log messages to a Swing JTextArea component
This file contains 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.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.text.MessageFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.logging.Handler; | |
import java.util.logging.Level; | |
import java.util.logging.LogRecord; | |
import java.util.logging.Logger; | |
import javax.swing.BorderFactory; | |
import javax.swing.Box; | |
import javax.swing.BoxLayout; | |
import javax.swing.JCheckBox; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.SwingUtilities; | |
public class EventLog { | |
final JPanel logPanel = new JPanel(new BorderLayout()); | |
private final JTextArea outputArea = new JTextArea(10, 10); | |
private final HashMap<Level, Boolean> enabledLevels = new HashMap<Level, Boolean>(); | |
private final JCheckBox finestBox = new JCheckBox("FINEST", false); | |
private final JCheckBox finerBox = new JCheckBox("FINER", false); | |
private final JCheckBox fineBox = new JCheckBox("FINE", false); | |
private final JCheckBox infoBox = new JCheckBox("INFO", true); | |
private final JCheckBox warningBox = new JCheckBox("WARNING", false); | |
private final JCheckBox severeBox = new JCheckBox("SEVERE", false); | |
private final JCheckBox autoscroll = new JCheckBox("Auto-scroll", true); | |
private final SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss.SSS"); | |
private final Handler LogPanelHandler = new Handler() { | |
@Override | |
public void publish(final LogRecord record) { | |
if (enabledLevels.get(record.getLevel()) != null && enabledLevels.get(record.getLevel())) | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
String logMessage = record.getMessage(); | |
Object[] args = record.getParameters(); | |
MessageFormat fmt = new MessageFormat(logMessage); | |
outputArea.append(timeFormatter.format(new Date(record.getMillis())) + "\t" + record.getLevel() | |
+ "\t" + fmt.format(args) + "\n"); | |
if (autoscroll.isSelected()) | |
outputArea.setCaretPosition(outputArea.getDocument().getLength() - 1); | |
} | |
}); | |
} | |
@Override | |
public void flush() { | |
} | |
@Override | |
public void close() throws SecurityException { | |
} | |
}; | |
public EventLog() { | |
initGui(); | |
initLogging(); | |
} | |
private void initGui() { | |
JPanel controlsPanel = new JPanel(); | |
controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.X_AXIS)); | |
controlsPanel.add(finestBox); | |
controlsPanel.add(finerBox); | |
controlsPanel.add(fineBox); | |
controlsPanel.add(infoBox); | |
controlsPanel.add(warningBox); | |
controlsPanel.add(severeBox); | |
controlsPanel.add(Box.createHorizontalGlue()); | |
controlsPanel.add(autoscroll); | |
finestBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.FINEST, finestBox.isSelected()); | |
} | |
}); | |
finerBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.FINER, finerBox.isSelected()); | |
} | |
}); | |
fineBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.FINE, fineBox.isSelected()); | |
} | |
}); | |
infoBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.INFO, infoBox.isSelected()); | |
} | |
}); | |
warningBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.WARNING, warningBox.isSelected()); | |
} | |
}); | |
severeBox.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
enabledLevels.put(Level.SEVERE, severeBox.isSelected()); | |
} | |
}); | |
logPanel.add(new JScrollPane(outputArea), BorderLayout.CENTER); | |
logPanel.add(controlsPanel, BorderLayout.SOUTH); | |
logPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); | |
outputArea.setEditable(false); | |
outputArea.setMinimumSize(new Dimension(0, 1800)); | |
outputArea.setBackground(new Color(0, 32, 0)); | |
outputArea.setForeground(Color.green); | |
outputArea.setFont(new Font("monospaced", Font.PLAIN, 14)); | |
outputArea.setText(""); | |
} | |
private void initLogging() { | |
enabledLevels.put(Level.FINEST, false); | |
enabledLevels.put(Level.FINER, false); | |
enabledLevels.put(Level.FINE, false); | |
enabledLevels.put(Level.INFO, false); | |
enabledLevels.put(Level.WARNING, false); | |
enabledLevels.put(Level.SEVERE, false); | |
final Logger LOGGER = Logger.getLogger("some.logger.name")); // XXX get default or root logger here? | |
LOGGER.addHandler(LogPanelHandler); | |
} | |
public JPanel getPanel() { | |
return logPanel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment