0

Thanks for trying to help the question. I have been trying to upgrade the log4j from version 2.2.x to 2.17.2 for that I have included following starters, the spring boot version is 2.6.6 -

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-logging</artifactId>
           <version>${spring.boot.version}</version>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-log4j2</artifactId>
           <version>${spring.boot.version}</version>
       </dependency>
       <dependency>
           <groupId>com.lmax</groupId>
           <artifactId>disruptor</artifactId>
           <version>3.4.4</version>
       </dependency>

but when I include the routing tag as per my simplified (& partial) logging-config as below, it throws several runtime errors -

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN" monitorInterval="30">

    <property name="log-path" value="/path/" />
    <!-- log levels precedence : ALL < DEBUG < INFO < WARN < ERROR < FATAL < 
        OFF -->
    <property name="log-level" value="INFO" />

    <Appenders>
        <appender name="console" target="SYSTEM_OUT"
            class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
                </pattern>
            </encoder>
        </appender>

        <Routing name="Routing">
            <Routes pattern="$${ctx:docURN}">
                <Route>
                    <appender name="LogFile"
                        class="ch.qos.logback.core.rolling.RollingFileAppender">
                        <file>${log-path}/filename.log</file>
                        <rollingPolicy
                            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                            <fileNamePattern>${log-path}/filename-%d{yyyy-MM-dd}.%i.log
                            </fileNamePattern>
                            <timeBasedFileNamingAndTriggeringPolicy
                                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                                <maxFileSize>100MB</maxFileSize>
                            </timeBasedFileNamingAndTriggeringPolicy>
                        </rollingPolicy>
                        <encoder>
                            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id=
                                ${ctx:docURN} - %msg%n</pattern>
                        </encoder>
                    </appender>
                </Route>
            </Routes>
        </Routing>
    </Appenders>
    <Loggers>
        <logger name="com.name" level="${log-level}" additivity="false">
            <appender-ref ref="LogFile" level="${log-level}" />
            <appender-ref ref="Routing" level="${log-level}" />
        </logger>
        <root level="info" additivity="false">
            <appender-ref ref="Routing" />
        </root>
    </Loggers>
</Configuration>

I apologize, if the question appears duplicate but I have searched all over the stackoverflow and internet and the apache documentation is so overwhelming, it seems to be correct as per the documentation. Thank you again for your help.

Edit - runtime errors thrown are for all the nodes in the logging-config.xml - java.lang.IllegalStateException: Logback configuration error detected: ERROR in ch.qos.logback.core.joran.spi.Interpreter@10:13 - no applicable action for [Appenders], current ElementPath is [[Configuration][Appenders]]

Edit2 - I tried following configuration example as it is from https://logging.apache.org/log4j/2.x/manual/configuration.html , still it throws those errors.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="RoutingTest" packages="org.apache.logging.log4j.test">
  <Properties>
    <Property name="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
  </Properties>
  <ThresholdFilter level="debug"/>
 
  <Appenders>
    <Console name="STDOUT">
      <PatternLayout pattern="%m%n"/>
      <ThresholdFilter level="debug"/>
    </Console>
    <Routing name="Routing">
      <Routes pattern="$${sd:type}">
        <Route>
          <RollingFile name="Rolling-${sd:type}" fileName="${filename}"
                       filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
            <PatternLayout>
              <pattern>%d %p %c{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <SizeBasedTriggeringPolicy size="500" />
          </RollingFile>
        </Route>
        <Route ref="STDOUT" key="Audit"/>
      </Routes>
    </Routing>
  </Appenders>
 
  <Loggers>
    <Logger name="EventLogger" level="info" additivity="false">
      <AppenderRef ref="Routing"/>
    </Logger>
 
    <Root level="error">
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
 
</Configuration>

Edit3 - This is the resultant logging-config.xml I have now -

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN" monitorInterval="30">

    <Properties>
        <property name="log-path">/logfiles//</property>
        <!-- log levels precedence : ALL < DEBUG < INFO < WARN < ERROR < FATAL 
            < OFF -->
        <property name="log-level">INFO</property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>

        <Routing name="Routing">
            <Routes pattern="$${ctx:docURN}">
                <Route>
                    <RollingFile name="LogFile" append="true"
                        fileName="${log-path}/-services.log" filePattern="${log-path}/-services-%d{yyyy-MM-dd}.log">
                        <PatternLayout>
                            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id= ${ctx:docURN} - %msg%n"></pattern>

                        </PatternLayout>
                        <Policies>
                            <TimeBasedTriggeringPolicy interval="1"
                                modulate="true" />
                        </Policies>
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
        
        <Routing name="ErrorRouting">
            <Routes pattern="$${ctx:docURN}">
                    <Route>
                        <RollingFile name="ErrorLogFile" append="true"
                            fileName="${log-path}/-services-error.log" filePattern="${log-path}/-services-error-%d{yyyy-MM-dd}.log">
                                <PatternLayout>
                                    <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} tx.id= ${ctx:docURN} - %msg%n"></pattern>
    
                                </PatternLayout>
                <Policies>
                            <TimeBasedTriggeringPolicy interval="1"
                                modulate="true" />
                        </Policies>
                    </RollingFile>
                </Route>
            </Routes>
        </Routing>
            
        <RollingFile name="ServerLogFile" fileName="${log-path}/Server.log" filePattern="${log-path}/Server-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <Pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>


    </Appenders>


    <Loggers>
        <Root level="info" additivity="false">
            <AppenderRef ref="ServerLogFile" />
            <AppenderRef ref="Routing" />
        </Root>
        <Logger name="com.name" level="${log-level}"
            additivity="false">
            <appender-ref ref="LogFile" level="${log-level}" />
            <appender-ref ref="ErrorLogFile" level="error" />
            <appender-ref ref="ServerLogFile" level="${log-level}" />
            <appender-ref ref="Routing" level="${log-level}" />
            <AppenderRef ref="ErrorRouting" level="error"/>
        </Logger>
    </Loggers>

