# Annotated Logger
[contribution]: https://github.com/github/annotated-logger/blob/main/CONTRIBUTING.md
[![Coverage badge](https://github.com/github/annotated-logger/raw/python-coverage-comment-action-data/badge.svg)](https://github.com/github/annotated-logger/tree/python-coverage-comment-action-data) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/)
The `annotated-logger` package provides a decorator that can inject a annotatable logger object into a method or class. This logger object is a drop in replacement for `logging.logger` with additional functionality.
## Background
Annotated Logger is actively used by GitHub's Vulnerability Management team to help to easily add context to our logs in splunk. It is more or less feature complete for our current use cases, but we will add additional features/fixes as we discover a need for them. But, we'd love feature requests, bug report and or PRs for either (see our [contribution guidelines][contribution] for more information if you wish to contribute).
## Requirements
Annotated Logger is a Python package. It should work on any version of Python 3, but it is currently tested on 3.9 and higher.
## Usage
The `annotated-logger` package allows you to decorate a function so that the start and end of that function is logged as well as allowing that function to request an `annotated_logger` object which can be used as if it was a standard python `logger`. Additionally, the `annotated_logger` object will have added annotations based on the method it requested from, any other annotations that were configured ahead of time and any annotations that were added prior to a log being made. Finally, any uncaught exceptions in a decorated method will be logged and re-raised, which allows you to when and how a method ended regardless of if it was successful or not.
```python
from annotated_logger import AnnotatedLogger
annotated_logger = AnnotatedLogger(
annotations={"this": "will show up in every log"},
)
annotate_logs = annotated_logger.annotate_logs
@annotate_logs()
def foo(annotated_logger, bar):
annotated_logger.annotate(bar=bar)
annotated_logger.info("Hi there!", extra={"mood": "happy"})
foo("this is the bar parameter")
{"created": 1708476277.102495, "levelname": "INFO", "name": "annotated_logger.fe18537a-d293-45d7-83c9-51dab3a4c436", "message": "Hi there!", "mood": "happy", "action": "__main__:foo", "this": "will show up in every log", "bar": "this is the bar parameter", "annotated": true}
{"created": 1708476277.1026022, "levelname": "INFO", "name": "annotated_logger.fe18537a-d293-45d7-83c9-51dab3a4c436", "message": "success", "action": "__main__:foo", "this": "will show up in every log", "bar": "this is the bar parameter", "run_time": "0.0", "success": true, "annotated": true}
```
The example directory has a few files that exercise all of the features of the annotated-logger package. The `Calculator` class is the most fully featured example (but not a fully featured calculator :wink:). The `logging_config` example shows how to configure a logger via a dictConfig, like django uses. It also shows some of the interactions that can exist between a `logging` logger and an `annotated_logger` if `logging` is configured to use the annotated logger filter.
## License
This project is licensed under the terms of the MIT open source license. Please refer to MIT for the full terms.
## Maintainers
This project is primarily maintained by `crimsonknave` on behalf of GitHub's Vulnerability Management team as it was initially developed for our internal use.
## Support
Reported bugs will be addressed, pull requests are welcome, but there is limited bandwidth for work on new features.
Raw data
{
"_id": null,
"home_page": null,
"name": "annotated-logger",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "annotation, decorator, logging",
"author": null,
"author_email": "Vuln Mgmt Eng <security+vulnmgmteng@github.com>",
"download_url": "https://files.pythonhosted.org/packages/25/9a/d23159c07e3f81088654d05bd1ff5d29a174cec6ef96f980e85ba0064a60/annotated_logger-1.2.2.tar.gz",
"platform": null,
"description": "# Annotated Logger\n\n[contribution]: https://github.com/github/annotated-logger/blob/main/CONTRIBUTING.md\n\n[![Coverage badge](https://github.com/github/annotated-logger/raw/python-coverage-comment-action-data/badge.svg)](https://github.com/github/annotated-logger/tree/python-coverage-comment-action-data) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/)\n\nThe `annotated-logger` package provides a decorator that can inject a annotatable logger object into a method or class. This logger object is a drop in replacement for `logging.logger` with additional functionality.\n\n## Background\n\nAnnotated Logger is actively used by GitHub's Vulnerability Management team to help to easily add context to our logs in splunk. It is more or less feature complete for our current use cases, but we will add additional features/fixes as we discover a need for them. But, we'd love feature requests, bug report and or PRs for either (see our [contribution guidelines][contribution] for more information if you wish to contribute).\n\n## Requirements\nAnnotated Logger is a Python package. It should work on any version of Python 3, but it is currently tested on 3.9 and higher.\n\n## Usage\n\nThe `annotated-logger` package allows you to decorate a function so that the start and end of that function is logged as well as allowing that function to request an `annotated_logger` object which can be used as if it was a standard python `logger`. Additionally, the `annotated_logger` object will have added annotations based on the method it requested from, any other annotations that were configured ahead of time and any annotations that were added prior to a log being made. Finally, any uncaught exceptions in a decorated method will be logged and re-raised, which allows you to when and how a method ended regardless of if it was successful or not.\n\n```python\nfrom annotated_logger import AnnotatedLogger\n\nannotated_logger = AnnotatedLogger(\n annotations={\"this\": \"will show up in every log\"},\n)\nannotate_logs = annotated_logger.annotate_logs\n\n@annotate_logs()\ndef foo(annotated_logger, bar):\n annotated_logger.annotate(bar=bar)\n annotated_logger.info(\"Hi there!\", extra={\"mood\": \"happy\"})\n\nfoo(\"this is the bar parameter\")\n\n{\"created\": 1708476277.102495, \"levelname\": \"INFO\", \"name\": \"annotated_logger.fe18537a-d293-45d7-83c9-51dab3a4c436\", \"message\": \"Hi there!\", \"mood\": \"happy\", \"action\": \"__main__:foo\", \"this\": \"will show up in every log\", \"bar\": \"this is the bar parameter\", \"annotated\": true}\n{\"created\": 1708476277.1026022, \"levelname\": \"INFO\", \"name\": \"annotated_logger.fe18537a-d293-45d7-83c9-51dab3a4c436\", \"message\": \"success\", \"action\": \"__main__:foo\", \"this\": \"will show up in every log\", \"bar\": \"this is the bar parameter\", \"run_time\": \"0.0\", \"success\": true, \"annotated\": true}\n```\n\nThe example directory has a few files that exercise all of the features of the annotated-logger package. The `Calculator` class is the most fully featured example (but not a fully featured calculator :wink:). The `logging_config` example shows how to configure a logger via a dictConfig, like django uses. It also shows some of the interactions that can exist between a `logging` logger and an `annotated_logger` if `logging` is configured to use the annotated logger filter.\n\n## License\n\nThis project is licensed under the terms of the MIT open source license. Please refer to MIT for the full terms.\n\n## Maintainers\nThis project is primarily maintained by `crimsonknave` on behalf of GitHub's Vulnerability Management team as it was initially developed for our internal use.\n\n## Support\n\nReported bugs will be addressed, pull requests are welcome, but there is limited bandwidth for work on new features.\n",
"bugtrack_url": null,
"license": null,
"summary": "A decorator that logs the start and end of a function as well as providing an optional logger with annotations",
"version": "1.2.2",
"project_urls": {
"Homepage": "https://github.com/github/annotated-logger"
},
"split_keywords": [
"annotation",
" decorator",
" logging"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "719f4029d8852d7533721f3ad10ad04d14dd87f32cc713f296310dc06dc0f0ce",
"md5": "50bdf816fc7219d4d871d34451a1c2d5",
"sha256": "5cb20a6c8b5874def3d6801be50f4843feadceb3f576e8d83d49db0e782205c6"
},
"downloads": -1,
"filename": "annotated_logger-1.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "50bdf816fc7219d4d871d34451a1c2d5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 16570,
"upload_time": "2024-12-19T17:46:37",
"upload_time_iso_8601": "2024-12-19T17:46:37.124529Z",
"url": "https://files.pythonhosted.org/packages/71/9f/4029d8852d7533721f3ad10ad04d14dd87f32cc713f296310dc06dc0f0ce/annotated_logger-1.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "259ad23159c07e3f81088654d05bd1ff5d29a174cec6ef96f980e85ba0064a60",
"md5": "1f028913d470d21a52b6abd722c66f3e",
"sha256": "8c2913bb67449f5621e4308f6da78bee4cd7d60e99a16c00c7a35510b3714136"
},
"downloads": -1,
"filename": "annotated_logger-1.2.2.tar.gz",
"has_sig": false,
"md5_digest": "1f028913d470d21a52b6abd722c66f3e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 15593,
"upload_time": "2024-12-19T17:46:40",
"upload_time_iso_8601": "2024-12-19T17:46:40.423135Z",
"url": "https://files.pythonhosted.org/packages/25/9a/d23159c07e3f81088654d05bd1ff5d29a174cec6ef96f980e85ba0064a60/annotated_logger-1.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-19 17:46:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "github",
"github_project": "annotated-logger",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "certifi",
"specs": [
[
"==",
"2024.8.30"
]
]
},
{
"name": "charset-normalizer",
"specs": [
[
"==",
"3.4.0"
]
]
},
{
"name": "idna",
"specs": [
[
"==",
"3.10"
]
]
},
{
"name": "makefun",
"specs": [
[
"==",
"1.15.6"
]
]
},
{
"name": "pychoir",
"specs": [
[
"==",
"0.0.27"
]
]
},
{
"name": "python-json-logger",
"specs": [
[
"==",
"3.2.0"
]
]
},
{
"name": "requests",
"specs": [
[
"==",
"2.32.3"
]
]
},
{
"name": "urllib3",
"specs": [
[
"==",
"2.2.3"
]
]
}
],
"lcname": "annotated-logger"
}