logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lohith BK <lohi...@gmail.com>
Subject Re: Configuration File and programmatic configuration not working
Date Fri, 13 Dec 2019 20:27:26 GMT
Thanks, I suspected the same, however I assumed that
"ConfigurationBuilderFactory.newConfigurationBuilder();" is just a
convenient way of creating components and adding to the current config.
Let me try what you suggested, will update the thread.

On Fri, Dec 13, 2019 at 12:10 PM Ralph Goers <ralph.goers@dslextreme.com>
wrote:

> OK. I see that I should have looked at the code in your configuration
> class more closely.  By the time doConfigure() is called the Configuration
> object has already been created. You are creating a ConfigurationBuilder
> and building a new Configuration. But the configuration you are building
> won’t be saved anywhere nor will it be merged with the Configuration your
> class is extending.
>
> If you look at the example, it accesses the configuration objects that
> have been built, creates new ones, and adds them to the current
> Configuration class. In other words, you shouldn’t be using
> ConfigurationBuilder to modify an existing configuration. Use the Builders
> or factory classes of the components you want to add and directly add them
> to your Configuration.
>
> Ralph
>
> > On Dec 13, 2019, at 12:55 PM, Lohith BK <lohit.b@gmail.com> wrote:
> >
> > Yes I did. XmlConfigurationFactory has an order value of 5.
> >
> > @Plugin(
> > name="confplugin",
> > category = "ConfigurationFactory")
> > @Order(6)
> >
> > public class CustomConfigurationFactory extends ConfigurationFactory{
> >
> >    @Override
> >    public Configuration getConfiguration(final LoggerContext
> > loggercontext, final ConfigurationSource confsource){
> >        return new CustomConfiguration(loggercontext,confsource);
> >    }
> >
> >    @Override
> >    public String[] getSupportedTypes(){
> >        return new String[] {"*",".xml"};
> >    }
> > }
> >
> > On Fri, Dec 13, 2019 at 11:35 AM Ralph Goers <ralph.goers@dslextreme.com
> >
> > wrote:
> >
> >> Did you also create a CustomConfigurationFactory with an Order value
> that
> >> is larger than the value in XmlConfigurationFactory?
> >>
> >> Ralph
> >>
> >>> On Dec 13, 2019, at 11:46 AM, Lohith BK <lohit.b@gmail.com> wrote:
> >>>
> >>> Greetings,
> >>>
> >>> I have been trying to get the programmatic configuration working (on
> top
> >> of
> >>> the file based configuration), but it doesn't seem to merge them
> >> together.
> >>> Here is the meat of the plugin code.
> >>> /tmp/xpose.log gets created, and also the output from the programmatic
> >>> configuration /tmp/customconfig.xml gets created and looks as expected.
> >>> However when I try to get the configured logger in the code it appears
> >> use
> >>> the ones inherited from the root logger. Also, the example code in the
> >>> documentation doesn't seem to reflect the newer builder API.
> >>>
> >>> https://logging.apache.org/log4j/2.x/manual/customconfig.html
> >>>
> >>>
> >>> class CustomConfiguration extends XmlConfiguration{
> >>>   public CustomConfiguration(final LoggerContext loggercontext, final
> >>> ConfigurationSource confsource){
> >>>       super(loggercontext,confsource);
> >>>   }
> >>>   @Override
> >>>   protected void doConfigure(){
> >>>       super.doConfigure();
> >>>
> >>>       ConfigurationBuilder<BuiltConfiguration> builder =
> >>> ConfigurationBuilderFactory.newConfigurationBuilder();
> >>>       AppenderComponentBuilder apbuilder =
> >>>
> >>
> builder.newAppender("XPOSE","File").addAttribute("fileName","/tmp/xpose.log");
> >>>       builder.add(apbuilder);
> >>>       LoggerComponentBuilder lgbuilder =
> >>>
> >>
> builder.newLogger("customlogger",Level.INFO).addAttribute("additivity","false");
> >>>       lgbuilder.add(builder.newAppenderRef("XPOSE"));
> >>>       builder.add(lgbuilder);
> >>>
> >>>       try{
> >>>           File opt = new File("/tmp/customconfig.xml");
> >>>           FileOutputStream fop = new FileOutputStream(opt);
> >>>           builder.writeXmlConfiguration(fop);
> >>>           builder.build();
> >>>       }catch(FileNotFoundException e){
> >>>           System.out.println(e.getMessage());
> >>>       }catch(IOException e){
> >>>           System.out.println(e.getMessage());
> >>>       }
> >>>   }
> >>> }
> >>>
> >>> Output of programmatic configuration.
> >>>
> >>> <?xml version="1.0" ?>
> >>> <Configuration>
> >>> <Appenders>
> >>>   <File name="XPOSE" fileName="/tmp/xpose.log"/>
> >>> </Appenders>
> >>> <Loggers>
> >>>   <Logger name="customlogger" level="INFO" additivity="false">
> >>>     <AppenderRef ref="XPOSE"/>
> >>>   </Logger>
> >>> </Loggers>
> >>> </Configuration>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> >> For additional commands, e-mail: log4j-user-help@logging.apache.org
> >>
> >>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

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