I got it working as per my requirement, well almost. I get an NPE when I
try to set the additional fields for the JsonLayout but works when I don't
set the them. I went through the source code, I think I am doing it right
but may be not.
KeyValuePair.Builder kb1 = new
KeyValuePair.Builder().setKey("compact").setValue("true");
KeyValuePair.Builder kb2 = new
KeyValuePair.Builder().setKey("complete").setValue("false");
KeyValuePair[] additionalFields = new
KeyValuePair[]{kb1.build(),kb2.build()};
System.out.println(additionalFields.length);
JsonLayout.Builder jb = new
JsonLayout.Builder().setAdditionalFields(additionalFields); <-- This breaks
//JsonLayout.Builder jb = new JsonLayout.Builder(); <-- This works.
FileAppender.Builder fp = new
FileAppender.Builder().withFileName("/tmp/xpose.json.log").withAppend(true);
fp.setName("DEMO");
fp.setLayout(jb.build());
FileAppender fps = fp.build();
fps.start();
addAppender(fps);
#### Relevant part of the NPE ####
2019-12-16 10:06:57,518 org.rio.lohit.Main.main() ERROR An exception
occurred processing Appender DEMO java.lang.NullPointerException
at
org.apache.logging.log4j.core.layout.AbstractJacksonLayout.resolveAdditionalFields(AbstractJacksonLayout.java:301)
at
org.apache.logging.log4j.core.layout.AbstractJacksonLayout.wrapLogEvent(AbstractJacksonLayout.java:286)
at
org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:319)
at
org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:291)
at
org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:274)
at
org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:68)
at
org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:51)
at
org.apache.logging.log4j.core.layout.AbstractStringLayout.toByteArray(AbstractStringLayout.java:302)
at
org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:210)
at
org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:37)
###########################
Thanks
Lohith
On Fri, Dec 13, 2019 at 12:27 PM Lohith BK <lohit.b@gmail.com> wrote:
> 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
>>
>>
|