logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bart S. (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LOG4J2-952) FAQ: How do I configure log4j2 programmatically in code without a configuration file?
Date Tue, 18 Aug 2015 14:19:46 GMT

    [ https://issues.apache.org/jira/browse/LOG4J2-952?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14701319#comment-14701319
] 

Bart S. commented on LOG4J2-952:
--------------------------------

That is like really verbose! Oeph!.

But I get the idea. However it is not much less verbose than the current method?....

Essentially, the pattern is:

- create a top level assember
- create sublevel assemblers that are already connected to the toplevel assembler
- assemble the sublevels and add them to the toplevel
- finally assemble the toplevel.

It requires a lot of local variables. And hence a lot of imports. It is completely compositing
in nature; that is cool. You could think to refactor it in this way:

{code}assembler.addLogger(
    assembler.getLoggerAssembler("name")
        .addAppenderRef("Console")
        .assemble()
){code}

Then you can chain that:

{code}assembler.addAppender(
    assembler.getAppenderAssembler("name")
        .addLayout( ..... )
        .assemble()
).addLogger(
    assembler.getLoggerAssembler("name")
        .addAppenderRef("Console")
        .assemble()
).assemble();{code}

Then you'd have the exact same functionality in a fluent layout, without requiring the local
variables unless you want to use an appender (for example) twice. It also means you don't
need to reference any classes, as the Assembler itself will produce new instances of those
classes for you (with {{getLoggerAssembler()}}. This will also void the requirement to constantly
pass 'assembler' as a parameter to those objects/methods.

This may be the best solution we have thus far. It is a compositing framework, but all the
assemblers return actual objects, right?. They are then not much different than the existing
Builders of certain classes, in fact they do the same thing. The difference is you do not
directly add them to a configuration, but that happens with the final assemble call.

> FAQ: How do I configure log4j2 programmatically in code without a configuration file?
> -------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-952
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-952
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: API, Configurators, Documentation
>    Affects Versions: 2.1
>            Reporter: Joe Merten
>
> I found [this link|http://logging.apache.org/log4j/2.x/faq.html#config_from_code] which
said:
> {quote}
> You could use the static method #initialize(String contextName, ClassLoader loader, String
configLocation) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for
the class loader.) Be aware that this class is not part of the public API so your code may
break with any minor release.
> {quote}
> This documentation is unclear because it points to a member function which needs a filename
{{configLocation}} where as the topic is ┬╗without a configuration file┬ź.
> It shoud rather point to the member function {{org.apache.logging.log4j.core.config.Configurator.initialize(ClassLoader
loader, ConfigurationSource source)}}.
> Example:
> {code:java}
> import org.apache.logging.log4j.core.config.ConfigurationSource;
> import org.apache.logging.log4j.core.config.Configurator;
> final String hardCodedXmlConfig =
>         "<?xml version='1.0' encoding='UTF-8'?>\n" +
>         "<Configuration status='INFO'>\n" +
>         "  <Appenders>\n" +
>         "    <Console name='Console' target='SYSTEM_OUT'>\n" +
>         "      <PatternLayout pattern='%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
- %msg%n'/>\n" +
>         "    </Console>\n" +
>         "  </Appenders>\n" +
>         "  <Loggers>\n" +
>         "    <Root level='debug'>\n" +
>         "      <AppenderRef ref='Console'/>\n" +
>         "    </Root>\n" +
>         "  </Loggers>\n" +
>         "</Configuration>\n";
> try {
>     Configurator.initialize(null, new ConfigurationSource(new ByteArrayInputStream(hardCodedXmlConfig.getBytes())));
> } catch (IOException e) {
>     e.printStackTrace();
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message