Java – logging and dependency injection
I try to build and applications based on Java
For dependency injection, I use Google Guice
Now I came up with the problem of recording some information during the application I'm not talking about general logging in the form of method calls I know AOP, I can be like method call tracing and so on
What I'm looking for is manual recording I need some methods to log in to almost every class in my application So I thought of two options:
Get the recorder by using Guice injection framework and do this to me through constructor (or setter or private...), but it feels like adding logging problems to each class and polluting my constructor > use global service locator in the method I want to call log Well, all di fans will hate me for doing this
So from a practical point of view, what is the best way?
Solution
Think again If you think you need to log in for almost every course, your design will have problems This stack overflow answer talks about what your design may be It's here Net, but the answer also applies to Java
The answer is mainly about exception logging. For non exception logs, I will say: prevent recording too much information in too many places For every message or warning to be recorded, ask first. This should not be an exception For example, don't record "we shouldn't be in this branch", but throw an exception!
Even if you want to record debugging information, will anyone read this? You'll get log files, thousands and thousands of lines, no one will read If they read them, they must go through all these text lines and do complex regular expression searches through them to get the information they are looking for
Another reason to see developers do this is to cover up their bad code It's like using opinions like this I see the developer record "we have executed this block" or "if the branch is skipped" This allows them to track code and large methods
However, we don't know how to write big methods now, not write big methods No, even smaller In addition, if you test your code alone, there is not much reason to debug the code, and you have verified that it performs what should be done
Again, good design can help here When you use the design described in the stack overflow answer (using a command handler), you can create a decorator again that can serialize any command message and record it to disk before execution begins This gives you a very accurate log Just add some context information (such as execution time and user name) to the log, and you have an audit trace that can even be used to replay commands during debugging or even loading tests
I have used this type of application design for several years, and since then, I have had little reason for additional logging in business logic It is necessary now, but these situations are rare
By doing so, you will eventually get a constructor with too many arguments But don't blame the recorder, blame your code You violated the single responsibility principle here You can "hide" this dependency through static appearance, but it does not reduce the number of dependencies and the overall complexity of the class
Finally, you will hate yourself because each class still has an additional dependency (a good hidden dependency in this case) This makes each class more complex and will force you to have more code: more code to test, more code with bugs, more maintenance code