/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.tomcat.util.log; import org.apache.juli.logging.Log; /** * This helper class assists with the logging associated with invalid input * data. A developer may want all instances of invalid input data logged to * assist with debugging whereas in production it is likely to be desirable not * to log anything for invalid data. The following settings may be used: *
null if the
* message should not be logged.
*
*
* If INFO_THEN_DEBUG configuration option is enabled, this
* method might change internal state of this object.
*
* @return Log mode, or null
*/
public Mode getNextMode() {
if (Config.NONE == config) {
return null;
} else if (Config.DEBUG_ALL == config) {
return log.isDebugEnabled() ? Mode.DEBUG : null;
} else if (Config.INFO_THEN_DEBUG == config) {
if (logAtInfo()) {
return log.isInfoEnabled() ? Mode.INFO_THEN_DEBUG : null;
} else {
return log.isDebugEnabled() ? Mode.DEBUG : null;
}
} else if (Config.INFO_ALL == config) {
return log.isInfoEnabled() ? Mode.INFO : null;
}
// Should never happen
return null;
}
/*
* Not completely thread-safe but good enough for this use case. I couldn't
* see a simple enough way to make it completely thread-safe that was not
* likely to compromise performance.
*/
private boolean logAtInfo() {
if (suppressionTime < 0 && lastInfoTime > 0) {
return false;
}
long now = System.currentTimeMillis();
if (lastInfoTime + suppressionTime > now) {
return false;
}
lastInfoTime = now;
return true;
}
private enum Config {
NONE,
DEBUG_ALL,
INFO_THEN_DEBUG,
INFO_ALL
}
/**
* Log mode for the next log message.
*/
public enum Mode {
DEBUG,
INFO_THEN_DEBUG,
INFO
}
}