We all know about log, ok, but why should we consider the «Logger» class a singleton one? What happens if we make it as a normal non-singleton class?
相关问题
- suppress a singleton constructor in java with powe
- I want to trace logs using a Macro multi parameter
- Error message 'No handlers could be found for
- convert logback.xml to log4j.properties
- Django management command doesn't show logging
相关文章
- how do I log requests and responses for debugging
- Android Studio doesn't display logs by package
- How to fix a purported lack of an “explicit instan
- Stacktrace does not print in Glassfish 4.1 Cluster
- Auto-property initializer Singleton implementation
- Out of curiosity — why don't logging APIs impl
- Laravel log file based on date
- Java -How to get logger to work in shutdown hook?
If you have more than one log streams with different content, you can use multiple instances of the logger class initialized for the different outputs.
However, if you have only one log stream, having multiple logger class instances leads to more complex implementation, as the instances have to work together to manage the actual resource. Consider for example a logger that logs each message with a sequence number. Two instances will have to synchronize their sequence counters, which requires them to knwp about each other, negotiate counter increases and so on. (The alternative of having shared counter in a static class member is equivalent to having a singleton logger)
The main problem is where the actual log is persisted.
If you are writing on a filesystem, having more than one instance (and therefore, probably, more than one thread) may result in a garbled file.
In the sense that depending on buffering and other low-level mechanisms messages from one write may end up mixed with messages (or parts of messages) from others.
This may be a minor problem, but it's the only one I can think of regarding having just one (and therefore serial) log writing object.
Depends on the logging framework. Usually you want all messages to go to one log, so you want all code to use the same logger. But the logger-class does not have to be a singleton to ensure that.
I found this here on the IBM site. It explains the usage of a Logger Singleton class quite well.
Here the link: Use your singletons wisely
If you wouldn't use a singleton class you would have to deal with the synchronisation (writing to a file, or whatever stream you use) between these different logger instances. So its much easier, when you just have one global Logger instance.