I'm trying to convert some Legacy code to OOP, and I'm wondering how to deal with Logging.
I have a Main routine, which establishes a Log file.
Then I have some classes to instantiate, and code in those classes would like to write to the Log file.
The two choices I see are:
1. Pass something about the log file to the classes as a parameter
2. Have a global somewhere which everyone tries to write to
The first option leads to endlessly passing a log file parameter around.
The second option makes the code dependent on external environment. (The code is no longer encapsulated, loosely coupled.)
What's the third option I'm missing?
To further complicate the issue, I'm trying to merge 8 nearly identical but subtly different copies of the code (yes over time they made 8 different versions). My idea is to move the duplicate parts into a common Library.
I've examined other projects. I see one which uses a static Logger class (all the methods are static). It looks like I might be able to reuse it.
The other thing I would like is to print log messages both to the console and also to the log file, so somewhere I need a tee function. Where would such a function belong? The current Logging module I'm looking at appears to use the Strategy pattern, so you can log to a file, OR to the console, but not both.
How would I convert this to have the option to send log messages to more than one place, without making a bunch of modifications to existing code (the Open/Closed principle)?