[
https://issues.apache.org/jira/browse/LOG4J2-952?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14695195#comment-14695195
]
Bart S. commented on LOG4J2-952:
--------------------------------
The problem is that it would no longer be a builder (not sure if that's a
problem though....).
The current builder implementation of Behrooz Nobakht (sorry if I don't
remember your name ;-)) saves string references and can validate them when the
configuration is built. So even though the compiler won't notice, if a string
that is referenced that should have been registered but isn't, the thing can
just throw an error or exception. (Currently though, for instance if you
request a loggerconfig that doesn't exist from the core API, it will just
return the loggerconfig for Root. If you reference a level that doesn't exist,
it will just return DEBUG. Etc, idk.).
Also not sure if Configuration.ROOT would exist (whether it'd be static
reference to a LoggerConfig at this point, I don't know) -- maybe it should
just be a static field of the interface, but it couldn't really be a
loggerConfig I think.
Besides, you'd still need to import all of those classes: LoggerConfig from
(LoggerConfig has a field called ... no a class called RootLogger but it is not
a reference. You can't use that) ... from core.config, Appender from
core.appender, and so on.
In any case it wouldn't probably be hard to troubleshoot a config where you
have made a small type, and the builder would probably tell you, so it's not
that great of a problem.
> 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: [email protected]
For additional commands, e-mail: [email protected]