For the past couple weeks, I have been trying on and off to find an elegant solution to this problem. So far, searching on the Internet for the answer hasn't produced anything fruitful.
What I would like to do is initialize log4j so that:
1) the root log has a specific Appender defined by myself
2) a specific log, say "foo.bar.whatever", has another Appender also defined by me, and whose additivity is false
The appenders are encapsulated in their own bundle called, say, "my.appenders". Moreover, I would like to have a situation where additional bundles can state that they want to use log4j in this initialized state. I wish to minimize the amount of work a bundle writer needs to do to get a properly initialized Logger. Ideally, a method in a bundle should be able to use Logger.getLogger().
Here are the ways I could envision this working:
I) Use Spring DM to inject an instance of LoggerFactory that returns instances of Logger preconfigured with the appropriate appender and settings. I find this solution cumbersome, because every object that requires a Logger must inject a LoggerFactory, something which isn't as easy as Logger.getLogger(). Moreover, Loggers can't be used in static methods.
I am aware of more OSGi-suited APIs, like OSP4J Logging, but I didn't find any way to express a configuration before other bundles use logging. Also, I found that injecting Loggers in OSP4J required an unnecessary amount of work, as it requires one to access the BundleContext just to get a logger. I feel that this approach suffices without the need for OSP4J.
II) Have a start method for the my.appenders bundle that appropriately initializes log4j. Bundles that use log4j can specify somewhere that they wish to have OSGi start my.appenders before loading themselves. Thus, methods can use Logger.getLogger().
I think this way is reasonable, but I have no idea how to tell Spring DM to (a) specify a start method for my.appenders, and (b) how to specify that my.appenders should be initialized before loading a bundle. (The only way to ensure a bundle is loaded is to import one of its beans. However, in this case, my.appenders isn't exporting any beans.) Could this approach be done? Is there a better way to do this?
What is the suggested means to do this?