Project log4j Debugging on --verbose: Difference between revisions
(23 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
* [[projects#Patterns|projects]] | * [[projects#Patterns|projects]] | ||
* [[clad]] | * [[clad]] | ||
* [[Project Alternative log4j Configuration]] | |||
=Overview= | =Overview= | ||
A pattern that consists in dynamically turning on logging verbosity and routing all log4j logging information to stderr if -v or --verbose command line options are used. Projects that implement it are [[clad]], [[Generic Load Driver (gld)]], [[events-java-memory]], [[events-csv]] etc. | |||
= | This pattern only applies to command line utilities, which are supposed to run in foreground and send their logging at stdout or stderr. For processes that run in background, use [[Project Alternative log4j Configuration|Project Alternative log4j Configuration]]. log4j debugging on --verbose and [[Project Alternative log4j Configuration|alternative log4j configuration]] can be combined, where --verbose flag applies until the configuration file is parsed and the new logging configuration is installed. | ||
=API= | |||
Use [[novaordis-utilities]] 4.2 or newer. novaordis-utilities 4.2.0 was the first release that contains <tt>StderrVerboseLogging</tt>. | Use [[novaordis-utilities]] 4.2 or newer. novaordis-utilities 4.2.0 was the first release that contains <tt>StderrVerboseLogging</tt>. | ||
Line 18: | Line 21: | ||
</blockquote> | </blockquote> | ||
<tt>StderrVerboseLogging</tt> comes with an <tt>init()</tt> static utility method that looks at the "verbose" system property and turns on stderr logging. The recommended usage pattern is: | |||
<syntaxhighlight lang='java'> | |||
import io.novaordis.utilities.logging.StderrVerboseLogging; | |||
StderrVerboseLogging.init(); | |||
</syntaxhighlight> | |||
< | You should call <tt>StderrVerboseLogging.init()</tt> as early as possible after the JVM starts running. For suggestion on how to pass the logging verbosity option from the shell wrapper to the JVM, see [[#Shell_Wrapper_Recommendations|Shell Wrapper Recommendations]] below. | ||
If you want to enable logging manually in the program, use: | |||
<syntaxhighlight lang='java'> | |||
import io.novaordis.utilities.logging.StderrVerboseLogging; | import io.novaordis.utilities.logging.StderrVerboseLogging; | ||
... | ... | ||
if (verbose) { | if (verbose) { | ||
StderrVerboseLogging.enable(); | StderrVerboseLogging.enable(); | ||
} | } | ||
... | |||
</syntaxhighlight> | |||
... | ==Logging Pattern== | ||
</ | |||
The default logging pattern, hardcoded in io.novaordis.utilities.logging.StderrVerboseLogging, is: | |||
@%t %d{ABSOLUTE} %-5p [%c{1}] %m%n | |||
However, it can be changed programmatically, by calling '''before''' the init() call: | |||
<syntaxhighlight lang='java'> | |||
StderrVerboseLogging.setLoggingPattern("%c{1}: %m%n"); | |||
</syntaxhighlight> | |||
=Shell Wrapper Recommendations= | |||
The recommended way to pass the "verbose" information that was already identified by a bash wrapper to the JVM is to use: | The recommended way to pass the "verbose" information that was already identified by a bash wrapper to the JVM is to use: | ||
Line 40: | Line 65: | ||
Typical bash wrapper code: | Typical bash wrapper code: | ||
< | <syntaxhighlight lang='bash'> | ||
verbose=false | verbose=false | ||
... | ... | ||
${verbose} && verbose_system_property="-Dverbose=true" | ${verbose} && verbose_system_property="-Dverbose=true" | ||
command="${java_bin} ... ${verbose_system_property} ${main_class}..." | command="${java_bin} ... ${verbose_system_property} ${main_class}..." | ||
</syntaxhighlight> | |||
[https://github.com/NovaOrdis/bash-wrapper-functions/blob/master/application-shell-wrapper-template application-shell-wrapper-template] of [[bash-wrapper-functions]] implements this pattern. |
Latest revision as of 18:46, 8 September 2017
Internal
Overview
A pattern that consists in dynamically turning on logging verbosity and routing all log4j logging information to stderr if -v or --verbose command line options are used. Projects that implement it are clad, Generic Load Driver (gld), events-java-memory, events-csv etc.
This pattern only applies to command line utilities, which are supposed to run in foreground and send their logging at stdout or stderr. For processes that run in background, use Project Alternative log4j Configuration. log4j debugging on --verbose and alternative log4j configuration can be combined, where --verbose flag applies until the configuration file is parsed and the new logging configuration is installed.
API
Use novaordis-utilities 4.2 or newer. novaordis-utilities 4.2.0 was the first release that contains StderrVerboseLogging.
For more details, see:
StderrVerboseLogging comes with an init() static utility method that looks at the "verbose" system property and turns on stderr logging. The recommended usage pattern is:
import io.novaordis.utilities.logging.StderrVerboseLogging;
StderrVerboseLogging.init();
You should call StderrVerboseLogging.init() as early as possible after the JVM starts running. For suggestion on how to pass the logging verbosity option from the shell wrapper to the JVM, see Shell Wrapper Recommendations below.
If you want to enable logging manually in the program, use:
import io.novaordis.utilities.logging.StderrVerboseLogging;
...
if (verbose) {
StderrVerboseLogging.enable();
}
...
Logging Pattern
The default logging pattern, hardcoded in io.novaordis.utilities.logging.StderrVerboseLogging, is:
@%t %d{ABSOLUTE} %-5p [%c{1}] %m%n
However, it can be changed programmatically, by calling before the init() call:
StderrVerboseLogging.setLoggingPattern("%c{1}: %m%n");
Shell Wrapper Recommendations
The recommended way to pass the "verbose" information that was already identified by a bash wrapper to the JVM is to use:
-Dverbose=true
Typical bash wrapper code:
verbose=false
...
${verbose} && verbose_system_property="-Dverbose=true"
command="${java_bin} ... ${verbose_system_property} ${main_class}..."
application-shell-wrapper-template of bash-wrapper-functions implements this pattern.