</Configuration>
code-match
  • 21
  • 8
  • What errors are thrown? – XtremeBaumer Jul 19 '22 at 10:38
  • Thanks @XtremeBaumer, i have included the errors. – code-match Jul 19 '22 at 10:40
  • I believe you are mixing logback and log4j configurations – XtremeBaumer Jul 19 '22 at 10:51
  • Thanks for pointing out. i will try to correct it and post here. I was trying to follow this page - https://logging.apache.org/log4j/2.x/manual/configuration.html – code-match Jul 19 '22 at 11:01
  • 1
    That is definitely log4j, but `` is just not log4j – XtremeBaumer Jul 19 '22 at 11:05
  • yes, you are right. I was wrong to blindly combine maybe two or multiple solutions. but when I tried to provide the configuration from one of the examples in the documentation link above, then also it throws the same error. So I guess problem is not just in config.xml but elsewhere too? I have edited the question with the example I tried if that is the case – code-match Jul 19 '22 at 11:08
  • 1
    Might help to remove `spring-boot-starter-logging` dependency as it provides logback dependency which could clash with slf4j dependency – XtremeBaumer Jul 19 '22 at 11:13
  • Thank you. will try by commenting `spring-boot-starter-logging` and post here. – code-match Jul 19 '22 at 11:18
  • you were so right. I have corrected both - the pom.xml & logging-config.xml and no longer getting those annoying errors. But I see there's a message in the logs - `main ERROR Unable to locate appender "LogFile" for logger config "com.name" ` . So I guess its still not able to find the appender? – code-match Jul 19 '22 at 11:40
  • Moreover the docURN is still not printed in the logs (which is different issue altogether though) as logs it like below - ```tx.id= ${ctx:docURN}` . I have provided it as in the config xml - ` ``` – code-match Jul 19 '22 at 11:43
  • Update the configuration to what you have right now. I am not seeing any reference to `LogFile`. – XtremeBaumer Jul 19 '22 at 11:51
  • Sorry, edited the question with the latest logging configuration xml – code-match Jul 19 '22 at 11:59
  • and I can see the tx.id in the logs printed now after I changed `System.setProperty("log4j2.isThreadContextMapInheritable", "true");` to simply `isThreadContextMapInheritable` – code-match Jul 19 '22 at 12:03
  • `` you only ref to the `Routing`. So remove all refs to `ErrorLogFile` and `LogFile`. Also use `AppenderRef` instead of `appender-ref`. Also also, check `%msg%n">`, not sure you want the `"` there on both occurences – XtremeBaumer Jul 19 '22 at 12:07
  • Also, have a look [here](https://stackoverflow.com/questions/49188105/log4j2-isthreadcontextmapinheritable-property-usage) for the `isThreadContextMapInheritable` parameter – XtremeBaumer Jul 19 '22 at 12:09
  • Thanks @XtremeBaumer it fixed the issue. Thanks a ton for your help and sorry for delay in acknowledging the same, my laptop had issues. – code-match Jul 21 '22 at 12:10

0 Answers0