Project Alternative log4j Configuration: Difference between revisions
No edit summary |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
=Overview= | =Overview= | ||
A pattern that consists in applying alternative log4j configuration for applications that run in background. The application rely on a base log4j configuration file, usually shipped as part of the application's installation bundle, but it can change its logging behavior based on logging configuration present in the application's configuration file. Configuration that comes from the application's configuration file takes priority over configuration stored in log4j.xml. | A pattern that consists in applying alternative log4j configuration for applications that run in background. The application rely on a base log4j configuration file, usually shipped as part of the application's installation bundle, but it can change its logging behavior based on logging configuration present in the application's configuration file. Configuration that comes from the application's configuration file takes priority over configuration stored in log4j.xml, and the application usually ends up with a combination of those two, which is the ''effective logging configuration''. | ||
{{Warn|'''Important!''' The effective logging configuration becomes active only after <tt>AlternativeLoggingConfiguration.apply(...)</tt> is invoked. Before this call, the basis log4j.xml configuration applies. For the same reason, caching log.isTraceEnabled() into a static variable may become stale after applying the configuration change, so it is not recommended.}} | |||
For command-line utilities that run in foreground and want to route their all log4j logging information to stderr if -v or --verbose command line options are used, see: {{Internal|Project log4j Debugging on --verbose|Project log4j Debugging on --verbose}} | For command-line utilities that run in foreground and want to route their all log4j logging information to stderr if -v or --verbose command line options are used, see: {{Internal|Project log4j Debugging on --verbose|Project log4j Debugging on --verbose}} | ||
Line 15: | Line 17: | ||
The API to support alternative logging configuration is available in [[Novaordis-utilities|novaordis-utilities]] 4.7.8 or newer, and consists in a collection of static utilities [https://github.com/NovaOrdis/novaordis-utilities/blob/master/src/main/java/io/novaordis/utilities/logging/AlternativeLoggingConfiguration.java AlternativeLoggingConfiguration], the [https://github.com/NovaOrdis/novaordis-utilities/blob/master/src/main/java/io/novaordis/utilities/logging/LoggingConfiguration.java LoggingConfiguration] interface and other support classes. | The API to support alternative logging configuration is available in [[Novaordis-utilities|novaordis-utilities]] 4.7.8 or newer, and consists in a collection of static utilities [https://github.com/NovaOrdis/novaordis-utilities/blob/master/src/main/java/io/novaordis/utilities/logging/AlternativeLoggingConfiguration.java AlternativeLoggingConfiguration], the [https://github.com/NovaOrdis/novaordis-utilities/blob/master/src/main/java/io/novaordis/utilities/logging/LoggingConfiguration.java LoggingConfiguration] interface and other support classes. | ||
<syntaxhighlight lang='java'> | |||
Yaml yaml = new Yaml(); | |||
Map m = (Map)yaml.load(...); | |||
Map alternativeLoggingConfigurationMap = m.get(...); | |||
LoggingConfiguration c = new YamlLoggingConfiguration(alternativeLoggingConfigurationMap); | |||
AlternativeLoggingConfiguration.apply(c); | |||
</syntaxhighlight> | |||
=YAML Configuration= | |||
logging: | |||
file: /some/file.log | |||
loggers: | |||
- org.something: INFO | |||
- com.something.else: TRACE | |||
=Successful Implementations= | |||
* [[DataBot]] |
Latest revision as of 18:13, 26 July 2017
Internal
Overview
A pattern that consists in applying alternative log4j configuration for applications that run in background. The application rely on a base log4j configuration file, usually shipped as part of the application's installation bundle, but it can change its logging behavior based on logging configuration present in the application's configuration file. Configuration that comes from the application's configuration file takes priority over configuration stored in log4j.xml, and the application usually ends up with a combination of those two, which is the effective logging configuration.
Important! The effective logging configuration becomes active only after AlternativeLoggingConfiguration.apply(...) is invoked. Before this call, the basis log4j.xml configuration applies. For the same reason, caching log.isTraceEnabled() into a static variable may become stale after applying the configuration change, so it is not recommended.
For command-line utilities that run in foreground and want to route their all log4j logging information to stderr if -v or --verbose command line options are used, see:
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
The API to support alternative logging configuration is available in novaordis-utilities 4.7.8 or newer, and consists in a collection of static utilities AlternativeLoggingConfiguration, the LoggingConfiguration interface and other support classes.
Yaml yaml = new Yaml();
Map m = (Map)yaml.load(...);
Map alternativeLoggingConfigurationMap = m.get(...);
LoggingConfiguration c = new YamlLoggingConfiguration(alternativeLoggingConfigurationMap);
AlternativeLoggingConfiguration.apply(c);
YAML Configuration
logging: file: /some/file.log loggers: - org.something: INFO - com.something.else: TRACE