Let's say I have four legacy jars:
- my-library.jar
- my-app.jar
- my-other-app.jar
- log4j.jar
"My App" and "My Other App" are unrelated applications, both with main() functions. They both use various library functions from "my-library-app". All three do logging through log4j (really slf4j, but I just want to keep the example simple).
Currently, the two applications are set up with two different log4j config files, which cause them to log to two different files.
Now I want to convert everything to OSGi. So I bundle up the first three each as a separate bundle, convert the main() of the actual apps to Activators, and either bundle up or find an existing bundle of log4j. I start both apps in the same OSGi framework.
But now the two distinct apps no longer log to different files! Right? There's only one instance of log4j running in the JVM, and it gets its configuration out of one single log4j.properties file.
So maybe instead of bundling each of my four jars separately, I make three bundles:
- My Library
- My App plus log4j
- My Other App plus log4j
Now I can get different logging config files for the two different apps. But what about log calls from My Library? The My Library bundle will latch onto one of the two copies of log4j, and now all log messages produced from My Library will come out in one specific one of the two log files - let's say the one for My App. But that's true even if it's a log message from My Library due to a call from My Other App! They'll go to the wrong log file.
So maybe bundle:
- My Library plus log4j
- My App plus log4j
- My Other App plus log4j
Now the log messages from My Library are going to their own log file, which I guess is better than some of them going to the wrong app's log file, but still, it's not great. That file has log messages from both apps, and neither of the log files intended to be for either app have all log messages from those apps.
So maybe bundle:
- My App plus My Library plus log4j
- My Other App plus My Library plus log4j
But now what's the point of OSGi? I'm not sharing use of My Library or of log4j. And in reality it will likely be even worse - there will be a bunch of jars that I have to stick multiple copies of into all of my actual app bundles, simply because I want to see their log messages associated with the app that caused them.
So maybe back up and try something different: I don't think this is possible in log4j, but in (say) slf4j I could go back to the original bundle plan:
- My Library
- My App
- My Other App
- log4j
And then I do something like put MDC information in each thread saying what app the thread is from. React to that MDC info to determine what log file it goes into.
But it seems like that won't work either! A call from some thread in My App to some function in My Library could cause a new thread to be spawned from My Library, which would not necessarily be associated with that MDC.
And it's worse than that: My Library could have some thread that is shared by any app that uses My Library, so cannot possibly be associated with some such marker.
So, all in all, I'm stumped. Any suggestions would be greatly appreciated. Thanks in advance.