2020-04-17 20:40:32 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import org.apache.tools.ant.BuildException
|
|
|
|
|
|
|
|
// Checks logging calls to keep from using patterns that might be expensive
|
2020-04-30 19:50:31 -04:00
|
|
|
// either in CPU or unnecessary object creation. Some of the checks are a little
|
|
|
|
// over-zealous, but it's worth a bit of aggravation to catch the egregious
|
|
|
|
// mistakes before they accrue.
|
|
|
|
|
|
|
|
// This all started with Java Flight Recorder showing a massive number of
|
|
|
|
// objects allocated and garbage collected. Tracing a bunch of them down showed
|
|
|
|
// that we have a number of calls like:
|
|
|
|
// log.debug("some string" + very_expensive_object_to_make_a_string).
|
|
|
|
// which _always_ constructed the very expensive ojbect even if the log level
|
|
|
|
// was INFO or more severe. Rather than leave lots of confusing formats laying
|
|
|
|
// about or expecting people to remember the rules, we opted to make this
|
|
|
|
// check part of Gradle precommit. It will be included by default in Solr
|
|
|
|
// 9.0.
|
|
|
|
|
|
|
|
// See the associated help task "gradlew helpValidateLogCalls"
|
2020-04-17 20:40:32 -04:00
|
|
|
|
2020-06-02 07:49:57 -04:00
|
|
|
configure(subprojects.findAll { it.path.startsWith(':solr') }) {
|
2020-04-27 20:45:57 -04:00
|
|
|
plugins.withType(JavaPlugin) {
|
2020-09-04 05:11:57 -04:00
|
|
|
task validateLogCalls(type: ValidateLogCallsTask) { task ->
|
2020-04-17 20:40:32 -04:00
|
|
|
description "Checks that log calls are either validated or conform to efficient patterns."
|
|
|
|
group "verification"
|
2020-06-02 07:49:57 -04:00
|
|
|
|
2020-09-04 05:11:57 -04:00
|
|
|
// This task has no proper outputs.
|
|
|
|
setupDummyOutputs(task)
|
|
|
|
|
2020-06-02 07:49:57 -04:00
|
|
|
sourceFiles = files(sourceSets*.java)
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
2020-09-08 04:08:51 -04:00
|
|
|
|
|
|
|
// Add log validation to per-project checks as well.
|
|
|
|
check.dependsOn validateLogCalls
|
|
|
|
}
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class ValidateLogCallsTask extends DefaultTask {
|
|
|
|
def logLevels = ["log.trace", "log.debug", "log.info", "log.warn", "log.error", "log.fatal"]
|
|
|
|
|
2020-04-18 19:08:15 -04:00
|
|
|
def errsFound = 0;
|
2020-04-27 08:34:10 -04:00
|
|
|
def violations = new ArrayList();
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
def reportViolation(String msg) {
|
|
|
|
violations.add(System.lineSeparator + msg);
|
2020-04-18 19:08:15 -04:00
|
|
|
errsFound++;
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
2020-06-02 07:49:57 -04:00
|
|
|
|
2020-04-26 09:15:21 -04:00
|
|
|
// We have a log.something line, check for patterns we're not fond of.
|
2020-04-29 10:56:54 -04:00
|
|
|
def checkLine(File file, String line, int lineNumber, String previous) {
|
2020-04-26 09:15:21 -04:00
|
|
|
boolean violation = false
|
|
|
|
|
2020-04-30 19:50:31 -04:00
|
|
|
// If the line has been explicitly OK'd, then it's OK!
|
2020-04-22 22:32:49 -04:00
|
|
|
if (line.replaceAll("\\s", "").toLowerCase().contains("//logok")) {
|
2020-04-27 20:45:57 -04:00
|
|
|
return
|
2020-04-22 22:32:49 -04:00
|
|
|
}
|
2020-04-17 20:40:32 -04:00
|
|
|
// Strip all of the comments, things in quotes and the like.
|
|
|
|
def stripped =
|
2020-04-30 19:50:31 -04:00
|
|
|
line.replaceFirst("//.*", " ") // remove comment to EOL. May be fragile due to the possibility of embedded double slashes
|
|
|
|
.replaceFirst(/.*?\(/, " ") // Get rid of "log.whatever("
|
2020-04-17 20:40:32 -04:00
|
|
|
.replaceFirst(/\);/, " ") // get rid of the closing ");"
|
|
|
|
.replaceFirst("/\\*.*?\\*/", " ") // replace embedded comments "/*....*/"
|
2020-04-29 10:56:54 -04:00
|
|
|
.replaceAll(/".*?"/, '""') // remove anything between quotes. This is a bit fragile if there are embedded double quotes.
|
2020-04-17 20:40:32 -04:00
|
|
|
.replaceAll(/timeLeft\(.*?\)/, " ") // used all over tests, it's benign
|
|
|
|
.replaceAll(/TimeUnit\..*?\.convert\(.*?\)/, " ") // again, a pattern that's efficient
|
2020-04-29 10:56:54 -04:00
|
|
|
.replaceAll("\\s", "")
|
2020-04-17 20:40:32 -04:00
|
|
|
|
2020-04-29 10:56:54 -04:00
|
|
|
// The compiler will pre-assemble patterns like 'log.info("string const1 {}" + " string const2 {}", obj1, obj2)'
|
2020-04-30 19:50:31 -04:00
|
|
|
// to log.info("string const1 {} string const2 {}", obj1, obj2)', so don't worry about any plus preceded and
|
|
|
|
// followed by double quotes, otherwise flag it.
|
2020-04-29 10:56:54 -04:00
|
|
|
def hasPlus = false
|
|
|
|
for (int idx = 0; idx < stripped.length(); ++idx) {
|
|
|
|
if (stripped.charAt(idx) == '+') {
|
|
|
|
if (idx == 0 || idx == stripped.length() - 1
|
|
|
|
|| stripped.charAt(idx - 1) != '"' || stripped.charAt(idx + 1) != '"') {
|
|
|
|
hasPlus = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 19:50:31 -04:00
|
|
|
//TODO remove me
|
|
|
|
String cause = ""
|
|
|
|
// Get the level of this log message.
|
|
|
|
// We don't really care about checking for method calls for WARN and more severe, the presumption
|
|
|
|
// is that they _should_ always be reported.
|
2020-04-26 09:15:21 -04:00
|
|
|
|
2020-04-30 19:50:31 -04:00
|
|
|
def level = ""
|
|
|
|
def lev = (line =~ "log\\.(.*?)\\(")
|
|
|
|
if (lev.find()) {
|
|
|
|
level = lev.group(1).toLowerCase().trim()
|
|
|
|
}
|
|
|
|
if (logLevels.contains("log." + level) == false) {
|
2020-05-01 13:06:57 -04:00
|
|
|
reportViolation(String.format("Found an unexpected log level: %s, file: %s:%d"
|
2020-04-30 19:50:31 -04:00
|
|
|
, level, file.getAbsolutePath(), lineNumber))
|
|
|
|
}
|
|
|
|
boolean persnicketyLevel = !(level.equals("fatal") || level.equals("error") || level.equals("warn"))
|
2020-04-24 13:34:03 -04:00
|
|
|
|
2020-04-30 19:50:31 -04:00
|
|
|
// Check that previous line isn't an if statement for always-reported log levels.
|
2020-04-20 21:08:15 -04:00
|
|
|
// There's a convention to declare a member variable for whether a level is enabled and check that rather than
|
2020-04-24 13:34:03 -04:00
|
|
|
// isDebugEnabled so we need to check both.
|
|
|
|
String prevLine = previous.replaceAll("\\s+", "").toLowerCase()
|
|
|
|
boolean prevLineNotIf = ((prevLine.contains("if(log.is" + level + "enabled") == false
|
|
|
|
&& prevLine.contains("if(" + level + ")") == false))
|
|
|
|
|
2020-04-30 19:50:31 -04:00
|
|
|
if (hasPlus) {
|
|
|
|
cause = "hasPlus: " + hasPlus
|
|
|
|
violation = true
|
|
|
|
}
|
|
|
|
if (violation == false) {
|
|
|
|
def m = stripped =~ "\\(.*?\\)"
|
|
|
|
def hasParens = m.find()
|
|
|
|
|
|
|
|
if (hasParens && prevLineNotIf && persnicketyLevel) {
|
|
|
|
cause = "hasParens " + hasParens + " prevLineNotIf " + prevLineNotIf + " pernicketyLevel " + persnicketyLevel
|
2020-04-24 13:34:03 -04:00
|
|
|
violation = true
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 19:50:31 -04:00
|
|
|
|
2020-04-29 10:56:54 -04:00
|
|
|
// Always report toString(). Note, this over-reports some constructs
|
|
|
|
// but just add //logOK if it's really OK.
|
2020-04-30 19:50:31 -04:00
|
|
|
if (violation == false) {
|
|
|
|
if (line.contains("toString(") == true && prevLineNotIf) {
|
|
|
|
cause = "Line contains toString"
|
2020-04-29 10:56:54 -04:00
|
|
|
violation = true
|
|
|
|
}
|
2020-04-24 13:34:03 -04:00
|
|
|
}
|
2020-07-04 09:38:41 -04:00
|
|
|
if (stripped.contains("getMessage()") || stripped.contains("getCause()")) {
|
|
|
|
cause = "getMessage or getCause in log line";
|
|
|
|
violation = true;
|
|
|
|
}
|
2020-04-24 13:34:03 -04:00
|
|
|
if (violation) {
|
2020-04-30 19:50:31 -04:00
|
|
|
reportViolation(String.format("cause: '%s' Suspicious logging call, Parameterize and possibly surround with 'if (log.is*Enabled) {..}'. Help at: 'gradlew helpValidateLogCalls' %s %s:%d"
|
|
|
|
, cause , System.lineSeparator, file.getAbsolutePath(), lineNumber))
|
2020-04-24 13:34:03 -04:00
|
|
|
}
|
2020-04-27 20:45:57 -04:00
|
|
|
return
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Require all our logger definitions lower case "log", except a couple of special ones.
|
|
|
|
def checkLogName(File file, String line) {
|
|
|
|
// It's many times faster to do check this way than use a regex
|
|
|
|
if (line.contains("static ") && line.contains("getLogger") && line.contains(" log ") == false) {
|
2020-04-30 19:50:31 -04:00
|
|
|
String name = file.name
|
|
|
|
if (name.equals("LoggerFactory.java")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (name.equals("SolrCore.java") && (line.contains("requestLog") || line.contains("slowLog"))) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (name.equals("StartupLoggingUtils.java") && line.contains("getLoggerImplStr")) {
|
|
|
|
return
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
2020-04-30 19:50:31 -04:00
|
|
|
// Apparently the Hadoop code expectes upper-case LOG, so...
|
|
|
|
|
|
|
|
if ((name.equals("HttpServer2.java") || name.equals("BlockPoolSlice.java") || name.equals("FileUtil.java"))
|
|
|
|
&& line.contains(" LOG ")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reportViolation("Change the logger name to lower-case 'log' in " + file.name + " " + line + " project" + project)
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def checkFile(File file) {
|
2020-04-30 19:50:31 -04:00
|
|
|
int state = 0
|
2020-04-17 20:40:32 -04:00
|
|
|
int lineNumber = 0
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
// We have to assemble line-by-line to get the full log statement. We require that all loggers (except requestLog
|
|
|
|
// and slowLog) be exactly "log". That will be checked as well.
|
|
|
|
String prevLine = ""
|
|
|
|
file.eachLine { String line ->
|
|
|
|
lineNumber++
|
|
|
|
checkLogName(file, line)
|
|
|
|
switch (state) {
|
2020-04-30 19:50:31 -04:00
|
|
|
case 0: // Not collecting a logging line.
|
2020-04-17 20:40:32 -04:00
|
|
|
logLevels.each {
|
|
|
|
if (line.contains(it)) {
|
|
|
|
if (line.contains(");")) {
|
|
|
|
state = 2
|
|
|
|
} else {
|
|
|
|
state = 1
|
|
|
|
}
|
|
|
|
sb.setLength(0)
|
|
|
|
sb.append(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2020-04-30 19:50:31 -04:00
|
|
|
case 1: // collecting a log line
|
|
|
|
if (line.replaceFirst("//.*", " ")
|
|
|
|
.replaceFirst("/\\*.*?\\*/", " ") // replace embedded comments "/*....*/"
|
|
|
|
.replaceAll(/".*?"/, '""') // remove anything between quotes. This is a bit fragile if there are embedded double quotes.
|
|
|
|
.trim().endsWith(");")) {
|
2020-04-17 20:40:32 -04:00
|
|
|
state = 2
|
|
|
|
}
|
|
|
|
sb.append(line)
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
reportViolation("Bad state, should be 0-1 in the switch statement") // make people aware the code sucks
|
|
|
|
break
|
|
|
|
}
|
2020-06-02 07:49:57 -04:00
|
|
|
|
|
|
|
// It's just easier to do this here rather than read another line in the switch above.
|
|
|
|
if (state == 0) {
|
|
|
|
// Not collecting a log line
|
2020-04-22 22:32:49 -04:00
|
|
|
prevLine = line.toLowerCase();
|
2020-06-02 07:49:57 -04:00
|
|
|
} else if (state == 1) {
|
|
|
|
// collecting a logging line.
|
|
|
|
} else if (state == 2) {
|
|
|
|
// We've collected the complete log line.
|
2020-04-29 10:56:54 -04:00
|
|
|
checkLine(file, sb.toString(), lineNumber, prevLine)
|
2020-04-17 20:40:32 -04:00
|
|
|
state = 0
|
2020-06-02 07:49:57 -04:00
|
|
|
} else {
|
|
|
|
assert false
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 07:49:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@InputFiles
|
|
|
|
FileCollection sourceFiles
|
|
|
|
|
2020-04-17 20:40:32 -04:00
|
|
|
@TaskAction
|
|
|
|
def checkLogLines() {
|
2020-06-02 07:49:57 -04:00
|
|
|
sourceFiles.each { checkFile(it) }
|
2020-07-20 06:47:14 -04:00
|
|
|
logger.info("Checked: ${sourceFiles.files.size()}")
|
2020-04-27 20:45:57 -04:00
|
|
|
|
2020-04-18 19:08:15 -04:00
|
|
|
if (errsFound > 0) {
|
2020-04-17 20:40:32 -04:00
|
|
|
throw new BuildException(String.format(Locale.ENGLISH, 'Found %d violations in source files (%s).',
|
2020-04-29 10:56:54 -04:00
|
|
|
errsFound, violations.join(', ')));
|
2020-04-17 20:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|