-
Notifications
You must be signed in to change notification settings - Fork 402
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
powertools logger set as child has no append_keys method #1016
Comments
hey @whardier - thanks for flagging it. That's indeed expected. Are you calling If you must do that, then it's best to omit Let me know if that suffices. If it does, we can improve the docs. Thank you! |
hey @whardier following up to remind you to share that snippet you added on Slack about the workaround when no handlers are set. This will help us to track this and reserve time to test for potential hierarchy bugs -- if we don't find any, we should incorporate into the source code :) |
Current WIP and some context surrounding our subclass of Logger: See area where child is tested and set to false. import logging
import sys
from typing import Optional
from aws_lambda_powertools.logging import Logger
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
from es_lambda_powertools.service.exceptions.identifiable import IdentifiableException
from es_lambda_powertools.service.logging.formatters.datadog import DatadogLoggerFormatter
from es_lambda_powertools.service.settings import ServiceSettings
class ServiceLogger(Logger):
def __init__(self, child: bool = False):
service_settings = ServiceSettings()
level: Optional[str] = None
if service_settings.log_level:
level = service_settings.log_level.name
logger_formatter: Optional[LambdaPowertoolsFormatter] = None
if service_settings.is_logger_formatter_type_datadog:
logger_formatter = DatadogLoggerFormatter()
if child is True:
# NOTE(SRS): If we want to be a child.. which seems nice some days.. but there are no parent handlers.. we
# need to self parent. This brings in a handler if one doesn't exist already so that things like
# .append_keys will work.
if len(logging.Logger.root.handlers) == 0:
child = False
super().__init__(
level=level,
child=child,
logger_formatter=logger_formatter,
utc=True,
)
def exception(self, msg, *args, exc_info=True, **kwargs): # type: ignore
exception: Optional[BaseException] = None
if exc_info:
if isinstance(exc_info, BaseException):
exception = exc_info
elif not isinstance(exc_info, tuple):
exception = sys.exc_info()[1]
if exception:
exception_ids = list(IdentifiableException.generate_exception_ids_from_exception(exception))
if exception_ids:
kwargs.setdefault("extra", {})["exception_ids"] = exception_ids
super().exception(msg, *args, exc_info=exc_info, **kwargs) |
@whardier quick update: I'm gonna tackle this (create tests etc) and all related logger issues this week. |
hey @whardier ! I looked into this today and I'm slightly inclined to raise an exception and improve the documentation instead. I'll ponder it some more. Solving like the snippet above ends up leading to two other issues:
Because we're dealing with state and it only occurs with an orphan child logger when |
I've had another look today and couldn't find a solution :(. I had updated the docs but didn't update this issue to inform that they were For the sake of transparency, I'll close this as unsuccessful and keep |
|
Chicken/egg problem with parent logger needing to be instantiated before child logger is able to utilize common methods like append_keys.
What were you trying to accomplish?
Expected Behavior
append_keys
provides structured information that the powertools logger unions into an output.Current Behavior
A logger can be set up with child=True but end up with a different method interface.
Current workaround is to avoid append_keys and only use extra as part of .debug/.info/etc... as that will go nowhere when a parent logger isn't instanciated.
Possible Solution
Open to suggestion
Steps to Reproduce (for bugs)
Various tests:
Environment
The text was updated successfully, but these errors were encountered: