Skip to content

Formatting of extra fields, repr() vs str() #91

@adaitche

Description

@adaitche

fields[key] = repr(value)

The value of extra fields are formatted via repr(). In most cases it seems to me that str() would be better. A typical example is Path objects:

from pathlib import Path
path = Path("foo/bar")
print(repr(path))
print(str(path))

The repr-version is PosixPath('foo/bar'), whereas the str-version is foo/bar. In logs, I always want to have the latter version and, currently, have to manually enforce this via extra={"path": str(path)}. This is also true for many other objects.

According to Python docs repr() should return "the “official” string representation of an object" whereas str() should return a "nicely printable string representation of an object". For logs, I think, the latter is almost always what one wants.

Therefore my question: would you be open to changing the formatting of extra fields from repr() to str()? Am I missing some reason why it is a bad idea? If you agree, I would submit a PR.

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