logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tungathurthi, Chandra Kiran Bharadwaj" <chandra.tungathur...@rwth-aachen.de>
Subject Log4j2.7 - Adding same appender to multiple loggers?
Date Mon, 10 Oct 2016 09:00:04 GMT
Hi users,


I have been stuck with a problem eating way my time and energy.

My goal: create one generic appender with parameters which can be used for multiple loggers
and the parameters to it will be furnished during runtime. Thus, creating a "dynamic" appender
which spans through multiple loggers.


This is my

<Routing name="defaultAppender">
    <Routes>
        <Script name="RoutingInit" language="JavaScript">
            <![CDATA[
            if (logEvent.getLevel().intLevel() >= 400) {
                "info-logs";
            } else {
                "error-logs";
            }
            ]]>
        </Script>
        <Route key="info-logs">
            <RollingRandomAccessFile name="rrafAppenderDsp-${ctx:file}"
                                     fileName="${ctx:dir}/${ctx:file}.log"
                                     filePattern="${ctx:gzip-dir}/${ctx:file}_%d{dd-MM-yyyy}_%i.log.gz"
                                     append="true" immediateFlush="false"
                                     bufferSize="${ctx:bufferSize}">
                <PatternLayout>
                    <pattern>${ctx:pattern}</pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="${ctx:rollover-filesize}"/>
                </Policies>
                <DefaultRolloverStrategy compressionLevel="${ctx:gzip-compression}">
                    <Delete basePath="${ctx:gzip-dir}">
                        <IfFileName glob="*/${ctx:file}_*.log.gz"/>
                        <IfLastModified age="${ctx:gzip-purgetime}"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingRandomAccessFile>
        </Route>
        <Route key="error-logs">
            <RollingRandomAccessFile name="rrafAppenderDsp"
                                     fileName="${ctx:dir}/${ctx:file}_errors.log"
                                     filePattern="${ctx:gzip-dir}/${ctx:file}_errors_%d{dd-MM-yyyy}_%i.log.gz"
                                     append="true" immediateFlush="false"
                                     bufferSize="${ctx:bufferSize}">
                <PatternLayout>
                    <pattern>${root.linePattern}</pattern>
                </PatternLayout>
                <Policies>
                    <SizeBasedTriggeringPolicy size="${ctx:rollover-filesize}"/>
                </Policies>
                <DefaultRolloverStrategy compressionLevel="${ctx:gzip-compression}">
                    <Delete basePath="${ctx:gzip-dir}">
                        <IfFileName glob="*/${ctx:file}_errors_*.log.gz"/>
                        <IfLastModified age="${ctx:gzip-purgetime}"/>
                    </Delete>
                </DefaultRolloverStrategy>
            </RollingRandomAccessFile>
        </Route>
    </Routes>
</Routing>

<Logger name="logger1" level="INFO" additivity="false">
    <AppenderRef ref="defaultAppender"/>
</Logger>
<Logger name="logger2" level="INFO" additivity="false">
    <AppenderRef ref="defaultAppender"/>
</Logger>
<Logger name="logger3" level="INFO" additivity="false">
    <AppenderRef ref="defaultAppender"/>
</Logger>?


the contextual params are filled as follows


// this called by each logger while logging

public void log(Level logingLevel, String data) {

// this loads the ?contextual params into ThreadContext

   loadPropertiesToContext();


   logger.log(logingLevel, data);

    ThreadContext.clearMap();

}?



Now, the problem is, the same settings works fine for one logger , but as soon as l use the
same appender for multiple loggers, the data is being mixed up and if logger1 has to written
in file1 and logger2 to file2, both the logevents are being written in file1. As I imagined,
routing appender uses current threadcontext to dynamically 'route' the events, the above settings
should work.


Has anyone had the same experience? or would like to pitch in on how to make this work ?


Cheers!

Chandra?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message