Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specific logs for multiple war having one ear approach

Tags:

java

log4j2

specific logs for multiple war having one ear .How to configure the same using log4j2.xml as i am not able to find any much help in this.i have 5 wars having one ear and when i configure the logs all war logs are printing to only one log file which got loaded first by the class loader.i want my log file to be separate out for each war in single ear.any help would be appreciated.

like image 881
TheFInder Avatar asked Oct 19 '22 16:10

TheFInder


1 Answers

configuration xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <RollingFile name="app4war1" fileName="/logs/war1/app.log" filePattern="/logs/war1/app-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <TimeBasedTriggeringPolicy />
        </RollingFile>
        <RollingFile name="app4war2" fileName="/logs/war2/app.log" filePattern="/logs/war2/app-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <TimeBasedTriggeringPolicy />
        </RollingFile>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="log4war1" level="trace">
            <AppenderRef ref="app4war1" />
        </Logger>
        <Logger name="log4war2" level="trace">
            <AppenderRef ref="app4war2" />
        </Logger>
        <Root level="error">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

and in your web applications use your own implementaion of LogManager.

package myloggermanagerimpl;

import org.apache.logging.log4j.Logger;

public class LogManager {

    private static final String prefix = "log4war1.";

    public static Logger getLogger(){
        return org.apache.logging.log4j.LogManager.getLogger(prefix);
    }

    public static Logger getLogger(Class<?> clazz){
        return org.apache.logging.log4j.LogManager.getLogger(prefix + clazz.getName());
    }

    // and other getLogger() methods in the same manner

}

now, you should implement this class in each of your web projects changing prefix for each. then refactor your projects to change import org.apache.logging.log4j.LogManager; to import myloggermanagerimpl.LogManager;

like image 169
guleryuz Avatar answered Nov 15 '22 05:11

guleryuz