Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Breadcrumbs: Python logging message is not formatted properly #1257

@Alex-Bogdanov

Description

@Alex-Bogdanov

Hi guys,
I wrote as simple project spawning 2 logging events (it's a bit rusty, but it was just created for testing purposes):

import logging

from aiohttp import ServerDisconnectedError
from raven import setup_logging, Client
from raven.handlers.logging import SentryHandler

DSN = 'https://#######@sentry-prod#############'
client = Client(dsn=DSN)
handler = SentryHandler(client, level=logging.WARNING)
setup_logging(handler)


logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

def retry():
    context = {
        'attempt': 1,
        'attempts': 3,
        'fn': dict.fromkeys,
    }
    exc = ServerDisconnectedError()
    logger.warning(
        exc.__class__.__name__ + ' -> Attempts (%(attempt)d) are over for %(fn)r',
        context,
        exc_info=exc,
    )

    raise exc


def get(url):
    try:
        retry()
    except Exception as exc:
        msg = 'Failed to download %(url)s'
        context = {'url': url}
        logger.exception(msg, context, exc_info=exc)


if __name__ == '__main__':
    get('https://twitter.com/BlankPageApp')

This an output which we receive on web UI:
screen_1
screen_2

Why python log message with WARNING logging level is not rendered properly showing log formatting kwargs instead of real values?

Thanks,
Alex

EDIT: Looks like Sentry doesn't know how to render the log message using kwargs passed through the context variable. Formatting using args works for me perfectly:

logger.warning(
     exc.__class__.__name__ + ' -> Attempts (%d) are over for %r', 1, dict.fromkeys, exc_info=exc,
)

But still I consider this like an issue, because Python logging module handles both approaches on default:

The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions