📄 logbrokermonitor.java
字号:
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software
* License version 1.1, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*/
package org.apache.log4j.lf5.viewer;
import org.apache.log4j.lf5.LogLevel;
import org.apache.log4j.lf5.LogRecord;
import org.apache.log4j.lf5.LogRecordFilter;
import org.apache.log4j.lf5.util.DateFormatManager;
import org.apache.log4j.lf5.util.LogFileParser;
import org.apache.log4j.lf5.util.StreamUtils;
import org.apache.log4j.lf5.viewer.categoryexplorer.CategoryExplorerTree;
import org.apache.log4j.lf5.viewer.categoryexplorer.CategoryPath;
import org.apache.log4j.lf5.viewer.configure.ConfigurationManager;
import org.apache.log4j.lf5.viewer.configure.MRUFileManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.List;
/**
* LogBrokerMonitor
*.
* @author Michael J. Sikorsky
* @author Robert Shaw
* @author Brad Marlborough
* @author Richard Wan
* @author Brent Sprecher
* @author Richard Hurst
*/
// Contributed by ThoughtWorks Inc.
public class LogBrokerMonitor {
//--------------------------------------------------------------------------
// Constants:
//--------------------------------------------------------------------------
public static final String DETAILED_VIEW = "Detailed";
// public static final String STANDARD_VIEW = "Standard";
// public static final String COMPACT_VIEW = "Compact";
//--------------------------------------------------------------------------
// Protected Variables:
//--------------------------------------------------------------------------
protected JFrame _logMonitorFrame;
protected int _logMonitorFrameWidth = 550;
protected int _logMonitorFrameHeight = 500;
protected LogTable _table;
protected CategoryExplorerTree _categoryExplorerTree;
protected String _searchText;
protected String _NDCTextFilter = "";
protected LogLevel _leastSevereDisplayedLogLevel = LogLevel.DEBUG;
protected JScrollPane _logTableScrollPane;
protected JLabel _statusLabel;
protected Object _lock = new Object();
protected JComboBox _fontSizeCombo;
protected int _fontSize = 10;
protected String _fontName = "Dialog";
protected String _currentView = DETAILED_VIEW;
protected boolean _loadSystemFonts = false;
protected boolean _trackTableScrollPane = true;
protected Dimension _lastTableViewportSize;
protected boolean _callSystemExitOnClose = false;
protected List _displayedLogBrokerProperties = new Vector();
protected Map _logLevelMenuItems = new HashMap();
protected Map _logTableColumnMenuItems = new HashMap();
protected List _levels = null;
protected List _columns = null;
protected boolean _isDisposed = false;
protected ConfigurationManager _configurationManager = null;
protected MRUFileManager _mruFileManager = null;
protected File _fileLocation = null;
//--------------------------------------------------------------------------
// Private Variables:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Constructors:
//--------------------------------------------------------------------------
/**
* Construct a LogBrokerMonitor.
*/
public LogBrokerMonitor(List logLevels) {
_levels = logLevels;
_columns = LogTableColumn.getLogTableColumns();
// This allows us to use the LogBroker in command line tools and
// have the option for it to shutdown.
String callSystemExitOnClose =
System.getProperty("monitor.exit");
if (callSystemExitOnClose == null) {
callSystemExitOnClose = "false";
}
callSystemExitOnClose = callSystemExitOnClose.trim().toLowerCase();
if (callSystemExitOnClose.equals("true")) {
_callSystemExitOnClose = true;
}
initComponents();
_logMonitorFrame.addWindowListener(
new LogBrokerMonitorWindowAdaptor(this));
}
//--------------------------------------------------------------------------
// Public Methods:
//--------------------------------------------------------------------------
/**
* Show the frame for the LogBrokerMonitor. Dispatched to the
* swing thread.
*/
public void show(final int delay) {
if (_logMonitorFrame.isVisible()) {
return;
}
// This request is very low priority, let other threads execute first.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Thread.yield();
pause(delay);
_logMonitorFrame.setVisible(true);
}
});
}
public void show() {
show(0);
}
/**
* Dispose of the frame for the LogBrokerMonitor.
*/
public void dispose() {
_logMonitorFrame.dispose();
_isDisposed = true;
if (_callSystemExitOnClose == true) {
System.exit(0);
}
}
/**
* Hide the frame for the LogBrokerMonitor.
*/
public void hide() {
_logMonitorFrame.setVisible(false);
}
/**
* Get the DateFormatManager for formatting dates.
*/
public DateFormatManager getDateFormatManager() {
return _table.getDateFormatManager();
}
/**
* Set the date format manager for formatting dates.
*/
public void setDateFormatManager(DateFormatManager dfm) {
_table.setDateFormatManager(dfm);
}
/**
* Get the value of whether or not System.exit() will be called
* when the LogBrokerMonitor is closed.
*/
public boolean getCallSystemExitOnClose() {
return _callSystemExitOnClose;
}
/**
* Set the value of whether or not System.exit() will be called
* when the LogBrokerMonitor is closed.
*/
public void setCallSystemExitOnClose(boolean callSystemExitOnClose) {
_callSystemExitOnClose = callSystemExitOnClose;
}
/**
* Add a log record message to be displayed in the LogTable.
* This method is thread-safe as it posts requests to the SwingThread
* rather than processing directly.
*/
public void addMessage(final LogRecord lr) {
if (_isDisposed == true) {
// If the frame has been disposed of, do not log any more
// messages.
return;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
_categoryExplorerTree.getExplorerModel().addLogRecord(lr);
_table.getFilteredLogTableModel().addLogRecord(lr); // update table
updateStatusLabel(); // show updated counts
}
});
}
public void setMaxNumberOfLogRecords(int maxNumberOfLogRecords) {
_table.getFilteredLogTableModel().setMaxNumberOfLogRecords(maxNumberOfLogRecords);
}
public JFrame getBaseFrame() {
return _logMonitorFrame;
}
public void setTitle(String title) {
_logMonitorFrame.setTitle(title + " - LogFactor5");
}
public void setFrameSize(int width, int height) {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
if (0 < width && width < screen.width) {
_logMonitorFrameWidth = width;
}
if (0 < height && height < screen.height) {
_logMonitorFrameHeight = height;
}
updateFrameSize();
}
public void setFontSize(int fontSize) {
changeFontSizeCombo(_fontSizeCombo, fontSize);
// setFontSizeSilently(actualFontSize); - changeFontSizeCombo fires event
// refreshDetailTextArea();
}
public void addDisplayedProperty(Object messageLine) {
_displayedLogBrokerProperties.add(messageLine);
}
public Map getLogLevelMenuItems() {
return _logLevelMenuItems;
}
public Map getLogTableColumnMenuItems() {
return _logTableColumnMenuItems;
}
public JCheckBoxMenuItem getTableColumnMenuItem(LogTableColumn column) {
return getLogTableColumnMenuItem(column);
}
public CategoryExplorerTree getCategoryExplorerTree() {
return _categoryExplorerTree;
}
// Added in version 1.2 - gets the value of the NDC text filter
// This value is set back to null each time the Monitor is initialized.
public String getNDCTextFilter() {
return _NDCTextFilter;
}
// Added in version 1.2 - sets the NDC Filter based on
// a String passed in by the user. This value is persisted
// in the XML Configuration file.
public void setNDCLogRecordFilter(String textFilter) {
_table.getFilteredLogTableModel().
setLogRecordFilter(createNDCLogRecordFilter(textFilter));
}
//--------------------------------------------------------------------------
// Protected Methods:
//--------------------------------------------------------------------------
protected void setSearchText(String text) {
_searchText = text;
}
// Added in version 1.2 - Sets the text filter for the NDC
protected void setNDCTextFilter(String text) {
// if no value is set, set it to a blank string
// otherwise use the value provided
if (text == null) {
_NDCTextFilter = "";
} else {
_NDCTextFilter = text;
}
}
// Added in version 1.2 - Uses a different filter that sorts
// based on an NDC string passed in by the user. If the string
// is null or is an empty string, we do nothing.
protected void sortByNDC() {
String text = _NDCTextFilter;
if (text == null || text.length() == 0) {
return;
}
// Use new NDC filter
_table.getFilteredLogTableModel().
setLogRecordFilter(createNDCLogRecordFilter(text));
}
protected void findSearchText() {
String text = _searchText;
if (text == null || text.length() == 0) {
return;
}
int startRow = getFirstSelectedRow();
int foundRow = findRecord(
startRow,
text,
_table.getFilteredLogTableModel().getFilteredRecords()
);
selectRow(foundRow);
}
protected int getFirstSelectedRow() {
return _table.getSelectionModel().getMinSelectionIndex();
}
protected void selectRow(int foundRow) {
if (foundRow == -1) {
String message = _searchText + " not found.";
JOptionPane.showMessageDialog(
_logMonitorFrame,
message,
"Text not found",
JOptionPane.INFORMATION_MESSAGE
);
return;
}
LF5SwingUtils.selectRow(foundRow, _table, _logTableScrollPane);
}
protected int findRecord(
int startRow,
String searchText,
List records
) {
if (startRow < 0) {
startRow = 0; // start at first element if no rows are selected
} else {
startRow++; // start after the first selected row
}
int len = records.size();
for (int i = startRow; i < len; i++) {
if (matches((LogRecord) records.get(i), searchText)) {
return i; // found a record
}
}
// wrap around to beginning if when we reach the end with no match
len = startRow;
for (int i = 0; i < len; i++) {
if (matches((LogRecord) records.get(i), searchText)) {
return i; // found a record
}
}
// nothing found
return -1;
}
/**
* Check to see if the any records contain the search string.
* Searching now supports NDC messages and date.
*/
protected boolean matches(LogRecord record, String text) {
String message = record.getMessage();
String NDC = record.getNDC();
if (message == null && NDC == null || text == null) {
return false;
}
if (message.toLowerCase().indexOf(text.toLowerCase()) == -1 &&
NDC.toLowerCase().indexOf(text.toLowerCase()) == -1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -