Matt's suggestion works. You can make a routing appender that will switch 
between appenders under program control. Example of something that switches 
between two different ConsoleAppenders

Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" packages="org.wegscd">
    <Appenders>
        <Routing name="AB">
            <Routes pattern="${ablookup:ab}">
                <Route key="A">
                    <Console name="ConsoleA">
                        <PatternLayout pattern="ConsoleA %msg%n"/>
                    </Console>
                </Route>
                <Route key="B">
                    <Console name="ConsoleB">
                        <PatternLayout pattern="ConsoleB %msg%n"/>
                    </Console>
                </Route>
            </Routes>
        </Routing>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="AB"/>
        </Root>
    </Loggers>
</Configuration>
Component to do the ablookup:
package org.wegscd;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name = "ablookup", category = StrLookup.CATEGORY)
public class ABLookup implements StrLookup {
    static String ab;
    public ABLookup() {
        System.out.println ("ABLookup instantiated");
    }
    @Override
    public String lookup(String key) {
        if (key.equalsIgnoreCase("ab")) {
            return ab;
        }
        return null;
    }

    @Override
    public String lookup(LogEvent event, String key) {
        return lookup(key);
    }

    public static void setAB(String _ab) {
        ab = _ab;
    }
}

Test Program:  import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wegscd.ABLookup;

public class MainAB {
    static Logger logger;

    public static void main(String[] args) {
        System.setProperty("log4j2.configurationFile", "ab.xml");

        logger = LoggerFactory.getLogger(MainAB.class);

        logger.warn ("Start (goes nowhere, since ab is not set");

        ABLookup.setAB("A");
        logger.info("this should go to ConsoleA");

        ABLookup.setAB("B");
        logger.info("this should go to ConsoleB");

        logger.warn ("Done");
    }
}

Output
 ABLookup instantiated
ConsoleA this should go to ConsoleA
ConsoleB this should go to ConsoleB
ConsoleB Done




    On Wednesday, March 25, 2020, 11:03:48 AM EDT, Matt Sicker 
<boa...@gmail.com> wrote:  
 
 Take a look at this:
https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender

This appender is extremely flexible for that use case. I'd recommend
avoiding the use of an inline script (JavaScript or Groovy) unless
absolutely necessary as that's likely the slowest routing mechanism
(though you could always benchmark that).

On Wed, 25 Mar 2020 at 09:58, EDMONDO SENA <edse...@gmail.com> wrote:
>
>
> 1)  Do the external appenders come and go, or do we know ahead of time if 
> they are available?
> No, come and go!
>
> 2)  Would a RoutingAppender() that knows if external or internal logging is 
> to be done and then routes messages to the appropriate appender a solution?
>
> More or less yes.
>
>
> On 2020/03/25 14:53:08, Doug Wegscheid <dwegsch...@sbcglobal.net> wrote:
> >  ah. a couple of solutions present themself (and hopefully someone more 
> >conversant with log4j4 can chime in).
> > Would a RoutingAppender() that knows if external or internal logging is to 
> > be done and then routes messages to the appropriate appender a solution?
> > Do the external appenders come and go, or do we know ahead of time if they 
> > are available?
> >
> >
> >
> >    On Wednesday, March 25, 2020, 10:41:33 AM EDT, EDMONDO SENA 
> ><edse...@gmail.com> wrote:
> >
> >  In order to disable local logging when the external logging is enabled.
> > So, remove all local appenders and disable local logging for re-enabling 
> > the delivery towards another system to run-time.
> >
> >
> > On 2020/03/25 14:29:26, Doug Wegscheid <dwegsch...@sbcglobal.net> wrote:
> > >  upon examination, I see that clearAppenders() is not public, so that 
> > >won't work.
> > > There is probably a way to do what you want, if you can tell us why you 
> > > need to clear the appenders.
> > >
> > >    On Wednesday, March 25, 2020, 10:23:38 AM EDT, EDMONDO SENA 
> > ><edse...@gmail.com> wrote:
> > >
> > >
> > > Can you explain me how can i do it?
> > >
> > > Regards.
> > >
> > >
> > >
> > >
> > > On 2020/03/25 14:18:03, Doug Wegscheid <dwegsch...@sbcglobal.net> wrote:
> > > >  you are trying to clear the list of appenderRefs. Are you trying to 
> > > >call clearAppenders() instead of clear()?
> > > >
> > > >    On Wednesday, March 25, 2020, 10:03:27 AM EDT, EDMONDO SENA 
> > > ><edse...@gmail.com> wrote:
> > > >
> > > >
> > > > Why this instruction goes in UnsupportedOperationException?
> > > >
> > > > List<AppenderRef> value = loggerConfig.getAppenderRefs();
> > > > value.clear();
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> > > > For additional commands, e-mail: log4j-user-h...@logging.apache.org
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> > > For additional commands, e-mail: log4j-user-h...@logging.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> > For additional commands, e-mail: log4j-user-h...@logging.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>


-- 
Matt Sicker <boa...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

  

Reply via email to