You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Simone Tripodi <si...@apache.org> on 2012/08/08 09:25:59 UTC
[2.X] Programmatic configuration
Hi all guys,
I have been maybe lazy on that, but I didn't figure out how to provide
a configuration to log4j programmatically, without using a textual
representation.
How to obtain a simple configuration which reflects the XML one on [1]?
Many thanks in advance, all the best!
-Simo
[1] https://gist.github.com/3283384
http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: [2.X] Programmatic configuration
Posted by Ralph Goers <ra...@dslextreme.com>.
There are several unit tests that you can look at. SocketServerTest is doing a fair amount. Of course, if you also have a configuration file and it is modified you are going to lose any of the programmatic configuration you have done. To get the root logger just do ctx.getLogger("").
However, the best way to do configuration is really to implement your own ConfigurationFactory and a Configuration class that extends BaseConfiguration. BaseConfiguration basically is expecting a node tree that matches the XML or JSON config but you can use anything you want to create the node hierarchy. Once it is constructed BaseConfiguration will take care of the rest. ConfigurationFactories are Plugins and they specify an Order annotation that identifies their precedence relative to other factories. Note that XMLConfigurationFactory really needs to be last as it uses a wildcard file extension.
Hopefully it is obvious that if you programmatically configure then at least some part of your application is tied to the Log4j 2 implementation, not just the API.
One piece I have not implemented is the JMX support. My expectation is that it is also going to expose a way to modify the configuration.
Ralph
On Aug 8, 2012, at 12:25 AM, Simone Tripodi wrote:
> Hi all guys,
>
> I have been maybe lazy on that, but I didn't figure out how to provide
> a configuration to log4j programmatically, without using a textual
> representation.
>
> How to obtain a simple configuration which reflects the XML one on [1]?
>
> Many thanks in advance, all the best!
> -Simo
>
> [1] https://gist.github.com/3283384
>
> http://people.apache.org/~simonetripodi/
> http://simonetripodi.livejournal.com/
> http://twitter.com/simonetripodi
> http://www.99soft.org/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org