InjLog – Another neat Injection Logger for Java 8
by Kristian Kraljic, January 2, 2016
And This Is It my dear visitors! Six days, six posts. This makes a total of four posts in 2015 and already two posts in 2016. Great! I am very happy to have done that, so we have some time to talk about what is going to happen in 2016.
As already said on my post yesterday, I plan on releasing more stuff on this blog. But no false promises, if it is going to take until Christmas again, that’ll be it. But I’ll try my best to release some smaller snippets throughout the year. Other than that, I’m planning on something in the area of home automation, but I’ll get to that as well in one of my future posts. Last but not least, there is still a lot of work to do for my projects at the University of Applied Sciences in Mannheim and at my main workplace. So it’s not going to be boring year at all.
Coming to the last release for this streak of posts. Logging in Java is a neverending story. There are hundrets of logging frameworks out there. Some build in to Java, some smaller or bigger external libraries. What most of them share: one logger is shared for all classes logging data. My requirement was a little different. I had classes wanting to log data, but where and when to log data, should be decided by another class. Therefore I came up with the InjLog Logging Framework for Java.
InjLog utilizes a concept, I knew about from Enterprise Java Beans (EJB) called “dependency injection”. InjLog does not really dynamically inject loggers into other objects, but the concept is quite similar anyways. Any class can announce to log data, by implementing the
InjectionLogger.Injectable interface. Any logger can then be injected into any object implementing the interface, by using the
InjectionLogger.injectLogger method. The objects itself may call the
getLogger method, implemented by default from the
Injectable interface, to receive a logger and log messages. This does work, even if no logger has been injected into the object so far. With this mechanism, each object is able to use a different logger. Some objects log to a file, while other objects log to the console, etc. And with a single class and two interfaces, the framework is very leight-weight.
To find more examples, contribute to the development, or give feedback, feel free to check out the InjLog repository on GitHub at: https://github.com/kristian/injlog.