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 Wed, 12 Aug 2015 21:19:45 GMT

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

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

You could have an API like:

addAppender("name", TYPE, parameter) where parameter could be a vararg and the function under
the hood adds the relevent type.

addAppenderTo("name", "target") where target could be a LoggerContext or anything else the
way it works in this system.
addFilter("name", ...., ......)
addFilterTo("name", "target"). If your system has unique names (you could introduce that)
for each identifier, then this single generic method could add both filters to appenders and
any other things that it might need to be added to.

What you get is a "ref" based system that almost mirrors the XML file. For example, my current
project's logger configuration would be:

addAppender("console", CONSOLE, pattern);
addConfig("server", TRACE, true);
addConfig("telnet", DEBUG, true);
addConfig("telnet.file", WARN, true);
addAppenderTo("console", "root");
addAppender("optionsLog", FILEAPPEND, pattern);
addAppenderTo("optionsLog", "telnet.file");

That's my entire configuration. Done in 7 lines instead of 28 + 18 for a helper method. Most,
many of those lines are builder methods though. You could also imagine these methods interfacing
with actual builder methods. Which is what I was getting to: if you can create the objects
direclty?.

Ah never mind, I don't know how to do that cleanly. The classes themselves have builders (some
of them) that you can use to complete the options. If you have an

public Builder addSyslogAppender(String, String, int, String, String, String, int, String);
        
then that kinda defeats the idea of having a builder ;-). You'd have to have a builder for
that as well:

addSyslogAppender("name", SyslogAppenderOptions.newBuilder().host("xxx.yyy.zz").port(2001).build());

alternatively

builder.addSyslogAppender("name", builder.syslog().host("").port(xx).build());

or

builder.addSyslogAppender("name").host("").port(xx);

in the latter case this method would return a different kind of builder (SyslogAppenderOptions.Builder)
that doesn't have a finalizer method (like build()) and in which the methods simply update
the object that has already been registered in other words you'd have a Map<String, SyslogAppenderOptions>
and the first method (addSyslogAppender) would create the SyslogAppenderOptions.Builder, put
it in the Map, return the object, and subsequent calls on that object fill in further details
without any completion checking taking place at that point and at that time.

Completion checking would happen when the generated spec is transformed into a configuration.
This is an alternative way of doing builder stuff called a "fluent API" or a "fluid API".


builder.syslog("name").host("").port(xx);

would probably be the smallest version of doing it ;-). Anyway. Time to sleep almost I guess.

> 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