简体   繁体   中英

Multiple 1st-party jar files and multiple log4j2 configurations under a web application

If you have multiple 1st-party log4j2 configurations, say in src/main/resources. How can you setup your logging configurations to be separate from each other and so they don't conflict.

My thought process right now is to have two different file configurations and not define the ROOT node and specify the different packages.

I have also tried having two different conf files. log4j2.xml and log4j2-test.xml but this would only work for two libraries. Does log4j2-myapp.xml work? The documentation says use a System property log4j.file= but this wouldn't work because I want both configuration files. Also, sometimes we don't have control over the system properties for this web container.

How can I solve the problem of multiple log4j2 configurations.

Log4j2 version, java8, websphere container, multiple 1stparty jars.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Properties>
        <Property name="logserverdir">defaultvm2</Property>
    </Properties>
    <!-- Log4j2 configuration, use with greylog -->
    <Appenders>        
        <RollingFile name="MainApiCommonsAppenderApi" fileName="/usr/local/pfs/logs/defaultvm2/log4j_api.log"           
                filePattern="/usr/local/pfs/logs/defaultvm2/log4j_api.%i.log">
            <PatternLayout pattern="%d [%t] -- [%X{mdcLogSessionId} - %-5p - %C{1}.%M(%F:%L) - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="140MB" />
            </Policies>
            <DefaultRolloverStrategy max="5" />     
        </RollingFile>          
    </Appenders>
    <Loggers>
        <Logger name="com.primerica.pfn" level="info" additivity="false">
                <AppenderRef ref="MainApiCommonsAppenderApi" />
                <AppenderRef ref="GraylogCommons" /> 
        </Logger>     
        <Logger name="org.springframework" level="debug" additivity="false">
                <AppenderRef ref="MainApiCommonsAppenderApi" />
                <AppenderRef ref="GraylogCommons" /> 
        </Logger>          
    </Loggers>
</Configuration>

From the comments, this was the best answer:

"You might consider using composite configuration. In general my thought is that an application as a whole should define its logging configuration not individual libraries. The library does not know how it is being used, the application does know how it uses the library. Maybe application A uses classes R and S and wants to see detailed messages from them while application B uses classes X and Y and needs to log messages from those"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM