You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Ralph Goers (JIRA)" <ji...@apache.org> on 2015/08/24 08:12:49 UTC

[jira] [Updated] (LOG4J2-952) FAQ: How do I configure log4j2 programmatically in code without a configuration file?

     [ https://issues.apache.org/jira/browse/LOG4J2-952?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ralph Goers updated LOG4J2-952:
-------------------------------
    Attachment: LOG4J2-952.patch

I have implemented the ConfigurationAssembler and have also implemented support for properties files that is implemented using the ConfigurationAssembler. Note that the property file syntax is in no way compatible with version 1.x. A sample file looks like:
{code}
filters = Threshold

filter.Threshold.type = ThresholdFilter
filter.Threshold.level = DEBUG

appenders = Stdout

appender.Stdout.type = Console
appender.Stdout.name = StdOut
appender.Stdout.target = SYSTEM_OUT
appender.Stdout.layout.type = PatternLayout
appender.Stdout.layout.pattern = %d [%t] %-5level: %msg%n%throwable
appender.Stdout.filters = marker
appender.Stdout.filter.marker.type = MarkerFilter
appender.Stdout.filter.marker.onMatch = DENY
appender.Stdout.filter.marker.onMisMatch = NEUTRAL
appender.Stdout.filter.marker.marker = FLOW

loggers = log4j

logger.log4j.name = org.apache.logging.log4j
logger.log4j.appenderRefs = console
logger.log4j.appenderRef.console.ref = StdOut
logger.log4j.level = DEBUG
logger.log4j.additivity = false

rootLogger.appenderRefs = console
rootLogger.appenderRef.console.ref = StdOut
rootLogger.level = ERROR
{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.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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org