logging: redirect closed streams to stderr/stdout (#40423)
This has the additional benefit of making the initial logger respect
changes to redirect_stderr/stdout, until the user explicitly sets
another stream as the logging destination.
Fix #26798
Fix #38482
Replaces #26920, which provided the idea and most of the implementation
Co-authored-by: Joe Petviashvili <joe@gnom.us>