下面列出了java.util.logging.Level#equals ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void write(final byte[] buf, final int offset,
final int len) throws IOException {
final Instant start = Instant.now();
this.origin.write(buf, offset, len);
final Instant end = Instant.now();
final long millis = Duration.between(start, end).toMillis();
this.bytes.getAndAdd(len);
this.time.getAndAdd(millis);
final Level level = this.logger.getLevel();
if (!level.equals(Level.INFO)) {
this.logger.log(
level,
new UncheckedText(
new FormattedText(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
).asString()
);
}
}
private static LogProtocol.LogMessage.Level toLogMessageLevel(Level level) {
Level vespaLevel = LogLevel.getVespaLogLevel(level);
if (vespaLevel.equals(LogLevel.FATAL)) {
return LogProtocol.LogMessage.Level.FATAL;
} else if (vespaLevel.equals(LogLevel.ERROR)) {
return LogProtocol.LogMessage.Level.ERROR;
} else if (vespaLevel.equals(LogLevel.WARNING)) {
return LogProtocol.LogMessage.Level.WARNING;
} else if (vespaLevel.equals(LogLevel.CONFIG)) {
return LogProtocol.LogMessage.Level.CONFIG;
} else if (vespaLevel.equals(LogLevel.INFO)) {
return LogProtocol.LogMessage.Level.INFO;
} else if (vespaLevel.equals(LogLevel.EVENT)) {
return LogProtocol.LogMessage.Level.EVENT;
} else if (vespaLevel.equals(LogLevel.DEBUG)) {
return LogProtocol.LogMessage.Level.DEBUG;
} else if (vespaLevel.equals(LogLevel.SPAM)) {
return LogProtocol.LogMessage.Level.SPAM;
} else {
return LogProtocol.LogMessage.Level.UNKNOWN;
}
}
private TypeSafeMatcher<LogRecord> matcher(
final Level level, final String substring, final Throwable throwable) {
return new TypeSafeMatcher<LogRecord>() {
@Override
public void describeTo(Description description) {
description.appendText(
String.format(
"log message of level [%s] containg message [%s] with exception [%s] "
+ "containing message [%s]",
level, substring, throwable.getClass(), throwable.getMessage()));
}
@Override
protected boolean matchesSafely(LogRecord item) {
return level.equals(item.getLevel())
&& item.getMessage().contains(substring)
&& item.getThrown().getClass().equals(throwable.getClass())
&& item.getThrown().getMessage().contains(throwable.getMessage());
}
};
}
/** Formats a {@link ConsoleEvent} and adds it to {@code lines}. */
public static ImmutableList<String> formatConsoleEvent(ConsoleEvent consoleEvent, Ansi ansi) {
String message = consoleEvent.getMessage();
if (message == null) {
LOG.error("Formatting console event with null message");
return ImmutableList.of();
}
String formattedLine = "";
Level logEventLevel = consoleEvent.getLevel();
if (consoleEvent.containsAnsiEscapeCodes() || logEventLevel.equals(Level.INFO)) {
formattedLine = message;
} else {
if (logEventLevel.equals(Level.WARNING)) {
formattedLine = ansi.asWarningText(message);
} else if (logEventLevel.equals(Level.SEVERE)) {
formattedLine = ansi.asHighlightedFailureText(message);
}
}
if (formattedLine.isEmpty()) {
return ImmutableList.of();
}
return ImmutableList.copyOf(Splitter.on(System.lineSeparator()).split(formattedLine));
}
protected int getMessageLevel( LogRecord record )
{
Level level = record.getLevel( );
if ( level.equals( Level.SEVERE ) )
{
return Project.MSG_ERR;
}
if ( level.equals( Level.WARNING ) )
{
return Project.MSG_WARN;
}
if ( level.equals( Level.INFO ) )
{
return Project.MSG_INFO;
}
return Project.MSG_VERBOSE;
}
private void logMe(ServerFaultCode serverFault, Level level) {
// If it's an internal error, then we should always log.
// A client fault is only logged if we're on debug logging.
if (ResponseCode.InternalError == serverFault.getResponseCode()) {
level = Level.WARNING;
}
Logger logger = getLogger();
String additionalInfo = additionalInfo();
String message = additionalInfo == null ? "Exception thrown" : "Exception thrown: " + additionalInfo;
if (level.equals(Level.SEVERE)) {
logger.error(message, this);
}
else if (level.equals(Level.WARNING)) {
logger.warn(message, this);
}
else {
logger.debug(message, this);
}
}
private boolean isLoggableWithRegisteredLogLevel(LogRecord record) {
long recordThreadId = record.getThreadID();
String logRecordCommandId = state.threadIdToCommandId(recordThreadId);
if (logRecordCommandId == null) {
// An unregistered thread created this LogRecord, so we don't want to force logging it.
return false;
}
Level commandIdLogLevel = state.getLogLevel(logRecordCommandId);
if (commandIdLogLevel == null) {
// No log level override registered for this command ID. Don't force logging it.
return false;
}
// Level.ALL.intValue() is Integer.MIN_VALUE, so have to compare it explicitly.
return commandIdLogLevel.equals(Level.ALL)
|| commandIdLogLevel.intValue() >= record.getLevel().intValue();
}
default JMenu createDevSettings() {
JMenu dev = new JMenu("Dev");
JMenu logging = new JMenu("Logging");
ButtonGroup bg = new ButtonGroup();
Level[] levels = new Level[]{Level.ALL, Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.OFF};
Level currentLevel = LafManager.getLogLevel();
for (Level level : levels) {
JRadioButtonMenuItem mi = new JRadioButtonMenuItem(level.getName());
mi.addActionListener(e -> LafManager.setLogLevel(level));
bg.add(mi);
logging.add(mi);
if (level.equals(currentLevel)) {
mi.setSelected(true);
}
}
JCheckBoxMenuItem aaPainting = new JCheckBoxMenuItem("Translucent Antialiasing");
aaPainting.addActionListener(e -> StringPainter.setTranslucentAAPaintingEnabled(aaPainting.isSelected()));
aaPainting.setSelected(StringPainter.isTranslucentAAPaintingEnabled());
JCheckBoxMenuItem experimentalAA = new JCheckBoxMenuItem("Experimental Antialiasing");
experimentalAA.addActionListener(e -> StringPainter.setExperimentalAntialiasingEnabled(experimentalAA.isSelected()));
experimentalAA.setSelected(StringPainter.isExperimentalAntialiasingEnabled());
dev.add(logging);
dev.add(aaPainting);
dev.add(experimentalAA);
dev.add(new JCheckBoxMenuItem("Custom Decorations") {
{
setSelected(LafManager.isDecorationsEnabled());
addActionListener(e -> LafManager.setDecorationsEnabled(isSelected()));
}
});
return dev;
}
@Override
public void close() throws IOException {
this.origin.close();
final Level level = this.logger.getLevel();
if (level.equals(Level.INFO)) {
this.logger.log(
level,
new UncheckedText(
new FormattedText(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
).asString()
);
}
this.logger.log(
level,
new UncheckedText(
new FormattedText(
"Closed output stream from %s.",
this.destination
)
).asString()
);
}
@Override
public void flush() throws IOException {
this.origin.flush();
final Level level = this.logger.getLevel();
if (level.equals(Level.INFO)) {
this.logger.log(
level,
new UncheckedText(
new FormattedText(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
).asString()
);
}
this.logger.log(
level,
new UncheckedText(
new FormattedText(
"Flushed output stream from %s.",
this.destination
)
).asString()
);
}
private TypeSafeMatcher<LogRecord> matcher(final Level level, final String substring) {
return new TypeSafeMatcher<LogRecord>() {
@Override
public void describeTo(Description description) {
description.appendText(
String.format("log message of level [%s] containing message [%s]", level, substring));
}
@Override
protected boolean matchesSafely(LogRecord item) {
return level.equals(item.getLevel()) && item.getMessage().contains(substring);
}
};
}
public LogRecord findRecord(Level level, String message) {
synchronized (records) {
for (LogRecord r : records) {
if (level.equals(r.getLevel()) && message.equals(r.getMessage()))
return r;
}
return null;
}
}
public LogRecord findRecord(Level level, String message) {
synchronized (records) {
for (LogRecord r : records) {
if (level.equals(r.getLevel()) && message.equals(r.getMessage()))
return r;
}
return null;
}
}
public LogLevelMenu() {
super("log_level");
for (final Level level : LogViewer.SELECTABLE_LEVELS) {
JMenuItem item = new JMenuItem(new LoggedAbstractAction(level.getName()) {
private static final long serialVersionUID = 1L;
@Override
public void loggedActionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
// change the log level outside the EDT
// no progress thread because the part that may take some time (the
// GUI refresh by Swing) cannot be cancelled anyway
setLogLevel(level);
}
}).start();
}
});
// highlight current log level
if (getLogSelectionModel().getCurrentLogModel() != null) {
if (level.equals(getLogSelectionModel().getCurrentLogModel().getLogLevel())) {
item.setFont(item.getFont().deriveFont(Font.BOLD));
}
}
add(item);
}
}
/** Configures "com.google.devtools.build.*" loggers to the given {@code level}. */
public static void setupLogging(Level level) {
if (!level.equals(currentVerbosityLevel)) {
templateLogger.setLevel(level);
templateLogger.info("Log level: " + templateLogger.getLevel());
currentVerbosityLevel = level;
}
}
/**
* Returns the integer value of the log record's level to be used
* by the CLR Bridge log function.
*/
private int getLevel(final Level recordLevel) {
if (recordLevel.equals(Level.OFF)) {
return 0;
} else if (recordLevel.equals(Level.SEVERE)) {
return 1;
} else if (recordLevel.equals(Level.WARNING)) {
return 2;
} else if (recordLevel.equals(Level.ALL)) {
return 4;
} else {
return 3;
}
}
@Override
public boolean shouldLog(Level level) {
return (level.equals(logLevel));
}
static String fromLevel(final Level level) {
return level.equals(DEBUG.level) ? DEBUG.label : NORMAL.label;
}
/**
* Returns a map of effective logging levels for SIS loggers. The effective logging level take in account the level
* of parent loggers and the level of handlers. For example if a logger level is set to {@link Level#FINE} but no
* handler have a level finer than {@link Level#INFO}, then the effective logging level will be {@link Level#INFO}.
*
* <p>This method does not report the loggers that have an effective level identical to its parent logger.</p>
*
* @return the effective logging levels of SIS loggers.
*/
public static SortedMap<String,Level> getEffectiveLevels() {
final SortedMap<String,Level> levels = new TreeMap<>();
for (final Field field : Loggers.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && field.getType() == String.class) try {
levels.put((String) field.get(null), null);
} catch (IllegalAccessException e) {
/*
* Should never happen, unless we added some fields and forgot to update this method.
* In such case forget the problematic fields and search the next one. This is okay
* since this method is only for information purpose.
*/
Logging.unexpectedException(Logging.getLogger(SYSTEM), Loggers.class, "getEffectiveLevels", e);
}
}
/*
* Process the loggers in alphabetical order. The intent is to process parent loggers before child.
* The first logger in the map should be the SIS root logger, "org.apache.sis".
*/
final Iterator<Map.Entry<String,Level>> it = levels.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<String,Level> entry = it.next();
final String name = entry.getKey();
final Logger logger = Logging.getLogger(name);
Level level = getEffectiveLevel(logger);
final Level h = getHandlerLevel(logger);
if (h.intValue() > level.intValue()) {
level = h; // Take in account the logging level of handlers.
}
entry.setValue(level);
/*
* Now verify if the level is identical to the effective level of parent logger.
* If they are identical, then we remove the entry in order to report only the changes.
*/
Logger parent = logger;
while ((parent = parent.getParent()) != null) {
final Level p = levels.get(parent.getName());
if (p != null) {
if (p.equals(level)) {
it.remove();
}
break;
}
}
}
return levels;
}
/**
* Some notes on using the Pro AI logger: First, to make the logs easily readable even when there
* are hundreds of lines, I want every considerable step down in the call stack to mean more log
* message indentation. For example, the base logs in the {@link AbstractProAi} class have no
* indentation before them, but the base logs in the {@link
* games.strategy.triplea.ai.pro.ProCombatMoveAi} class will have two spaces inserted at the
* start, and the level below that, four spaces. In this way, when you're reading the log, you can
* skip over unimportant areas with speed because of the indentation. Second, I generally want the
* Fine logs to be messages that run less than 10 times each round, including almost all messages
* in the Pro AI class. Finest for messages showing details within a method that, for example,
* returns a value. (So, for example, the NCM_Task method IsTaskWorthwhile() would primarily use
* finest, as it just returns a boolean, and the logs within it are just for details) Finer for
* just about everything else. (There's also the SERVER, INFO, etc. levels) Just keep these things
* in mind while adding new logging code.
*/
public static void log(final Level level, final String message, final @Nullable Throwable t) {
final ProLogSettings settings = ProLogSettings.loadSettings();
if (!settings.isLogEnabled()) {
return; // Skip displaying to settings window if settings window option is turned off
}
final Level logDepth = settings.getLogLevel();
if (logDepth.equals(Level.FINE) && (level.equals(Level.FINER) || level.equals(Level.FINEST))) {
return; // If the settings window log depth is a higher level than this messages, skip
}
if (logDepth.equals(Level.FINER) && level.equals(Level.FINEST)) {
return;
}
ProLogUi.notifyAiLogMessage(formatMessage(message, t, level));
}