|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
VISUALISER DES JOURNAUX LOG4J
Information sur la source
Description
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
- /*
- * 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();
- }
- }
- }
/*
* 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.
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Log applicative [ par darktonight ]
Euh question de vocalulaire mais ca veut dire quoi "Trace dans la log applicative" ?C'est dans les recommandations de mon exercice.
Java tel mobile ? -> Quel log ? [ par guizmo123 ]
Salut tout le monde !Voilà j'ai un problème, je sais qu'on peut créer des logiciels en java pour téléphone mobiles tels que Nokia ou encore Sageme myx
fichier log [ par merie ]
quelq poura me dire comment je peut avoir des fichier log d'une site d'enseignement à distance
postgres sous windows XP [ par geraldl ]
geraldoBonjour à tous,voici ma première tentative sur javafr. En fait j'ai une question concernant postgres installé sous windows (version 7.5). J'ai
Pb avec les log [ par neo1260 ]
Bonjour, j'essay d'ajouter a mon programme, un systéme de fichier de log, pour ça je m'aide de l'API logging et de l'aide http://cyberzoide.developpez
TexteArea et couleur [ par bandw ]
Bonjour, Vu que la derniere fois j'ai eu une réponse ( et une bonne en plus :) )je reviens poser une question.. Peut etre qu'un jour c'est moi qui rep
Log ou exception [ par neo1260 ]
Bonjour, dans mon programme j'ai décidé d'utiliser les log pour tracer les informations général mais aussi les erreurs. C'est log sont redirigés vers
Problème de double [ par Xalendar ]
Bon mon problème est simple je construis une HashMap de log de probabilité et toute mes proba qui sont au format double se bloquent 0.0 donc
midlet j2me ou est passé java.lang.Math.log [ par misterzinzin ]
bonjour, je vien de commencer la programmation de midlet (java pour mobile) et je ne trouva pas la fonction permettant de calculer des logarithme dans
Help! warning : initialize log4j [ par kakol ]
Bonjour,Je suis en train de développer un moteur de recherche interne. j'utilise pour cele le moteur Lucene.J'essaie d'indexer des fichiers pdf a
|
Téléchargements
Logiciels à télécharger sur le même thème :
|