[Misc] Apply the logging best practices: don't drop the caught exception in a warn() - #399
Merged
Conversation
…ion in a warn() First pass of the xwiki-platform logging audit over xwiki-rendering. Two warn() calls inside a catch never mentioned the exception they were holding, so the reason the operation failed reached no log at all. warn() must not print a stack trace, so the cause goes into a placeholder as ExceptionUtils.getRootCauseMessage(e). Everything else the audit's detector reports in this repo is a non-violation and is recorded in the PR description: the four isDebugEnabled() guards in BlockStateChainingListener (they also skip a `new IllegalStateException()`, which captures a stack trace, so removing them would pay that on every event) and JspWikiSerializer's System.out (an output sink, not logging). Also fixes "continnue" in the comment right above the link-checker change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The six-pass logging audit that ran over
xwiki-platform(PRs #6055 … #6079 there) had never been pointed at this repository. This is that scan, re-implemented as a single parser covering all six passes' categories: placeholder arity and syntax, bracketing,warn()with a Throwable, concatenated /String.format()messages,e.getMessage(), calls with no message literal at all,printStackTrace(),System.out, redundant level guards, what acatchdoes with the exception it holds, and how theLoggerfield is acquired, named and modified. It resolves everyLogger-typed identifier in a file rather than matching a receiver namedlogger, so a differently-named field is not invisible to it.Over 667
src/main/javafiles it reports 8 candidates, of which 2 are real — andsrc/test/javaadds nothing at all.The two fixes
Both are the same defect: a
warn()inside acatchthat never mentions the exception, so the reason the operation failed reached no log at all.warn()must not print a stack trace, so the cause goes into a placeholder:DefaultLinkCheckerThread.sendEvent—catch (Exception e)around the Observation Manager lookup. The comment says "log a warning since it's not really normal", but nothing said what was not normal.DefaultMacroManager.getMacroIds—catch (ParseException e)on an invalid macro id. The message explains the expected format at length; the exception is the only thing that said which part of the actual hint was wrong (Invalid macro id format [macro/invalidsyntax]).Both tests that pin these messages are updated. Also fixes
continnuein the comment immediately above the link-checker change.The six candidates that are not violations
Recorded here so a future re-run recognises them instead of re-deciding:
if (LOGGER.isDebugEnabled())guards inBlockStateChainingListener(invalid definition-list / list nesting). The pass-4 rule is to unwrap a guard around a single parameterized call — unless the guard also skips real work. Each of these calls builds anew IllegalStateException(), i.e. captures a stack trace, and argument evaluation is not deferred by SLF4J. Removing the guard would pay a stack-trace capture on every misnested list item in every parse. They are alsoelse ifbranches of a real condition, so the guard is load-bearing in the control flow as well.JspWikiSerializer.print/println'sSystem.out— the serializer's output sink when no buffer is set, i.e. console output is the point (the same call asMimeTypesUtil.mainin the platform audit). The class is also part of the integrated WikiModel code.Verification
mvn -B -ntp testgreen fromxwiki-rendering-transformation-macro(93 tests) and fromxwiki-rendering-transformation-linkchecker(13 tests).