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

Gary Gregory commented on LOG4J2-952:
-------------------------------------

{quote}
As I said previously, I didn't use "Builder" because I thought it gave the 
wrong impression. I would expect the build() method of a Builder to return the 
actual object.
{quote}

But now, the test shows:
{java:code}
    public Configuration getConfiguration(final String name, final URI 
configLocation) {
        ConfigurationAssembler<AssembledConfiguration> assembler = 
newConfiguration();
        assembler.setStatusLevel(Level.ERROR);
        assembler.add(assembler.newFilter("ThresholdFilter", 
Filter.Result.ACCEPT, Filter.Result.NEUTRAL)
                .addAttribute("level", Level.DEBUG));
        AppenderAssembler appenderAssembler = assembler.newAppender("Stdout", 
"CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
        appenderAssembler.add(assembler.newLayout("PatternLayout").
                addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
        appenderAssembler.add(assembler.newFilter("MarkerFilter", 
Filter.Result.DENY,
                Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
        assembler.add(appenderAssembler);
        assembler.add(assembler.newLogger("org.apache.logging.log4j", 
Level.DEBUG).
                add(assembler.newAppenderRef("Stdout")).
                addAttribute("additivity", false));
        
assembler.add(assembler.newRootLogger(Level.ERROR).add(assembler.newAppenderRef("Stdout")));
        return assembler.assemble();
    }
{code}

and the only call to assembler does return the actual object. The other 
assemble() calls are internal granted.

And how is the assemble method documented? By using the "builder" terms! ;-)

{code:java}
    /**
     * Builds the plugin object after all configuration has been set. This will 
use default values for any
     * unspecified attributes for the plugin.
     *
     * @return the configured plugin instance.
     * @throws org.apache.logging.log4j.core.config.ConfigurationException if 
there was an error building the plugin
     * object.
     */
    T assemble();
{code}

;-)

> 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
>         Attachments: LOG4J2-952-2.patch, LOG4J2-952-3.patch, 
> LOG4J2-952-4.patch, LOG4J2-952.patch
>
>
> 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]

Reply via email to