Repository: logging-log4j2 Updated Branches: refs/heads/master 794c46911 -> 155dfc739
Added 3 questions to FAQ: * How do I reconfigure log4j2 in code with a specific configuration file? * How do I change a logger's level in code? * How do I shut down log4j2 in code? Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2d731112 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2d731112 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2d731112 Branch: refs/heads/master Commit: 2d7311127d5dbc41a7b9a6ea95206aa13324a5d7 Parents: 3d92506 Author: rpopma <[email protected]> Authored: Thu Jun 18 00:20:51 2015 +0900 Committer: rpopma <[email protected]> Committed: Thu Jun 18 00:20:51 2015 +0900 ---------------------------------------------------------------------- src/site/xdoc/faq.xml | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2d731112/src/site/xdoc/faq.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/faq.xml b/src/site/xdoc/faq.xml index a078c00..37bea76 100644 --- a/src/site/xdoc/faq.xml +++ b/src/site/xdoc/faq.xml @@ -28,6 +28,9 @@ <li><a href="#which_jars">Which JAR files do I need?</a></li> <li><a href="#config_location">How do I specify the configuration file location?</a></li> <li><a href="#config_from_code">How do I configure log4j2 in code without a configuration file?</a></li> + <li><a href="#reconfig_from_code">How do I reconfigure log4j2 in code with a specific configuration file?</a></li> + <li><a href="#set_logger_level_from_code">How do I change a logger's level in code?</a></li> + <li><a href="#shutdown">How do I shut down log4j2 in code?</a></li> <li><a href="#config_sep_appender_level">How do I send log messages with different levels to different appenders?</a></li> <li><a href="#troubleshooting">How do I debug my configuration?</a></li> <li><a href="#separate_log_files">How do I dynamically write to separate log files?</a></li> @@ -66,6 +69,60 @@ (You can pass null for the class loader.) Be aware that this class is not part of the public API so your code may break with any minor release.</p> + <a name="reconfig_from_code" /> + <h4>How do I reconfigure log4j2 in code with a specific configuration file?</h4> + <p>See the below example. + Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release.</p> + <pre class="prettyprint linenums">// import org.apache.logging.log4j.core.LoggerContext; + +LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false); +File file = new File("path/to/a/different/log4j2.xml"); + +// this will force a reconfiguration +context.setConfigLocation(file.toURI()); +</pre> + + <a name="set_logger_level_from_code" /> + <h4>How do I change a logger's level in code?</h4> + <p>See the below example. + Be aware that these classes are not part of the public API so your code may break with any minor release.</p> + <pre class="prettyprint linenums">// import org.apache.logging.log4j.core.LoggerContext; +// import org.apache.logging.log4j.core.config.Configuration; +// import org.apache.logging.log4j.core.config.LoggerConfig; + +LoggerContext context = (LoggerContext) LogManager.getContext(false); +Configuration config = context.getConfiguration(); +LoggerConfig rootConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); +rootConfig.setLevel(Level.DEBUG); + +// You could also specify the actual logger name as below +// and it will return the LoggerConfig used by the Logger. +LoggerConfig loggerConfig = config.getLoggerConfig("com.apache.test"); +loggerConfig.setLevel(Level.TRACE); + +// This causes all Loggers to refetch information from their LoggerConfig. +context.updateLoggers(); +</pre> + + <a name="shutdown" /> + <h4>How do I shut down log4j2 in code?</h4> + <p>Normally there is no need to do this manually. + Each LoggerContext registers a shutdown hook that takes care of releasing resources + when the JVM exits (unless system property <code>log4j.shutdownHookEnabled</code> + is set to <code>false</code>). + Web applications should include the log4j-web + module in their classpath which disables the shutdown hook but instead + cleans up log4j resources when the web application is stopped.</p> + <p>However, if you need to manually shut down log4j, you can do so + as in the below example. + Be aware that these classes are not part of the public API so your code may break with any minor release.</p> + <pre class="prettyprint linenums">// import org.apache.logging.log4j.core.LoggerContext; +// import org.apache.logging.log4j.core.config.Configurator; + +// get the current context +LoggerContext context = (LoggerContext) LogManager.getContext(); +Configurator.shutdown(context);</pre> + <a name="config_sep_appender_level" /> <h4>How do I send log messages with different levels to different appenders?</h4> You don't need to declare separate loggers to achieve this.
