{"id":22292129,"url":"https://github.com/joaodrp/gelf-formatter","last_synced_at":"2025-07-28T23:33:09.148Z","repository":{"id":45751671,"uuid":"184643214","full_name":"joaodrp/gelf-formatter","owner":"joaodrp","description":"GELF formatter for the Python standard library logging module.","archived":false,"fork":false,"pushed_at":"2022-12-09T05:18:14.000Z","size":77,"stargazers_count":9,"open_issues_count":6,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-29T15:52:19.430Z","etag":null,"topics":["formatter","gelf","graylog","log","logging","python"],"latest_commit_sha":null,"homepage":"","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/joaodrp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-02T19:56:29.000Z","updated_at":"2024-12-31T01:24:21.000Z","dependencies_parsed_at":"2023-01-25T11:02:05.568Z","dependency_job_id":null,"html_url":"https://github.com/joaodrp/gelf-formatter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/joaodrp/gelf-formatter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaodrp%2Fgelf-formatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaodrp%2Fgelf-formatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaodrp%2Fgelf-formatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaodrp%2Fgelf-formatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joaodrp","download_url":"https://codeload.github.com/joaodrp/gelf-formatter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaodrp%2Fgelf-formatter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267604310,"owners_count":24114521,"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-07-28T02:00:09.689Z","response_time":68,"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":["formatter","gelf","graylog","log","logging","python"],"created_at":"2024-12-03T17:19:51.593Z","updated_at":"2025-07-28T23:33:08.895Z","avatar_url":"https://github.com/joaodrp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\" style=\"border-bottom: none;\"\u003eGELF Formatter\u003c/h1\u003e\n\u003ch3 align=\"center\"\u003e\n    \u003ca href=\"http://docs.graylog.org/en/latest/pages/gelf.html\"\u003eGraylog Extended Log Format (GELF)\u003c/a\u003e formatter for the\u003cbr\u003ePython standard library \u003ca href=\"https://docs.python.org/3/library/logging.html\"\u003elogging\u003c/a\u003e module\n\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/joaodrp/gelf-formatter/releases/latest\"\u003e\n        \u003cimg alt=\"Release\" src=\"https://img.shields.io/github/release/joaodrp/gelf-formatter.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/gelf-formatter/\"\u003e\n        \u003cimg alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/gelf-formatter.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/gelf-formatter/\"\u003e\n        \u003cimg alt=\"Python versions\" src=\"https://img.shields.io/pypi/pyversions/gelf-formatter.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://travis-ci.com/joaodrp/gelf-formatter\"\u003e\n        \u003cimg alt=\"Travis\" src=\"https://img.shields.io/travis/com/joaodrp/gelf-formatter.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://codecov.io/gh/joaodrp/gelf-formatter\"\u003e\n        \u003cimg alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/joaodrp/gelf-formatter/master.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"LICENSE\"\u003e\n        \u003cimg alt=\"Software License\" src=\"https://img.shields.io/badge/license-MIT-brightgreen.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://semver.org/\"\u003e\n        \u003cimg alt=\"SemVer\" src=\"https://img.shields.io/badge/semver-2.0.0-blue.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://conventionalcommits.org\"\u003e\n        \u003cimg alt=\"Conventional Commits\" src=\"https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/ambv/black\"\u003e\n        \u003cimg alt=\"Code style\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pepy.tech/project/gelf-formatter\"\u003e\n        \u003cimg alt=\"Downloads\" src=\"https://pepy.tech/badge/gelf-formatter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/joaodrp/gelf-formatter/graphs/contributors\"\u003e\n        \u003cimg alt=\"Contributors\" src=\"https://img.shields.io/github/contributors/joaodrp/gelf-formatter.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://saythanks.io/to/joaodrp\"\u003e\n        \u003cimg alt=\"SayThanks.io\" src=\"https://img.shields.io/badge/say%20thanks-%E2%98%BC-1EAEDB.svg\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n## Motivation\n\nThere are several packages available providing *handlers* for the standard library logging module that can send application logs to [Graylog](https://www.graylog.org/) by TCP/UDP/HTTP ([py-gelf](https://pypi.org/project/pygelf/) is a good example). Although these can be useful, it's not ideal to make an application performance dependent on network requests just for the purpose of delivering logs.\n\nAlternatively, one can simply log to a file or `stdout` and have a collector (like [Fluentd](https://www.fluentd.org/)) processing and sending those logs *asynchronously* to a remote server (and not just to Graylog, as GELF can be used as a generic log format), which is a common pattern for containerized applications. In a scenario like this all we need is a GELF logging *formatter*.\n\n## Features\n\n- Support for arbitrary additional fields;\n- Support for including reserved [`logging.LogRecord`](https://docs.python.org/3/library/logging.html#logrecord-attributes) attributes as additional fields;\n- Exceptions detection with traceback formatting;\n- Zero dependencies and tiny footprint.\n\n## Installation\n\n### With pip\n\n```text\n$ pip install gelf-formatter\n```\n\n### From source\n\n```text\n$ python setup.py install\n```\n\n## Usage\n\nSimply create a `gelfformatter.GelfFormatter` instance and pass it as argument to [`logging.Handler.setFormatter`](https://docs.python.org/3/library/logging.html#logging.Handler.setFormatter):\n\n```python\nimport sys\nimport logging\n\nfrom gelfformatter import GelfFormatter\n\nformatter = GelfFormatter()\n\nhandler = logging.StreamHandler(sys.stdout)\nhandler.setFormatter(formatter)\n```\n\nApply it globally with [`logging.basicConfig`](https://docs.python.org/3/library/logging.html#logging.basicConfig) to automatically format log records from third-party packages as well:\n\n```python\nlogging.basicConfig(level=logging.DEBUG, handlers=[handler])\n```\n\nAlternatively, you can configure a local [`logging.Logger`](https://docs.python.org/3/library/logging.html#logging.Logger) instance through [`logging.Logger.addHandler`](https://docs.python.org/3/library/logging.html#logging.Logger.addHandler):\n\n```python\nlogger = logging.getLogger('my-app')\nlogger.addHandler(handler)\n```\n\nThat's it. You can now use the logging module as usual, all records will be formatted as GELF messages.\n\n### Standard Fields\n\nThe formatter will output all (non-deprecated) fields described in the [GELF Payload Specification (version 1.1)](http://docs.graylog.org/en/latest/pages/gelf.html#gelf-payload-specification):\n\n- `version`: String, always set to `1.1`;\n\n- `host`: String, the output of [`socket.gethostname`](https://docs.python.org/3/library/socket.html#socket.gethostname) at initialization;\n- `short_message`: String, log record message;\n- `full_message` (*optional*): String, formatted exception traceback (if any);\n- `timestamp`: Number, time in seconds since the epoch as a floating point;\n- `level`: Integer, *syslog* severity level.\n\nNone of these fields can be ignored, renamed or overridden.\n\n#### Example\n\n```python\nlogging.info(\"Some message\")\n```\n\n```text\n{\"version\":\"1.1\",\"host\":\"my-server\",\"short_message\":\"Some message\",\"timestamp\":1557342545.1067393,\"level\":6}\n```\n\n#### Exceptions\n\nThe `full_message` field is used to store the traceback of exceptions. You just need to log them with [`logging.exception`](https://docs.python.org/3/library/logging.html#logging.exception).\n\n##### Example\n\n```python\nimport urllib.request\n\nreq = urllib.request.Request('http://www.pythonnn.org')\ntry:\n    urllib.request.urlopen(req)\nexcept urllib.error.URLError as e:\n    logging.exception(e.reason)\n```\n\n```text\n{\"version\": \"1.1\", \"short_message\": \"[Errno -2] Name or service not known\", \"timestamp\": 1557342714.0695107, \"level\": 3, \"host\": \"my-server\", \"full_message\": \"Traceback (most recent call last):\\n  ...(truncated)... raise URLError(err)\\nurllib.error.URLError: \u003curlopen error [Errno -2] Name or service not known\u003e\"}\n```\n\n### Additional Fields\n\nThe GELF specification allows arbitrary additional fields, with keys prefixed with an underscore.\n\nTo include additional fields use the standard logging `extra` keyword. Keys will be automatically prefixed with an underscore (if not already).\n\n#### Example\n\n```python\nlogging.info(\"request received\", extra={\"path\": \"/orders/1\", \"method\": \"GET\"})\n```\n\n```text\n{\"version\": \"1.1\", \"short_message\": \"request received\", \"timestamp\": 1557343604.5892842, \"level\": 6, \"host\": \"my-server\", \"_path\": \"/orders/1\", \"_method\": \"GET\"}\n```\n\n#### Reserved Fields\n\nBy default the formatter ignores all [`logging.LogRecord` attributes](https://docs.python.org/3/library/logging.html#logrecord-attributes). You can however opt to include them as additional fields. This can be used to display useful information like the current module, filename, line number, etc.\n\nTo do so, simply pass a list of `LogRecord` attribute names as value of the `allowed_reserved_attrs` keyword when initializing a `GelfFormatter`. You can also modify the `allowed_reserved_attrs` instance variable of an already initialized formatter.\n\n##### Example\n\n```python\nattrs = [\"lineno\", \"module\", \"filename\"]\n\nformatter = GelfFormatter(allowed_reserved_attrs=attrs)\n# or\nformatter.allowed_reserved_attrs = attrs\n\nlogging.debug(\"starting application...\")\n```\n\n```text\n{\"version\": \"1.1\", \"short_message\": \"starting application...\", \"timestamp\": 1557346554.989846, \"level\": 6, \"host\": \"my-server\", \"_lineno\": 175, \"_module\": \"myapp\", \"_filename\": \"app.py\"}\n```\n\nYou can optionally customize the name of these additional fields using a [`logging.Filter`](https://docs.python.org/3/library/logging.html#filter-objects) (see below).\n\nSimilarily, you can choose to ignore additional attributes passed via the `extra` keyword argument. This can be usefull to e.g. not log keywords named `secret` or `password`.\n\nTo do so, pass a list of names to the `ignored_attrs` keyword when initializing a `GelfFormatter`. You can also modify the `ignored_attrs` instance variable of an already initialized formatter.\n\n##### Example\n\nBut be aware: nested fields will be printed! Only the root level of keywords is filtered by the `ignored_attrs`.\n\n```python\nattrs = [\"secret\", \"password\"]\n\nformatter = GelfFormatter(ignored_attrs=attrs)\n# or\nformatter.ignored_attrs = attrs\n\nlogging.debug(\"app config\", extra={\"connection\": \"local\", \"secret\": \"verySecret!\", \"mysql\": {\"user\": \"test\", \"password\": \"will_be_logged\"}})\n```\n\n```text\n{\"version\": \"1.1\", \"short_message\": \"app config\", \"timestamp\": 1557346554.989846, \"level\": 6, \"host\": \"my-server\", \"_connection\": \"local\", \"_mysql\": {\"user\": \"test\", \"password\": \"will_be_logged\"}}\n```\n\n\n#### Context Fields\n\nHaving the ability to define a set of additional fields once and have them included in all log messages can be useful to avoid repetitive `extra` key/value pairs and enable contextual logging.\n\nPython's logging module provides several options to add context to a logger, among which we highlight the  [`logging.LoggerAdapter`](https://docs.python.org/3/library/logging.html#loggeradapter-objects) and [`logging.Filter`](https://docs.python.org/3/library/logging.html#filter-objects).\n\nBetween these we recommend a `logging.Filter`, which is simpler and can be attached directly to a [`logging.Handler`](https://docs.python.org/3/library/logging.html#handler-objects). A `logging.Filter` can therefore be used locally (on a [`logging.Logger`](https://docs.python.org/3/library/logging.html#logger-objects)) or globally (through `logging.basicConfig`). If you opt for a `LoggerAdapter` you'll need a `logging.Logger` to wrap.\n\nYou can also use a `logging.Filter` to reuse/rename any of the reserved `logging.LogRecord` attributes.\n\n##### Example\n\n```python\nclass ContextFilter(logging.Filter):\n    def filter(self, record):\n        # Add any number of arbitrary additional fields\n        record.app = \"my-app\"\n        record.app_version = \"1.2.3\"\n        record.environment = os.environ.get(\"APP_ENV\")\n\n        # Reuse any reserved `logging.LogRecord` attributes\n        record.file = record.filename\n        record.line = record.lineno\n        return True\n\n\nformatter = GelfFormatter()\n\nhandler = logging.StreamHandler(sys.stdout)\n\nhandler.setFormatter(formatter)\nhandler.addFilter(ContextFilter())\n\nlogging.basicConfig(level=logging.DEBUG, handlers=[handler])\n\nlogging.info(\"hi\", extra=dict(foo=\"bar\"))\n```\n\n```text\n{\"version\": \"1.1\", \"short_message\": \"hi\", \"timestamp\": 1557431642.189755, \"level\": 6, \"host\": \"my-server\", \"_foo\": \"bar\", \"_app\": \"my-app\", \"_app_version\": \"1.2.3\", \"_environment\": \"development\", \"_file\": \"app.py\", \"_line\": 159}\n```\n\n## Pretty-Print\n\nLooking for a GELF log pretty-printer? If so, have a look at [gelf-pretty](https://github.com/joaodrp/gelf-pretty) :fire:\n\n## Contributions\n\nThis project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please refer to our [contributing guide](CONTRIBUTING.md) for further information.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaodrp%2Fgelf-formatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoaodrp%2Fgelf-formatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaodrp%2Fgelf-formatter/lists"}