2020-04-17 20:40:32 -04:00
|
|
|
Using log calls
|
|
|
|
===============
|
|
|
|
|
2020-05-01 13:06:57 -04:00
|
|
|
There are several gotchas when logging. In particular, methods
|
|
|
|
are called and strings are concatenated that have variables
|
|
|
|
and objects can be constructed even if the logging level
|
|
|
|
is more restrictive than the call. For instance, if the logging
|
|
|
|
level is WARN, then a call like:
|
|
|
|
log.trace("something {}", some_object.toString())
|
|
|
|
will cause some_object.toString() to be called, then the resulting
|
|
|
|
string is passed to log.trace() where the log level is evaluated
|
|
|
|
and the logging call may be aborted. Any method called explicitly
|
|
|
|
as part of a logging call is executed regardless of whether the
|
|
|
|
message will be logged or not.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
This can be very expensive as per measurements with Java Flight
|
|
|
|
Recorder. It's particularly egregious when complex operations
|
2020-05-01 13:06:57 -04:00
|
|
|
are performed for, say, debug or trace levels, which
|
|
|
|
are rarely reported.
|
|
|
|
|
|
|
|
Here are a few patterns that generate useless strings/objects:
|
2020-04-17 20:40:32 -04:00
|
|
|
|
2020-04-29 10:56:54 -04:00
|
|
|
- log.info("some stuff " + some_object) will concatenate the
|
2020-05-01 13:06:57 -04:00
|
|
|
some_object.toString() and create a new string. The only
|
|
|
|
exception is if "some_object" is a literal string.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
- log.info("some stuff {}", object.method()) will execute the
|
|
|
|
method.
|
|
|
|
|
|
|
|
- log.info("some stuff {}", object.toString()) will execute
|
2020-05-01 13:06:57 -04:00
|
|
|
the toString and then may throw the results away.
|
|
|
|
log.info("some stuff{}", object)
|
|
|
|
is safe, the object's toString() method is called only after
|
|
|
|
the log/do not log decision has been made.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
-----------
|
|
|
|
NOTES:
|
|
|
|
|
|
|
|
- If you're creating a logging call that has no choice but to
|
|
|
|
do something expensive, surround it with "if (log.is*Enabled)".
|
2020-05-01 13:06:57 -04:00
|
|
|
Validation will flag any situation where the level in the "if"
|
|
|
|
clause does not match the level of the actual call.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
- Logging calls can take a bare exception, but do not
|
|
|
|
use the braces if you want the stack trace! So
|
|
|
|
log.info("stuff {}", exception) will not print the full stack,
|
|
|
|
log.info("stuff ", exception) _will_ print the full stack.
|
2020-05-01 13:06:57 -04:00
|
|
|
log.info("stuff {} ", some_object, exception) will print "stuff + the result
|
|
|
|
of the object's toString() method" plus the full stack. If you're puzzled
|
|
|
|
as to why so many logging calls don't have a matching number of curly-braces
|
|
|
|
and parameters, this is usually why.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
|
|
|
- When slf4j supports lambdas in logging calls (log4j2 does now),
|
|
|
|
we can use lambdas rather than "if log.is*Enabled". slf4j 2.0
|
2020-05-01 13:06:57 -04:00
|
|
|
will when released. This will probably mean this check has to be re-worked
|
|
|
|
to avoid false flags
|
2020-04-17 20:40:32 -04:00
|
|
|
|
2020-05-01 13:06:57 -04:00
|
|
|
- warn, error, and fatal level messages are NOT flagged for explicit,
|
|
|
|
function calls, but will be flagged if they contain plusses that aren't
|
|
|
|
simple concatenation. This last is something of a style check.
|
2020-04-17 20:40:32 -04:00
|
|
|
|
2020-04-22 22:32:49 -04:00
|
|
|
- You can get into some pretty convolued consructs trying to pass some of these
|
2020-10-03 09:47:37 -04:00
|
|
|
checks. Adding //nowarn, with or without spaces will cause the line to pass
|
2020-05-01 13:06:57 -04:00
|
|
|
no matter what. Please use this hack sparingly and be conscientious about
|
|
|
|
surrounding with 'if (log.is*Enabled)'.
|
2020-04-22 22:32:49 -04:00
|
|
|
|
2020-05-01 13:06:57 -04:00
|
|
|
- String constants may be added with '+', but they _must_ be literal strings.
|
2020-04-29 10:56:54 -04:00
|
|
|
The compiler is smart enough to concatenate them when compiling. For example:
|
2020-05-01 13:06:57 -04:00
|
|
|
log.info("some {} "+ "more nonsense {}", object1, object2) is fine
|
|
|
|
log.info("some{} " + " more nonsense " + object1, object2) is NOT fine (note
|
|
|
|
the plus in front of object1).
|
2020-04-29 10:56:54 -04:00
|
|
|
|
2020-04-17 20:40:32 -04:00
|
|
|
For a fuller discussion, see LUCENE-7788 and the other JIRAs linked
|
|
|
|
from there.
|