Hi,
Logging level is set in logback.xml. To change the logging level for a given
logger/package/class, you must modify that file. See [1] for more details.
In your code example, you are creating the logger correctly using the
LoggerFactory. All NiFi Frameworks and extensions use the slf4j API, which is
configured by logback.xml. That is why you do not see any explicit setting of
log levels in our codebase - the logging framework we are using takes care of
that.
With regards to why you will sometimes see logging gated by "if
(logger.isDebugEnabled()) { .. }" conditionals: this is not strictly necessary,
but is useful in a few scenarios. One case is when the construction of the log
message itself is expensive and adds non-trivial overhead, and you don't want
to incur that expense unless you are actually logging the message. Consider the
following:
String jsonRepresentationOfX = x.toJsonString();
logger.debug("The JSON representation of x is: {}", jsonRepresentationOfX");
If the log level is not set to debug, then the call to logger.debug(...) is a
no-op; however, you still incur the overhead of the call to x.toJsonString().
This is a somewhat contrived example, but in an unavoidable situation you can
see where sometimes gating a block of code by the logger.isDebugEnabled()
conditional can save computation that is only necessary when you need to log
the result.
The rule of thumb I follow (though others can chime in with their own thoughts
on the subject), is to almost always log using the simple logger.{level}("my
logging message") methods without worrying much about adding additional guards.
But if you do run into a situation where you want your code to behave
differently depending on the log level aside from just what shows up in the
log, that is what those lookup methods on the logger object are there for.
[1]
https://community.hortonworks.com/articles/106931/nifi-debugging-tutorial.html
Hope this helps,
Kevin
On 9/11/18, 08:38, "James McMahon" <[email protected]> wrote:
Good morning. I am confused about logging levels from within java code that
defines a nar.
I have a java class file AMQPWorker.java, within which I establish a logger
used throughout my nar. This is what I inherited in the class:
abstract class AMQPWorker implements AutoCloseable {
private final static Logger logger =
LoggerFactory.getLogger(AMQPWorker.class);
Later in this class there is a method that appears to check the log level
prior to publishing a message to the log, like so
if (logger.isDebugEnabled()) {
logger.debug("some debug message");
}
When this if statement is executed, what exactly is being checked to verify
whether isDebugEnabled() returns true or false?
I can't find anywhere that the log level is set within the code.
How does the logging level get set?
Does the nar somehow inherit the logger properties set in logback.xml?
And if it does, wouldn't we always want the logging configuration in
logback.xml to be the level that allows any logging level from within the
code, a la logger.debug(), .info(), .warn(), etc?
Bottom line for me: throughout my code I want to be sure when I use a
particular logging level to determine if a message gets logged to my logger
or not, that level is respected.
Thank you for any help.