Open JosephHobbs opened 4 months ago
Why not reuse the same logger?
from dynatrace_extension.sdk.extension import extension_logger as logger
logger.info("Hello world")
I'd argue against reusing the same logger for a couple of reasons...
I'm happy to update/submit the PR if this makes sense. I just didn't want to make a foundational change like this and submit it without at least having this conversation. :-)
Won't your idea affect all the 200+ existing extensions? Adding log lines from third party dependencies without the developer explicitly asking for it?
I agree that importing the logger from dynatrace_extension
is not a good idea, your modules should not depend on dynatrace_extension
What I think you should have though is a way for your modules to receive a logging.Logger
object, ie in the constructor of a class, so that you can pass the extension self.logger
to your module if you so desire
This is done extensively by many third party libraries and extensions
Won't your idea affect all the 200+ existing extensions? Adding log lines from third party dependencies without the developer explicitly asking for it?
No. This does not change the root logger's level at all... All it does is give loggers a handler that actually writes logs somewhere when that specific logger is enabled...
I agree that importing the logger from
dynatrace_extension
is not a good idea, your modules should not depend ondynatrace_extension
What I think you should have though is a way for your modules to receive a
logging.Logger
object, ie in the constructor of a class, so that you can pass the extensionself.logger
to your module if you so desireThis is done extensively by many third party libraries and extensions
This is definitely a workaround I could use for my own custom module, but it's still not the greatest option. It means I can only manage the logging level of the primary logger versus being able to control log levels for my extension and submodule independently. All logs from my submodule will be logged as if reside in the primary extension module. It also means I'll still NOT get any logging data from any 3rd party module I enable debug for...
Won't your idea affect all the 200+ existing extensions? Adding log lines from third party dependencies without the developer explicitly asking for it?
No. This does not change the root logger's level at all... All it does is give loggers a handler that actually writes logs somewhere when that specific logger is enabled...
I didn't mean that this changes the root logger level.
What I am saying is that other libraries writing to the root logger will now be logged to the extension log
We don't want to automatically start logging from other libraries, even if they write info level, this is a change to too many extensions at once
This proposal will not cause that. Python's default logging level is WARN. The only loggers you are currently setting to INFO are the extension and api loggers.
If even WARN is a concern, we could increase the root logger to CRITICAL to keep all but the most critical items from being logged. This would abate much of the 'sudden new logs' risk while also providing extensions a fully functioning logger implementation.
If it helps, one of the guiding principals of python logging PEP-282 is specifically avoid having to pass around loggers.
:-)
Is your feature request related to a problem? Please describe.
The current extension logging initialization only adds handlers to the 'dynatrace_extension.sdk.extension' and 'api' loggers. Due to this, other modules do not get these appenders, which prevents logs from being written for any logger outside of these two. This means that any logging I do in my own modules does not show up by default.
Describe the solution you'd like
I would love to see these appenders attached to the ROOT logger. This will ensure that all loggers would send their logs to these appenders as opposed to just the two built-in loggers. So for example, changing...
to something like this...
Describe alternatives you've considered
At this point, my workaround/alternative is to duplicate your logging code within my own init.py. This (at least) initializes the logger for my extension (module) and would apply to that module down...