Visualiser des journaux log4j

Contenu du snippet

Cette JInternalFrame permet de visualiser les journaux et permet d'appliquer des filtres de type [erreur][info][debug].
Le code source initial provient du projet SQuireL. Principale modification : la console est réactualisé toutes les secondes.
Impératif : dans votre log, il faut que le appender est en paramètre [%t] pour permettre la reconnaissance du type d'erreur. Par exemple pour moi j'ai quelque chose comme :
log4j.appender.A1.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

Source / Exemple :


/*

  • Copyright (C) 2002-2006 Colin Bell
  • colbell@users.sourceforge.net
*
  • This library is free software; you can redistribute it and/or
  • modify it under the terms of the GNU Lesser General Public
  • License as published by the Free Software Foundation; either
  • version 2.1 of the License, or (at your option) any later version.
*
  • This library is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  • Lesser General Public License for more details.
*
  • You should have received a copy of the GNU Lesser General Public
  • License along with this library; if not, write to the Free Software
  • Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  • /
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Cursor; import java.awt.event.*; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.lang.reflect.InvocationTargetException; import javax.swing.*; import org.apache.log4j.Logger; /**
  • This sheet shows the SQuirreL log files.
*
  • @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
  • Ce n'est pas la version original..
  • /
public class ViewLogsSheet extends JInternalFrame { /** Logger for this class. */ private static Logger _log = Logger.getLogger(ViewLogsSheet.class); /** Combo box containing all the log files. */ /** Text area containing the log contents. */ private final JTextArea _logContentsTxt = new JTextArea(20, 50); /** Button that refreshes the log contents. */ private final JButton _refreshBtn = new JButton("Rafraichir"); private final JCheckBox _errorChkbox = new JCheckBox("Errors"); private final JCheckBox _debugChkbox = new JCheckBox("Debug"); private final JCheckBox _infoChkbox = new JCheckBox("Info"); /** Directory containing the log files. */ private final File _logDir; /** If <TT>true</TT> user is closing this window. */ private boolean _closing = false; /** If <TT>true</TT> log is being refreshed. */ private boolean _refreshing = false; /** Size of current log file */ private long _fileLenght; private static final long serialVersionUID = 1L; /**
*
  • @throws IllegalArgumentException
  • Thrown if a <TT>null</TT> <TT>IApplication passed.
  • /
public ViewLogsSheet() { super("Journaux", true, true, true, true); _logDir = new File("opc-client.log"); //!!! mettez ici votre fichier _fileLenght = _logDir.length(); createUserInterface(); startRefreshingLog(); new Thread(new LenghtDetect()).start(); } @Override public void dispose() { // Stop refresh if it is running. _closing = true; super.dispose(); } /**
  • Close this sheet.
  • /
private void performClose() { dispose(); } /**
  • Start a thread to refrsh the log.
  • /
private synchronized void startRefreshingLog() { if (!_refreshing) { new Thread(new Refresher()).run(); } // refreshLog(); } /**
  • Enables the log combo box and refresh button using the Swing event
  • thread.
  • /
private void enableComponents(final boolean enabled) { Runnable todo = new Runnable() { public void run() { _refreshBtn.setEnabled(enabled); } }; if (SwingUtilities.isEventDispatchThread()) { todo.run(); } else { try { SwingUtilities.invokeAndWait(todo); } catch (InterruptedException ex) { _log.error(ex); } catch (InvocationTargetException ex) { _log.error(ex); } } } /**
  • Refresh the log.
  • /
private void refreshLog() { enableComponents(false); setCursor(new Cursor(Cursor.WAIT_CURSOR)); try { _logContentsTxt.setText(""); final File logFile = _logDir; if (logFile != null) { try { if (logFile.exists() && logFile.canRead()) { final BufferedReader rdr = new BufferedReader(new FileReader(logFile)); try { String line = null; StringBuffer chunk = new StringBuffer(16384); while ((line = rdr.readLine()) != null) { if (_closing) { return; } if (chunk.length() > 16000) { final String finalLine = chunk.toString(); if (!_closing) { _logContentsTxt.append(finalLine); } chunk = new StringBuffer(16384); } else { if (shouldAppendLineToChunk(line)) { chunk.append(line).append('\n'); } } } if (_closing) { return; } final String finalLine = chunk.toString(); if (!_closing) { _logContentsTxt.append(finalLine); } } finally { rdr.close(); } } } catch (Exception ex) { // i18n[ViewLogsSheet.error.processinglogfile=Error occured processing log file] final String msg = "ViewLogsSheet.error.processinglogfile"; _log.error(msg, ex); } } else { // i18n[ViewLogsSheet.info.nulllogfile=Null log file name] _log.debug("ViewLogsSheet.info.nulllogfile"); } if (_closing) { return; } // Position to the start of the last line in log. try { int pos = Math.max(0, _logContentsTxt.getText().length() - 1); int line = _logContentsTxt.getLineOfOffset(pos); final int finalpos = _logContentsTxt.getLineStartOffset(line); _logContentsTxt.setCaretPosition(finalpos); } catch (Exception ex) { // i18n[ViewLogsSheet.error.setcaret=Error positioning caret in log text component] _log.error("ViewLogsSheet.error.setcaret", ex); } } finally { enableComponents(true); _refreshing = false; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } } /** 199828 [Foo Thread] message
  • @param line
  • @return
  • /
private boolean shouldAppendLineToChunk(String line) { boolean result = false; if (line == null || line.length() == 0) { return false; } if (_errorChkbox.isSelected() && _debugChkbox.isSelected() && _infoChkbox.isSelected()) { return true; } int threadNameEndIdx = line.indexOf("]"); if (threadNameEndIdx > -1) { if (threadNameEndIdx + 2 >= line.length()) { return false; } //Garde de fou char levelChar = line.charAt(threadNameEndIdx + 2); if (_errorChkbox.isSelected() && levelChar == 'E') { result = true; } if (_debugChkbox.isSelected() && levelChar == 'D') { result = true; } if (_infoChkbox.isSelected() && levelChar == 'I') { result = true; } if (levelChar != 'E' && levelChar != 'D' && levelChar != 'I') { result = true; } } else { result = true; } return result; } /**
  • Create user interface.
  • /
private void createUserInterface() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); // putClientProperty("JInternalFrame.isPalette", true); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); // contentPane.add(createToolBar(), BorderLayout.NORTH); contentPane.add(createMainPanel(), BorderLayout.CENTER); contentPane.add(createButtonsPanel(), BorderLayout.SOUTH); pack(); AbstractAction closeAction = new AbstractActionImpl(); KeyStroke escapeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escapeStroke, "CloseAction"); getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke, "CloseAction"); getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(escapeStroke, "CloseAction"); getRootPane().getActionMap().put("CloseAction", closeAction); } /**
  • Create the main panel containing the log details and selector.
  • /
private JPanel createMainPanel() { // File appLogFile = new ApplicationFiles().getExecutionLogFile(); final JPanel pnl = new JPanel(new BorderLayout()); _logContentsTxt.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 0)); pnl.add(new JScrollPane(_logContentsTxt), BorderLayout.CENTER); return pnl; } /**
  • Create panel at bottom containing the buttons.
  • /
private JPanel createButtonsPanel() { JPanel pnl = new JPanel(); pnl.add(_refreshBtn); _refreshBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { startRefreshingLog(); } }); JButton closeBtn = new JButton("Fermer"); closeBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { performClose(); } }); pnl.add(closeBtn); _errorChkbox.setSelected(true); _debugChkbox.setSelected(true); _infoChkbox.setSelected(true); ActionListener changeLogListener= new ChangeLogListener(); _errorChkbox.addActionListener(changeLogListener); _debugChkbox.addActionListener(changeLogListener); _infoChkbox.addActionListener(changeLogListener); pnl.add(_errorChkbox); pnl.add(_infoChkbox); pnl.add(_debugChkbox); //GUIUtilities.setJButtonSizesTheSame(new JButton[]{closeBtn, _refreshBtn}); getRootPane().setDefaultButton(closeBtn); return pnl; } private final class ChangeLogListener implements ActionListener { public void actionPerformed(ActionEvent evt) { ViewLogsSheet.this.startRefreshingLog(); } } private final class Refresher implements Runnable { public void run() { // ViewLogsSheet.this. refreshLog(); } } private class LenghtDetect implements Runnable { public void run() { while (!_closing) { try { Thread.sleep(1000); if(_fileLenght<_logDir.length()){ if(!_refreshing)refreshLog(); //doit être exécuter sur le même thread _fileLenght = _logDir.length(); } } catch (InterruptedException ex) { _log.error("Erreur Thread lecture console",ex); } } } } private class AbstractActionImpl extends AbstractAction { private static final long serialVersionUID = 1L; public AbstractActionImpl() { } public void actionPerformed(ActionEvent actionEvent) { performClose(); } } }

Conclusion :


J'avais proposé il y a quelque temps une technique pour redirigé la console, mais c'est un procédé lourd. Celle-ci me semble plus sympa.
Si il y a un bug dans le code, je vous souhaite bonne chance ca je n'ai pas laissé tellement de commentaires.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.