Skip to content

Library is swallowing log messages from function code #38

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

Open
synic opened this issue Jun 4, 2021 · 1 comment
Open

Library is swallowing log messages from function code #38

synic opened this issue Jun 4, 2021 · 1 comment

Comments

@synic
Copy link

synic commented Jun 4, 2021

I'm using logging messages in my python code, but none of those are ever visible. I'm currently configuring my logger like so:

formatter = json_log_formatter.JSONFormatter()
handler = logging.StreamHandler(sys.stdout)

if conf.USE_JSON_LOGGING:
    handler.setFormatter(formatter)

logging.basicConfig(
    level=logging.INFO,
    format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',  # noqa
    handlers=[handler],
)
logger = logging.getLogger(__name__)

However, I never see the logs. Where are they going?

@juls858
Copy link

juls858 commented Jul 21, 2021

For whatever reason the lambda runtime interface client doesn't follow standard python logging. It definitely needs top e fixed. There is a logger in aws_lambda_powertools for bootstrapping the loggers and emits json logs as aws CloudWatch logs use structured logging. I assume this should work in cases where you are not using a lambda.

I ended having to do this to get logging to work for imported modules:

module being imported

# mymodule.py
import inspect
import logging

from aws_lambda_powertools import Logger

if os.path.basename(sys.modules["__main__"].__file__) == "app.py":
    # use aws lambda logger, formatter
    logger = Logger(child=True)
else:
    # use default logger
    logger = logging.getLogger(__name__)
    logger.addHandler(NullHandler())

Foo:
    ...

importing module

# lambda.py
from aws_lambda_powertools import Logger

from mymodule import Foo

def handler(event, context):
    Foo()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants