Closed Zeitsperre closed 2 years ago
If loguru makes are logging easier it would be nice. It has also something to handle in multiprocessing which we might want to use in pywps.
I'm a bit worried about the transition of the codes and the activation of the loguru logging.
I wasn't going to plunge into the depths of asynchronous logging, but yes it looks as though there is robust support for safely-threaded logging in loguru. I'm leaving this here as an example, and perhaps we can talk about this change in our next meeting.
Logging in general is something that I would like us to make more use of on the Ouranos side. @huard mentioned that we could try deploying this on some Ouranos projects first to get a sense of its behaviour. I'll find an afternoon to give that a try.
@Zeitsperre, this looks great. Let me know when it is ready to merge.
Hey Ag, there are a few final changes to this that I want to make, based on the work done in https://github.com/Ouranosinc/xclim/pull/1041. The biggest breaking change is that the default behaviour for logging should be set to 'disabled'; The running process/user should be responsible for enabling logging:
import sys
from loguru import logger
logger.enable("clisops")
logger.add(sys.stdout, level="INFO||DEBUG||etc.")
This whole process should also be documented for clarity as well, so I'll add something similar based on the other PR too. It should be finished today!
@agstephens This PR is now fully functioning for me!
Hi @Zeitsperre , could you please fix the conflicts and then we can merge this one as well.
Thanks @Zeitsperre, I have merged this in now.
Pull Request Checklist:
[x] This PR addresses an already opened issue (for bug fixes / features)
[x] Tests for the changes have been added (for bug fixes / features)
[x] Documentation has been added / updated (for bug fixes / features)
[x] HISTORY.rst has been updated (with summary of main changes)
[x] I have added my relevant user information to
AUTHORS.md
What kind of change does this PR introduce?:
This reimplements logging in CLISOPS using Loguru, an alternative logging library that is virtually identical by way of API but much more streamlined in its configuration.
The current logging setup initializes
Handlers
on import, which are then propagated to upstream libraries. This causes issues upstream when outputs tostdout
andstderr
can falsely cause build failures on CI and ReadTheDocs in other libraries and applications (see note about creating Handlers in libraries: https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library).The changes presented here ~disable logging~ remove the rootlogger for CLISOPS by default, allowing for logging to be explicitly set for the library using a function (
clisops.utils.common.enable_logging()
), or by setting a handler in themain()
loop of a running process using theloguru
'slogger
class (e.g.from loguru import logger; logger.add(sink=sys.stdout)
).Yes. Logging is no longer dependent on the standard
logging
library. In order to enable logging for CLISOPS, the called process must enable logging withclisops.utils.common.enable_logging()
(This can be moved somewhere/renamed if needed). This will create the handlers for CLISOPS to output tostdout
, stderr` (and elsewhere, if we want).Since there are pytests relying on
caplog
andloguru
does not write to the standard logging handler, I used a pytest plugin that passes messages from loguru tocaplog
. For tests that examine the logger, there is no difference beyond ensuring that logging is enabled for clisops (i.e.logger.enable("clisops")
)