You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@logging.apache.org by Volkan Yazıcı <vo...@yazi.ci> on 2021/12/03 21:23:18 UTC
Programmatically creating layout from configuration source
Given a JSON snippet for configuring a layout, how can I instantiate the
layout class itself? I have tried the following, which "runs" without a
failure, but couldn't access to a layout:
LoggerContext context = (LoggerContext) LogManager.getContext();
ConfigurationSource configSource =
new ConfigurationSource(
new ByteArrayInputStream(
("{'PatternLayout': {'MarkerPatternSelector': {" +
"'defaultPattern': '%m%n', " +
"'PatternMatch': {'key': 'FLOW',
'pattern': '===%m===%n'}" +
"}}}")
.replaceAll("'", "\"")
.getBytes(StandardCharsets.UTF_8)));
JsonConfiguration configuration = new JsonConfiguration(context, configSource);
Eventually I want to obtain a Layout<?> instance. Any ideas?
Re: Programmatically creating layout from configuration source
Posted by Matt Sicker <bo...@gmail.com>.
I do not, no, though that sounds like the default configuration you’re looking at?
Matt Sicker
> On Dec 3, 2021, at 16:36, Volkan Yazıcı <vo...@yazi.ci> wrote:
>
> I think I have pulled it out: after doing a configuration.start(),
> configuration.getAppenders().values().iterator().next().getLayout() does
> the trick. Even though I did not specify an appender, the configuration
> creates a default ConsoleAppender using the layout I have provided. Matt,
> do you know the magic going on here?
>
>> On Fri, Dec 3, 2021 at 11:24 PM Matt Sicker <bo...@gmail.com> wrote:
>>
>> I don't think that's possible in the current plugin system without
>> copying a bunch of code from AbstractConfiguration or similar. The
>> code for translating configurations into objects works starting from
>> the root configuration object. Ideally, the next gen plugin system
>> will be able to support something along the lines of what you're
>> asking for, though (i.e., being more of a generic way of configuring
>> beans rather than being purely tied to a log4j configuration).
>>
>>> On Fri, Dec 3, 2021 at 3:23 PM Volkan Yazıcı <vo...@yazi.ci> wrote:
>>>
>>> Given a JSON snippet for configuring a layout, how can I instantiate the
>>> layout class itself? I have tried the following, which "runs" without a
>>> failure, but couldn't access to a layout:
>>>
>>> LoggerContext context = (LoggerContext) LogManager.getContext();
>>> ConfigurationSource configSource =
>>> new ConfigurationSource(
>>> new ByteArrayInputStream(
>>> ("{'PatternLayout': {'MarkerPatternSelector': {"
>> +
>>> "'defaultPattern': '%m%n', " +
>>> "'PatternMatch': {'key': 'FLOW',
>>> 'pattern': '===%m===%n'}" +
>>> "}}}")
>>> .replaceAll("'", "\"")
>>> .getBytes(StandardCharsets.UTF_8)));
>>> JsonConfiguration configuration = new JsonConfiguration(context,
>> configSource);
>>>
>>> Eventually I want to obtain a Layout<?> instance. Any ideas?
>>
Re: Programmatically creating layout from configuration source
Posted by Ralph Goers <ra...@dslextreme.com>.
The easy way to tell what is going on is to set log4j2.debug=true before starting the application.
You will see that Log4j has a default configuration that is created (see LoggerContext where the
Configuration reference is set to it. If reconfiguration fails then the default configuration will
continue to be used.
Ralph
> On Dec 3, 2021, at 3:35 PM, Volkan Yazıcı <vo...@yazi.ci> wrote:
>
> I think I have pulled it out: after doing a configuration.start(),
> configuration.getAppenders().values().iterator().next().getLayout() does
> the trick. Even though I did not specify an appender, the configuration
> creates a default ConsoleAppender using the layout I have provided. Matt,
> do you know the magic going on here?
>
> On Fri, Dec 3, 2021 at 11:24 PM Matt Sicker <bo...@gmail.com> wrote:
>
>> I don't think that's possible in the current plugin system without
>> copying a bunch of code from AbstractConfiguration or similar. The
>> code for translating configurations into objects works starting from
>> the root configuration object. Ideally, the next gen plugin system
>> will be able to support something along the lines of what you're
>> asking for, though (i.e., being more of a generic way of configuring
>> beans rather than being purely tied to a log4j configuration).
>>
>> On Fri, Dec 3, 2021 at 3:23 PM Volkan Yazıcı <vo...@yazi.ci> wrote:
>>>
>>> Given a JSON snippet for configuring a layout, how can I instantiate the
>>> layout class itself? I have tried the following, which "runs" without a
>>> failure, but couldn't access to a layout:
>>>
>>> LoggerContext context = (LoggerContext) LogManager.getContext();
>>> ConfigurationSource configSource =
>>> new ConfigurationSource(
>>> new ByteArrayInputStream(
>>> ("{'PatternLayout': {'MarkerPatternSelector': {"
>> +
>>> "'defaultPattern': '%m%n', " +
>>> "'PatternMatch': {'key': 'FLOW',
>>> 'pattern': '===%m===%n'}" +
>>> "}}}")
>>> .replaceAll("'", "\"")
>>> .getBytes(StandardCharsets.UTF_8)));
>>> JsonConfiguration configuration = new JsonConfiguration(context,
>> configSource);
>>>
>>> Eventually I want to obtain a Layout<?> instance. Any ideas?
>>
Re: Programmatically creating layout from configuration source
Posted by Volkan Yazıcı <vo...@yazi.ci>.
I think I have pulled it out: after doing a configuration.start(),
configuration.getAppenders().values().iterator().next().getLayout() does
the trick. Even though I did not specify an appender, the configuration
creates a default ConsoleAppender using the layout I have provided. Matt,
do you know the magic going on here?
On Fri, Dec 3, 2021 at 11:24 PM Matt Sicker <bo...@gmail.com> wrote:
> I don't think that's possible in the current plugin system without
> copying a bunch of code from AbstractConfiguration or similar. The
> code for translating configurations into objects works starting from
> the root configuration object. Ideally, the next gen plugin system
> will be able to support something along the lines of what you're
> asking for, though (i.e., being more of a generic way of configuring
> beans rather than being purely tied to a log4j configuration).
>
> On Fri, Dec 3, 2021 at 3:23 PM Volkan Yazıcı <vo...@yazi.ci> wrote:
> >
> > Given a JSON snippet for configuring a layout, how can I instantiate the
> > layout class itself? I have tried the following, which "runs" without a
> > failure, but couldn't access to a layout:
> >
> > LoggerContext context = (LoggerContext) LogManager.getContext();
> > ConfigurationSource configSource =
> > new ConfigurationSource(
> > new ByteArrayInputStream(
> > ("{'PatternLayout': {'MarkerPatternSelector': {"
> +
> > "'defaultPattern': '%m%n', " +
> > "'PatternMatch': {'key': 'FLOW',
> > 'pattern': '===%m===%n'}" +
> > "}}}")
> > .replaceAll("'", "\"")
> > .getBytes(StandardCharsets.UTF_8)));
> > JsonConfiguration configuration = new JsonConfiguration(context,
> configSource);
> >
> > Eventually I want to obtain a Layout<?> instance. Any ideas?
>
Re: Programmatically creating layout from configuration source
Posted by Matt Sicker <bo...@gmail.com>.
I don't think that's possible in the current plugin system without
copying a bunch of code from AbstractConfiguration or similar. The
code for translating configurations into objects works starting from
the root configuration object. Ideally, the next gen plugin system
will be able to support something along the lines of what you're
asking for, though (i.e., being more of a generic way of configuring
beans rather than being purely tied to a log4j configuration).
On Fri, Dec 3, 2021 at 3:23 PM Volkan Yazıcı <vo...@yazi.ci> wrote:
>
> Given a JSON snippet for configuring a layout, how can I instantiate the
> layout class itself? I have tried the following, which "runs" without a
> failure, but couldn't access to a layout:
>
> LoggerContext context = (LoggerContext) LogManager.getContext();
> ConfigurationSource configSource =
> new ConfigurationSource(
> new ByteArrayInputStream(
> ("{'PatternLayout': {'MarkerPatternSelector': {" +
> "'defaultPattern': '%m%n', " +
> "'PatternMatch': {'key': 'FLOW',
> 'pattern': '===%m===%n'}" +
> "}}}")
> .replaceAll("'", "\"")
> .getBytes(StandardCharsets.UTF_8)));
> JsonConfiguration configuration = new JsonConfiguration(context, configSource);
>
> Eventually I want to obtain a Layout<?> instance. Any ideas?