{"id":31578802,"url":"https://github.com/josheppinette/python-logfmter","last_synced_at":"2025-10-05T20:01:47.734Z","repository":{"id":62576671,"uuid":"455972874","full_name":"josheppinette/python-logfmter","owner":"josheppinette","description":"Add logfmt structured logging using the stdlib logging module and without changing a single log call.","archived":false,"fork":false,"pushed_at":"2025-09-24T22:44:07.000Z","size":1196,"stargazers_count":93,"open_issues_count":8,"forks_count":11,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-09-24T23:33:55.986Z","etag":null,"topics":["log","logfmt","logging","python","python-3","structured","structured-logging"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/logfmter/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josheppinette.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"josheppinette"}},"created_at":"2022-02-05T20:02:03.000Z","updated_at":"2025-09-24T22:44:11.000Z","dependencies_parsed_at":"2024-06-19T12:12:43.744Z","dependency_job_id":"7c80f0d4-c798-4cde-8d02-c7d41474b5bc","html_url":"https://github.com/josheppinette/python-logfmter","commit_stats":{"total_commits":42,"total_committers":2,"mean_commits":21.0,"dds":0.09523809523809523,"last_synced_commit":"89b4d16d0545d84a7f8aa6a6ca0d038d1b4fc998"},"previous_names":["josheppinette/python-logfmter"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/josheppinette/python-logfmter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josheppinette%2Fpython-logfmter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josheppinette%2Fpython-logfmter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josheppinette%2Fpython-logfmter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josheppinette%2Fpython-logfmter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josheppinette","download_url":"https://codeload.github.com/josheppinette/python-logfmter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josheppinette%2Fpython-logfmter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276977402,"owners_count":25738892,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-25T02:00:09.612Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["log","logfmt","logging","python","python-3","structured","structured-logging"],"created_at":"2025-10-05T20:01:27.587Z","updated_at":"2025-10-05T20:01:47.728Z","avatar_url":"https://github.com/josheppinette.png","language":"Python","readme":"![python logfmter](./banner.png)\n\n\u003cdiv align=\"center\"\u003e\n\n[![pre-commit](https://github.com/josheppinette/python-logfmter/actions/workflows/pre-commit.yml/badge.svg)](https://github.com/josheppinette/python-logfmter/actions/workflows/pre-commit.yml)\n[![test](https://github.com/josheppinette/python-logfmter/actions/workflows/test.yml/badge.svg)](https://github.com/josheppinette/python-logfmter/actions/workflows/test.yml)\n[![python-3.9-3.10-3.11-3.12-3.13](https://img.shields.io/badge/python-3.9%20|%203.10%20|%203.11|%203.12|%203.13-blue.svg)](.github/workflows/test.yml)\n\nAdd [logfmt](https://www.brandur.org/logfmt) structured logging using the stdlib logging module and without changing a single log call.\n\n\u003c/div\u003e\n\n```python\n\u003e logging.warn(\"user created\", extra=user)\n\nat=WARNING msg=\"user created\" first_name=John last_name=Doe age=25\n```\n\n# Table of Contents\n\n1. [Why](#why)\n2. [Install](#install)\n3. [Usage](#usage)\n   1. [Integration](#integration)\n   2. [Configuration](#configuration)\n   3. [Extension](#extension)\n   4. [Guides](#guides)\n   5. [Gotchas](#gotchas)\n4. [Development](#development)\n   1. [Required Software](#required-software)\n   2. [Getting Started](#getting-started)\n   3. [Contributing](#contributing)\n   4. [Publishing](#publishing)\n\n# Why\n\n- enables both human and computer readable logs, [recommended as a \"best practice\" by Splunk](https://dev.splunk.com/enterprise/docs/developapps/addsupport/logging/loggingbestpractices/)\n- formats all first and third party logs, you never have to worry about a library using a different logging format\n- simple to integrate into any existing application, requires no changes to existing log statements i.e. [structlog](https://github.com/hynek/structlog)\n\n# Install\n\n```sh\n$ pip install logfmter\n```\n\n# Usage\n\nThis package exposes a single `Logfmter` class that can be integrated into\nthe [standard library logging system](https://docs.python.org/3/howto/logging.html) like any [`logging.Formatter`](https://docs.python.org/3/howto/logging.html#formatters).\n\n## Integration\n\nSimply use the standard logger's `basicConfig` or `dictConfig` initialization systems to get started. Examples are provided below.\n\n**[basicConfig](https://docs.python.org/3/library/logging.html#logging.basicConfig)**\n\n```python\nimport logging\nfrom logfmter import Logfmter\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(Logfmter())\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\", extra={\"alpha\": 1}) # at=ERROR msg=hello alpha=1\nlogging.error({\"token\": \"Hello, World!\"}) # at=ERROR token=\"Hello, World!\"\n```\n\n**[dictConfig](https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig)**\n\n_If you are using `dictConfig`, you need to consider your setting\nof `disable_existing_loggers`. It is enabled by default, and causes\nany third party module loggers to be disabled._\n\n```python\nimport logging.config\n\nlogging.config.dictConfig(\n    {\n        \"version\": 1,\n        \"formatters\": {\n            \"logfmt\": {\n                \"()\": \"logfmter.Logfmter\",\n            }\n        },\n        \"handlers\": {\n            \"console\": {\"class\": \"logging.StreamHandler\", \"formatter\": \"logfmt\"}\n        },\n        \"loggers\": {\"\": {\"handlers\": [\"console\"], \"level\": \"INFO\"}},\n    }\n)\n\nlogging.info(\"hello\", extra={\"alpha\": 1}) # at=INFO msg=hello alpha=1\n```\n\n_Notice, you can configure the `Logfmter` by providing keyword arguments as dictionary\nitems after `\"()\"`:_\n\n```python\n...\n\n    \"logfmt\": {\n        \"()\": \"logfmter.Logfmter\",\n        \"keys\": [...],\n        \"mapping\": {...}\n    }\n\n...\n```\n\n**[fileConfig](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig)**\n\nUsing logfmter via fileConfig is not supported, because fileConfig does not support custom formatter initialization. There may be some hacks to make this work in the future. Let me know if you have ideas or really need this.\n\n## Configuration\n\nThere is no additional configuration necessary to get started using Logfmter.\nHowever, if desired, you can modify the functionality using the following\ninitialization parameters.\n\n**keys**\n\nBy default, the `at=\u003clevelname\u003e` key/value will be included in all log messages. These\ndefault keys can be overridden using the `keys` parameter. If the key you want to include\nin your output is represented by a different attribute on the log record, then you can\nuse the `mapping` parameter to provide that key/attribute mapping.\n\nReference the Python [`logging.LogRecord` Documentation](https://docs.python.org/3/library/logging.html?highlight=logrecord#logging.LogRecord)\nfor a list of available attributes.\n\n```python\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(keys=[\"at\", \"processName\"])\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\") # at=ERROR processName=MainProceess msg=hello\n```\n\n**mapping**\n\nBy default, a mapping of `{\"at\": \"levelname\"}` is used to allow the `at` key to reference\nthe log record's `levelname` attribute. You can override this parameter to provide your\nown mappings.\n\n```python\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(\n    keys=[\"at\", \"process\"],\n    mapping={\"at\": \"levelname\", \"process\": \"processName\"}\n)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\") # at=ERROR process=MainProceess msg=hello\n```\n\n**datefmt**\n\nIf you request the `asctime` attribute (directly or through a mapping), then the date format\ncan be overridden through the `datefmt` parameter.\n\n```python\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(\n    keys=[\"at\", \"when\"],\n    mapping={\"at\": \"levelname\", \"when\": \"asctime\"},\n    datefmt=\"%Y-%m-%d\"\n)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\") # at=ERROR when=2022-04-20 msg=hello\n```\n\n**defaults**\n\nInstead of providing key/value pairs at each log call, you can provide defaults:\n\n```py\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(\n    keys=[\"at\", \"when\", \"trace_id\"],\n    mapping={\"at\": \"levelname\", \"when\": \"asctime\"},\n    datefmt=\"%Y-%m-%d\",\n    defaults={\"trace_id\": \"123\"},\n)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\") # at=ERROR when=2022-04-20 trace_id=123 msg=hello\n```\n\nThis will cause all logs to have the `trace_id=123` pair regardless of including\n`trace_id` in keys or manually adding `trace_id` to the `extra` parameter or the `msg` object.\n\n\u003e Note, the defaults object uses format strings as values. This allows for variables templating. See \"Aliases\" guide for more information.\n\n**ignored_keys**\n\nSometimes log records include fields that you don't want in your output.\nThis often happens when other libraries or frameworks add extra keys to the `LogRecord` that are not relevant to your log format.\n\nYou can explicitly exclude unwanted keys by using the `ignored_keys` parameter.\n\n```py\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(\n    keys=[\"at\"],\n    mapping={\"at\": \"levelname\"},\n    datefmt=\"%Y-%m-%d\",\n    ignored_keys=[\"color_message\"],\n)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.info(\"Started server process [%s]\", 97819, extra={\"color_message\": \"Started server process [%d]\"})\n# at=INFO msg=\"Started server process [97819]\"\n```\n\n## Extension\n\nYou can subclass the formatter to change its behavior.\n\n```python\nimport logging\nfrom logfmter import Logfmter\n\n\nclass CustomLogfmter(Logfmter):\n    \"\"\"\n    Provide a custom logfmt formatter which formats\n    booleans as \"yes\" or \"no\" strings.\n    \"\"\"\n\n    @classmethod\n    def format_value(cls, value):\n        if isinstance(value, bool):\n            return \"yes\" if value else \"no\"\n\n\treturn super().format_value(value)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(CustomLogfmter())\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error({\"example\": True}) # at=ERROR example=yes\n```\n\n## Guides\n\n**Aliases**\n\nProviding a format string as a default's key/value allows the realization of aliases:\n\n```py\nimport logging\nfrom logfmter import Logfmter\n\nformatter = Logfmter(\n    keys=[\"at\", \"when\", \"func\"],\n    mapping={\"at\": \"levelname\", \"when\": \"asctime\"},\n    datefmt=\"%Y-%m-%d\",\n    defaults={\"func\": \"{module}.{funcName}:{lineno}\"},\n)\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogging.basicConfig(handlers=[handler])\n\nlogging.error(\"hello\") # at=ERROR when=2022-04-20 func=\"mymodule.__main__:12\" msg=hello\n```\n\n## Gotchas\n\n**Reserved Keys**\n\nThe standard library logging system restricts the ability to pass internal [log record attributes](https://docs.python.org/3/library/logging.html#logrecord-attributes) via the log call's `extra` parameter.\n\n```py\n\u003e logging.error(\"invalid\", extra={\"filename\": \"alpha.txt\"})\nTraceback (most recent call last):\n  ...\n```\n\nThis can be circumvented by utilizing logfmter's ability to pass extras\nvia the log call's `msg` argument.\n\n```py\n\u003e logging.error({\"msg\": \"valid\", \"filename\": \"alpha.txt\"})\nat=ERROR msg=valid filename=alpha.txt\n```\n\n# Development\n\n## Required Software\n\nIf you are using [nix](https://zero-to-nix.com/start/install/) \u0026 [direnv](https://direnv.net/docs/installation.html), then your dev environment will be managed automatically. Otherwise, you will need to manually install the following software:\n\n- [direnv](https://direnv.net)\n- [git](https://git-scm.com/)\n- [pyenv](https://github.com/pyenv/pyenv#installation)\n\n\u003e Additionally, if you aren't using nix, then you will need to manually build\n\u003e the \"external\" tools found in `external`. These are used during testing to\n\u003e verify compatibility with libraries from different ecosystems. Alternatively, you can exclude those tests with `pytest -m \"not external\"`, but this is not recommended.\n\n## Getting Started\n\n**Setup**\n\n\u003e If you are using pyenv, you will need to install the correct versions of python using `\u003cruntimes.txt xargs -n 1 pyenv install -s`.\n\n```sh\n$ direnv allow\n$ pip install -r requirements/dev.txt\n$ pre-commit install\n$ pip install -e .\n```\n\n**Tests**\n\n_Run the test suite against the active python environment._\n\n```sh\n$ pytest\n```\n\n_Run the test suite against the active python environment and\nwatch the codebase for any changes._\n\n```sh\n$ ptw\n```\n\n_Run the test suite against all supported python versions._\n\n```sh\n$ tox\n```\n\n## Contributing\n\n1. Create an issue with all necessary details.\n2. Create a branch off from `main`.\n3. Make changes.\n4. Verify tests pass in all supported python versions: `tox`.\n5. Verify code conventions are maintained: `git add --all \u0026\u0026 pre-commit run -a`.\n6. Create your commit following the [conventionalcommits](https://www.conventionalcommits.org/en/v1.0.0/#summary).\n7. Create a pull request with all necessary details: description, testing notes, resolved issues.\n\n## Publishing\n\n**Create**\n\n1. Update the version number in `logfmter/__init__.py`.\n\n2. Add an entry in `HISTORY.md`.\n\n3. Commit the changes, tag the commit, and push the tags:\n\n   ```sh\n   $ git commit -am \"v\u003cmajor\u003e.\u003cminor\u003e.\u003cpatch\u003e\"\n   $ git tag v\u003cmajor\u003e.\u003cminor\u003e.\u003cpatch\u003e\n   $ git push origin main --tags\n   ```\n\n4. Convert the tag to a release in GitHub with the history\n   entry as the description.\n\n**Build**\n\n```sh\n$ python -m build\n```\n\n**Upload**\n\n```\n$ twine upload dist/*\n```\n","funding_links":["https://github.com/sponsors/josheppinette"],"categories":["Python","Logging"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosheppinette%2Fpython-logfmter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosheppinette%2Fpython-logfmter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosheppinette%2Fpython-logfmter/lists"}