Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-129

RoutingAppender dynamic appender creation only creates 1



    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.0-beta3
    • 2.0-beta4
    • Appenders
    • None


      I was looking through the new appenders and found RoutingAppender which intrigued me


      From the documentation it seems that the RoutingAppender would create a new appender dynamically every time the key didn't match an existing appender name. However when testing this I find it only creates the first dynamically and then reuses that one as it is set to default. That doesn't seem what I would expect. My example code is as the following:

      public class TestRouting {
          public static void main(String[] args) {
              ThreadContext.put("filename", "foo.bar");
              ThreadContext.put("filename", "foo.bar2");
          private final static Logger _logger = LogManager.getLogger(TestRouting.class);

      Here is the log4j2 xml file it picks up.

      <?xml version="1.0" encoding="UTF-8"?>
          <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
          <Routing name="RoutingAppender">
            <Routes pattern="${ctx:filename}">
                <RollingFile name="Rolling-${ctx:filename}" fileName="${ctx:filename}"
                    <pattern>%d %p %C{1.} [%t] %m%n</pattern>
                  <SizeBasedTriggeringPolicy size="500" />
              <!-- By having this set to ${ctx:filename} it will match when filename
                   is not set in the context -->
              <Route ref="Console" key="${ctx:filename}"/>
          <root level="info">
            <appender-ref ref="RoutingAppender"/>

      When I run the test though it only creates 1 file/appender with the name foo.bar and logs both messages to it. I would expect that it creates 2 files where each gets the different message.

      Is there something I am missing about this or is this the intended behavior?

      I have a change that gets it working, however part of it is hack and would need to be updated to the best alternative. Also I had to change BaseConfiguration slightly to make a defensive copy of the attributes. It seemed that was fine, otherwise the Route cannot be reused to make another appender if it blows away the attributes, don't know if that will affect other things though.


        1. LOG4J2-129-RoutingAppender.patch
          8 kB
          Ingo Feltes
        2. LOG4J2-129-RoutingAppenderTest.patch
          1 kB
          Ingo Feltes
        3. patch
          6 kB
          William Burns



            rgoers Ralph Goers
            rpwburns William Burns
            0 Vote for this issue
            4 Start watching this issue