Log4j 2
  1. Log4j 2
  2. LOG4J2-468

Dynamically (programmatically) add logger in Log4j2

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      Please add the ability for someone to dynamically add a new Logger programmatically.

      Thank you.

        Activity

        Hide
        Girfas added a comment -

        Hi Ralph, Can you give me the link to this fix on log4j2? I am a bit quite new to this forum.

        Show
        Girfas added a comment - Hi Ralph, Can you give me the link to this fix on log4j2? I am a bit quite new to this forum.
        Hide
        Ralph Goers added a comment -

        Fixed in revision 1572705. Please verify and close.

        Show
        Ralph Goers added a comment - Fixed in revision 1572705. Please verify and close.
        Hide
        Ralph Goers added a comment -

        Sure, but only after it is implemented

        Show
        Ralph Goers added a comment - Sure, but only after it is implemented
        Hide
        Remko Popma added a comment -

        Ralph,
        would it be an idea to add this code snippet to the FAQ?
        Also, programmatically adding appenders is another frequently asked question (and the answer is non-trivial as it entails changing the configuration and calling context.start(newConfig) if I remember correctly).

        These would need the appropriate warnings - not the public API, may break with subsequent releases, etc - but we do get this question often... Thoughts?

        Show
        Remko Popma added a comment - Ralph, would it be an idea to add this code snippet to the FAQ? Also, programmatically adding appenders is another frequently asked question (and the answer is non-trivial as it entails changing the configuration and calling context.start(newConfig) if I remember correctly). These would need the appropriate warnings - not the public API, may break with subsequent releases, etc - but we do get this question often... Thoughts?
        Hide
        Ralph Goers added a comment -

        You would do something like:

        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        config.addLogger("com.mycorp.myproduct.mypackage", Level.INFO, (Filter) null);
        

        Note that anything added programmatically will be lost if a reconfiguration occurs. In that case you are better off extending XMLConfiguration to add your own customizations.

        Show
        Ralph Goers added a comment - You would do something like: LoggerContext ctx = (LoggerContext) LogManager.getContext( false ); Configuration config = ctx.getConfiguration(); config.addLogger( "com.mycorp.myproduct.mypackage" , Level.INFO, (Filter) null ); Note that anything added programmatically will be lost if a reconfiguration occurs. In that case you are better off extending XMLConfiguration to add your own customizations.
        Hide
        Andre Bogus added a comment -

        What would the runtime semantics for "adding a new Logger" be?

        • Getting a Logger object from the LogManager - You can already do that, just call LogManager.getLogger(). No new feature here.
        • Setting the log level for a logger at runtime: You may want to look into the JMX extensions, which I haven't tested yet, but the docs promise to allow things like setting log levels.
        • Adding ore redirecting an appender. This will need a property in your configuration, along with a routing appender and calling reconfigure. For example:
        <?xml version="1.0" encoding="UTF-8" ?>
        <configuration status="warn">
        	<properties>
        		<property name="filename">$${sys:LOG_FILENAME}</property>
        	</properties>
        	<appenders>
                    <Routing name="routing">
        		<Routes pattern="$${ctx:zwangsgruppe}">
        			<Route>
        				<File name="file" fileName="${filename}">
        					<PatternLayout pattern="..."/>
        				</File>
        			</Route>
        		</Routes>
        	    </Routing>
        	</appenders>
               ...
        </configuration>
        

        (Yeah, it's ugly) Then in the code, you can do:

        System.setProperty("LOG_FILENAME", logFilename);
        ((org.apache.logging.log4j.core.LoggerContext)LogManager.getContext()).reconfigure();

        Not very nice either, but it should work.

        Show
        Andre Bogus added a comment - What would the runtime semantics for "adding a new Logger" be? Getting a Logger object from the LogManager - You can already do that, just call LogManager.getLogger(). No new feature here. Setting the log level for a logger at runtime: You may want to look into the JMX extensions, which I haven't tested yet, but the docs promise to allow things like setting log levels. Adding ore redirecting an appender. This will need a property in your configuration, along with a routing appender and calling reconfigure. For example: <?xml version="1.0" encoding="UTF-8" ?> <configuration status="warn"> <properties> <property name="filename">$${sys:LOG_FILENAME}</property> </properties> <appenders> <Routing name="routing"> <Routes pattern="$${ctx:zwangsgruppe}"> <Route> <File name="file" fileName="${filename}"> <PatternLayout pattern="..."/> </File> </Route> </Routes> </Routing> </appenders> ... </configuration> (Yeah, it's ugly) Then in the code, you can do: System.setProperty("LOG_FILENAME", logFilename); ((org.apache.logging.log4j.core.LoggerContext)LogManager.getContext()).reconfigure(); Not very nice either, but it should work.

          People

          • Assignee:
            Ralph Goers
            Reporter:
            Dean Anderson
          • Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development