[
https://issues.apache.org/jira/browse/LOG4J2-952?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14695397#comment-14695397
]
Bart S. commented on LOG4J2-952:
--------------------------------
{quote}
Having some extra imports is not a big deal since all reasonable IDEs will help
you doing them. Forcing compile time dependency on the log4j-core module would
be an issue though (if that could be avoided otherwise).
{quote}
That's not an excuse. If you have an IDE that will automate everything for you
then you don't need an abstraction layer / builder thing. If you as well are
going to have to store references and thus create local variables, then the
amount of code reduction will be very very minimal. With adequate builder
classes in the objects themselves, constructing the required objects is trivial
really:
{code}conf.addAppender(
org.apache.logging.log4j.core.appender.ConsoleAppender.newBuilder().
setName("ConsoleOwn").
setLayout(
org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
withPattern(myPattern).
withCharset(java.nio.charset.Charset.forName("UTF-8")).
withConfiguration(conf).
build()).
build()
);
{code}
But even that is too arduous for my tastes. Just to avoid having to store
objects, I would do:
{code}conf.getLoggerConfig("root").addAppender(conf.getAppender("ConsoleOwn"),
null, null);{code}
So you see I'm already using 'string literals'. I just want it to be faster,
less verbose. And of course my tastes matter ;-). that's why I'm doing this :).
> 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]