-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor logging #209
Draft
rplevka
wants to merge
1
commit into
SatelliteQE:master
Choose a base branch
from
rplevka:logzero_logger
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
refactor logging #209
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from broker.broker import Broker | ||
#from broker.broker import Broker | ||
|
||
VMBroker = Broker | ||
#VMBroker = Broker | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ class LOG_LEVEL(IntEnum): | |
WARNING = logging.WARNING | ||
ERROR = logging.ERROR | ||
|
||
|
||
class RedactingFilter(logging.Filter): | ||
"""Custom logging.Filter to redact secrets from the Dynaconf config""" | ||
def __init__(self, sensitive): | ||
|
@@ -55,12 +54,12 @@ def redact_dynaconf(self, data): | |
_sensitive = ["password", "pword", "token", "host_password"] | ||
logging.addLevelName("TRACE", LOG_LEVEL.TRACE) | ||
logzero.DEFAULT_COLORS[LOG_LEVEL.TRACE.value] = logzero.colors.Fore.MAGENTA | ||
logger_prefix = '' | ||
|
||
def patch_awx_for_verbosity(api): | ||
client = api.client | ||
awx_log = client.log | ||
|
||
awx_log.parent = logzero.logger | ||
#awx_log.parent = logzero.logger | ||
|
||
def patch(cls, name): | ||
func = getattr(cls, name) | ||
|
@@ -130,21 +129,36 @@ def set_file_logging(level=settings.logging.file_level, path="logs/broker.log"): | |
) | ||
|
||
|
||
def setup_logzero( | ||
def setup_logger( | ||
level=settings.logging.console_level, | ||
formatter=None, | ||
file_level=settings.logging.file_level, | ||
name=None, | ||
path="logs/broker.log", | ||
path=None, | ||
Comment on lines
-138
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why default to none instead of Broker's default log path? |
||
propagate=False, | ||
): | ||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | ||
patch_awx_for_verbosity(awxkit.api) | ||
set_log_level(level) | ||
set_file_logging(file_level, path) | ||
if formatter: | ||
logzero.formatter(formatter) | ||
logzero.logger.name = name or "broker" | ||
logzero.logger.addFilter(RedactingFilter(_sensitive)) | ||
|
||
|
||
setup_logzero() | ||
if formatter is None: | ||
formatter = formatter_factory(level) | ||
logger = logging.getLogger(name or 'broker') | ||
logger.propagate = propagate | ||
logger.setLevel(resolve_log_level(file_level)) | ||
logger.addFilter(RedactingFilter(_sensitive)) | ||
# cover logging to console | ||
sh = logging.StreamHandler() | ||
sh.setLevel(resolve_log_level(level)) | ||
logger.addHandler(sh) | ||
# attach awxkit upstream logger to the hierarchy | ||
awxkit.api.client.log.parent = logger | ||
|
||
# used for attaching module-based loggers to hierarchy properly | ||
global logger_prefix | ||
logger_prefix = logger.parent.name+'.' if logger.parent.name != 'root' else '' | ||
#set_log_level(level) | ||
#set_file_logging(file_level, path) | ||
#logzero.logger.name = name or "broker" | ||
# logzero.logger.addFilter(RedactingFilter(_sensitive)) | ||
|
||
def init_logger(module): | ||
return logging.getLogger(f'{logger_prefix}{module}') | ||
# setup_logzero() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why comment these out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question.
I have checked out SatelliteQE/robottelo#1332 and installed broker from this PR/branch and I reverted the patch in this
__init__.py
file. I put breakpoint into both logger setup functions in broker and here is the call stack.We can see that broker's logger gets initialized during the import time. That is not ideal, we want to initialize broker logger once
setup_logger
is called, not when it is imported.from broker.logger import setup_logger as broker_log_setup
import importsfrom broker.broker import Broker
which importsfrom broker.hosts import Host
which callslogger = init_logger(__name__)
that initializes the logger.Do you have any idea what could be a different approach to avoid setting the logger up during the import time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's reframe this a bit.
Why try to avoid the initial logger setup?
What is gained from finding a way around it?
What is lost from user experience by implementing workarounds?
It's not always easy to do this in a way that satisfies what @rplevka wants here while maintaining a nice API and reducing waste. One potential option is for Broker to use its own logging wrapper that lazily sets up the logging interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi.
My primary motivation was to redesign broker logging in a way, so it can fully give away control over its setup to library that uses it (robottelo).
I was looking to setup different handlers (that would log to their own files) for different pytest objects - especially fixtures.
In pytest, it is possible to control this from the appropriate hook functions, however, these are being entered quite late in the process (by that time, the broker logger is self-initialized by mere module import - thus, it generates unwanted loggers and log files that are outside of robottelo control.
I don't have a strong opinion about how this is supposed to work, so I am opened to some ideas, but this design is something I came to while trying to solve a problem of a proper structured logging (to know what logs belong to what part of the session) for purposes of log parsing and pushing towards reporting platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to import sequences, this isn't something that we can do easily in Python. In my opinion, it's not a big enough concern to justify changes like this. If Broker creates a log file and potentially a few logs before Robottelo redirects its logger, that's not a big deal. Please revert changes like the ones here.