{ "info": { "author": "Jo\u00e3o Pereira", "author_email": "", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: System :: Logging" ], "description": "

GELF Formatter

\n

\n Graylog Extended Log Format (GELF) formatter for the
Python standard library logging module\n

\n

\n \n \"Release\"\n \n \n \"PyPI\"\n \n \n \"Python\n \n \n \"Travis\"\n \n \n \"Codecov\"\n \n \n \"Software\n \n \n \"SemVer\"\n \n \n \"Conventional\n \n \n \"Code\n \n \n \"Downloads\"\n \n \n \"Contributors\"\n \n \n \"SayThanks.io\"\n \n

\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: \"}\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\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\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/joaodrp/gelf-formatter", "keywords": "gelf,graylog,logger,logging,log,json", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "gelf-formatter", "package_url": "https://pypi.org/project/gelf-formatter/", "platform": "", "project_url": "https://pypi.org/project/gelf-formatter/", "project_urls": { "Homepage": "https://github.com/joaodrp/gelf-formatter" }, "release_url": "https://pypi.org/project/gelf-formatter/0.1.0/", "requires_dist": null, "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "summary": "GELF formatter for the Python standard library logging module.", "version": "0.1.0" }, "last_serial": 5253728, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "b11d808d8228d4864c4e3c5050d269b5", "sha256": "95ae7280310bd5deeee0346b13cdf0455a6033148bf40b7c24b611895cd186d0" }, "downloads": -1, "filename": "gelf_formatter-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "b11d808d8228d4864c4e3c5050d269b5", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7632, "upload_time": "2019-05-10T19:37:02", "url": "https://files.pythonhosted.org/packages/24/92/560ff770d47074455c8d9121a612e5b8494959b388eac789efee88082ad8/gelf_formatter-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4c70e98e207f4dfec6b9a4c8a226bfc0", "sha256": "f841adb1aa555976e9a04747a70c36612dcf49c61dbc845b2a2c77d73916fe48" }, "downloads": -1, "filename": "gelf-formatter-0.1.0.tar.gz", "has_sig": false, "md5_digest": "4c70e98e207f4dfec6b9a4c8a226bfc0", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 8744, "upload_time": "2019-05-10T19:37:04", "url": "https://files.pythonhosted.org/packages/7d/21/f4332ba9057a0adada76e0044fc6648a45af4910149ecef08b7db5417796/gelf-formatter-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b11d808d8228d4864c4e3c5050d269b5", "sha256": "95ae7280310bd5deeee0346b13cdf0455a6033148bf40b7c24b611895cd186d0" }, "downloads": -1, "filename": "gelf_formatter-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "b11d808d8228d4864c4e3c5050d269b5", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 7632, "upload_time": "2019-05-10T19:37:02", "url": "https://files.pythonhosted.org/packages/24/92/560ff770d47074455c8d9121a612e5b8494959b388eac789efee88082ad8/gelf_formatter-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4c70e98e207f4dfec6b9a4c8a226bfc0", "sha256": "f841adb1aa555976e9a04747a70c36612dcf49c61dbc845b2a2c77d73916fe48" }, "downloads": -1, "filename": "gelf-formatter-0.1.0.tar.gz", "has_sig": false, "md5_digest": "4c70e98e207f4dfec6b9a4c8a226bfc0", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", "size": 8744, "upload_time": "2019-05-10T19:37:04", "url": "https://files.pythonhosted.org/packages/7d/21/f4332ba9057a0adada76e0044fc6648a45af4910149ecef08b7db5417796/gelf-formatter-0.1.0.tar.gz" } ] }