Is there any reference for creating routing appender programmatically ?

On Thu, Oct 19, 2023, 9:35 AM Ralph Goers <ralph.go...@dslextreme.com>
wrote:

> Why are you doing it this way? Can you not just use the RoutingAppender?
> https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender
>
> Ralph
>
> > On Oct 18, 2023, at 8:50 PM, Ganesh S <ganesh123.g...@gmail.com> wrote:
> >
> > Hello,
> > I'm trying to create different logger with different hostname and port
> for
> > different client requests to the server.
> > If a new client request comes, I will store the information in the thread
> > local object and based on the information I need to append a new logger
> > object to the existing configuration object using CompositeConfiguration
> > and update loggers in the logger context.
> > I'm using the name of the logger object not to have a package level
> > hierarchy but just as a named (client name) logger.
> >
> > Currently I'm getting the below error when I try to append new syslog
> using
> > CompositeConfiguration
> > at the line
> > logContext.reconfigure(compositeConfiguration);
> >
> > ERROR StatusConsoleListener Appender references must contain a reference
> > ERROR StatusConsoleListener Null object returned for AppenderRef in
> Logger.
> > ERROR StatusConsoleListener No appender name provided
> > ERROR StatusConsoleListener Could not create plugin of type class
> > org.apache.logging.log4j.core.appender.SyslogAppender for element Syslog
> > org.apache.logging.log4j.core.config.ConfigurationException: Arguments
> > given for element Syslog are invalid: field 'name' has invalid value
> 'null'
> > at
> >
> org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.injectFields(PluginBuilder.java:199)
> > .....
> > at
> >
> org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
> > at
> >
> org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:715)
> > ERROR StatusConsoleListener Null object returned for Syslog in Appenders.
> >
> >
> > Below is my implementation
> >
> > ConfigurationBuilder<BuiltConfiguration> builder1 =
> > ConfigurationBuilderFactory.newConfigurationBuilder();
> >    AppenderComponentBuilder syslogAppender1 =
> > builder1.newAppender("syslog", "Syslog")
> >        .addAttribute("protocol", "UDP")
> >        .addAttribute("host", hostname)
> >        .addAttribute("port", port1)
> >        .addAttribute("appName", "serviceName")
> >        .addAttribute("id", "serviceName")
> >        .addAttribute("facility", "LOCAL0")
> >        .addAttribute("newLine", true)
> >        .addAttribute("format", "RFC5424")
> >        .addAttribute("ignoreExceptions", false);
> > syslogAppender1.add(builder1.newFilter("ThresholdFilter",
> > Filter.Result.ACCEPT, Filter.Result.DENY)
> > .addAttribute("level", Level.DEBUG));
> > builder1.add(syslogAppender1);
> >
> > builder1.add(builder1.newLogger("client1", Level.DEBUG)
> >               .add(builder1.newAppenderRef("syslog"))
> >               .addAttribute("additivity", false));
> > Configurator.reconfigure(builder1.build());
> >
> > Logger logger = LogManager.getLogger("client1");
> > logger.info("client1 message1");
> >
> > ConfigurationBuilder<BuiltConfiguration> builder =
> > ConfigurationBuilderFactory.newConfigurationBuilder();
> > AppenderComponentBuilder syslogAppender = builder.newAppender("syslog1",
> > "Syslog")
> >        .addAttribute("protocol", "UDP")
> >        .addAttribute("host", hostname)
> >        .addAttribute("port", port2)
> >        .addAttribute("appName", "serviceName")
> >        .addAttribute("id", "serviceName")
> >        .addAttribute("facility", "LOCAL0")
> >        .addAttribute("newLine", true)
> >        .addAttribute("format", "RFC5424")
> >        .addAttribute("ignoreExceptions", false);
> >
> > syslogAppender.add(builder.newFilter("ThresholdFilter",
> > Filter.Result.ACCEPT, Filter.Result.DENY)
> >        .addAttribute("level", Level.DEBUG));
> > builder.add(syslogAppender);
> >
> > builder.add(builder.newLogger("client2", Level.DEBUG)
> >               .add(builder.newAppenderRef("syslog1"))
> >               .addAttribute("additivity", false));
> >
> > LoggerContext logContext = LoggerContext.getContext(false);
> > org.apache.logging.log4j.core.config.Configuration configurationcore =
> > logContext.getConfiguration();
> > List<AbstractConfiguration> configurationList = new ArrayList<>();
> > if (configurationcore != null)
> > configurationList.add((AbstractConfiguration) configurationcore);
> > org.apache.logging.log4j.core.config.Configuration newconfig =
> > builder.build();
> > configurationList.add((AbstractConfiguration) newconfig);
> > CompositeConfiguration compositeConfiguration = new
> > CompositeConfiguration(configurationList);
> > logContext.reconfigure(compositeConfiguration);
> > logContext.updateLoggers();
> >
> > Logger logger1 = LogManager.getLogger("client2");
> > logger1.info("client2 message1");
> > logger1.info("client2 message2");
> > logger.info("client1 message2");
> > logger1.info("client2 message3");
> > logger.info("client1 message3");
> >
> >
> > Thank you,
> > Ganesh S
>
>

Reply via email to