Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jktujg/logging-extension

Python logging module extension
https://github.com/jktujg/logging-extension

extension filter handler json jsonformatter logging python

Last synced: about 2 months ago
JSON representation

Python logging module extension

Awesome Lists containing this project

README

        

{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"# logging-extenson\n",
"Extension of built-in python module `logging`\n"
],
"id": "31b6396577c25058"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Instalation\n",
"```\n",
"pip install logging-extension\n",
"```\n"
],
"id": "57d89f2c9ba61abf"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Extensions\n",
"### JSONFormatter\n",
"Formats logs in JSON format\n",
"\n",
"\n",
"Configuration example\n",
"\n"
],
"id": "976733534a5b3e66"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T11:12:54.442388Z",
"start_time": "2024-07-29T11:12:54.438684Z"
}
},
"cell_type": "code",
"source": [
"import logging\n",
"from logging_extension import JSONFormatter\n",
"\n",
"\n",
"json_formatter = JSONFormatter(fmt_keys=dict(\n",
" logger='name',\n",
" level='levelno',\n",
"))\n",
"\n",
"logging.basicConfig(level='DEBUG', force=True)\n",
"logging.getLogger().handlers[0].setFormatter(json_formatter)\n",
"\n",
"logging.getLogger().info('info_message', extra={'extra': ['value']})"
],
"id": "bfd32aff2d6afc51",
"outputs": [],
"execution_count": 9
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
""
],
"id": "24c8fb8a13923356"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"\n",
"Dictionary-based configuration example\n",
"\n"
],
"id": "8b065fb9a97377ad"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T11:13:13.107038Z",
"start_time": "2024-07-29T11:13:13.098922Z"
}
},
"cell_type": "code",
"source": [
"import logging.config\n",
"\n",
"\n",
"config = {\n",
" \"version\": 1,\n",
" \"disable_existing_handlers\": False,\n",
" \"formatters\": {\n",
" \"json_formatter\": {\n",
" \"()\": \"logging_extension.JSONFormatter\",\n",
" \"fmt_keys\": {\n",
" \"name\": \"name\",\n",
" \"level\": \"levelno\",\n",
" }\n",
" }\n",
" },\n",
" \"handlers\": {\n",
" \"stream_handler\": {\n",
" \"class\": \"logging.StreamHandler\",\n",
" \"stream\": \"ext://sys.stderr\",\n",
" \"formatter\": \"json_formatter\"\n",
" },\n",
" },\n",
" \"root\": {\n",
" \"level\": \"DEBUG\",\n",
" \"handlers\": [\n",
" \"stream_handler\"\n",
" ]\n",
" }\n",
"}\n",
"\n",
"logging.config.dictConfig(config)\n",
"logging.debug('debug_msg', extra={'extra': ['value']})"
],
"id": "ad9591d0d2c9655c",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"{\"name\": \"root\", \"level\": 10, \"created\": \"2024-07-29T11:13:13.105325+00:00\", \"message\": \"debug_msg\", \"extra\": [\"value\"]}\n"
]
}
],
"execution_count": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "",
"id": "1914d83659263b94"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### LevelFilter\n",
"Logging level filter. Allows specifying a comparison function from the built-in `operator` module or providing your own.\n",
"\n",
"\n",
"Configuration example\n",
""
],
"id": "c93f12bde3c0c23"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T11:17:33.671404Z",
"start_time": "2024-07-29T11:17:33.662798Z"
}
},
"cell_type": "code",
"source": [
"import logging \n",
"from logging_extension import LevelFilter\n",
"\n",
"# `compare` argument also can be any function that compares levels\n",
"# e.g. compare(record_level: int, filter_level: int) -> bool: ...\n",
"only_error_filter = LevelFilter(level='ERROR', compare='eq', name='only_error_filter')\n",
"\n",
"logging.basicConfig(level='DEBUG', force=True)\n",
"logging.getLogger().addFilter(only_error_filter)\n",
"\n",
"logging.critical('skip critical')\n",
"logging.error('show error')"
],
"id": "b1aed373723b3f5b",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:show error\n"
]
}
],
"execution_count": 3
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"\n"
],
"id": "b32efe6ac5800082"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"\n",
"Dictionary-based configuration example\n",
""
],
"id": "5ee71e22a4126d5f"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T11:17:23.383921Z",
"start_time": "2024-07-29T11:17:23.379349Z"
}
},
"cell_type": "code",
"source": [
"import logging.config\n",
"\n",
"config = {\n",
" \"version\": 1,\n",
" \"disable_existing_handlers\": False,\n",
" \"filters\": {\n",
" \"only_error_filter\": {\n",
" \"()\": \"logging_extension.LevelFilter\",\n",
" \"level\": \"ERROR\",\n",
" \"compare\": \"eq\",\n",
" }\n",
" \n",
" },\n",
" \"loggers\": {\n",
" \"root\": {\n",
" \"level\": \"DEBUG\",\n",
" \"filters\": [\n",
" \"only_error_filter\",\n",
" ]\n",
" }\n",
" }\n",
"}\n",
"logging.config.dictConfig(config)\n",
"\n",
"logging.critical('skip critical', extra={'extra': 'value'})\n",
"logging.error('show error', extra={'extra': 'value'})\n",
"\n",
"logging.getLogger().filters.clear()"
],
"id": "4e58cb1a4ed9c69e",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:show error\n"
]
}
],
"execution_count": 2
},
{
"metadata": {},
"cell_type": "markdown",
"source": "",
"id": "57e5da7d6b30cbf"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "",
"id": "a3654a4cc2a1b49a"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### ThreadedHandler\n",
"Container for handlers that perform non-blocking logging in a separate thread.\n",
"Essentially, a wrapper around `QueueHandler` with automatic start of `QueueListener` using the provided handlers.\n",
"\n",
"\n",
"Configuration example\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
],
"id": "84fbab3d85eefcce"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T11:58:14.310015Z",
"start_time": "2024-07-29T11:58:14.305052Z"
}
},
"cell_type": "code",
"source": [
"import logging\n",
"from logging_extension import ThreadedHandler\n",
"\n",
"\n",
"threaded_handler = ThreadedHandler(\n",
" handler_0=logging.StreamHandler(),\n",
" handler_2=logging.StreamHandler(),\n",
")\n",
"\n",
"logging.basicConfig(force=True, level='DEBUG')\n",
"logging.getLogger().handlers = [threaded_handler]\n",
"\n",
"logging.getLogger().warning('debug msg')\n",
"print('in main thread')"
],
"id": "3a65e30fd259339b",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in main thread\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"debug msg\n",
"debug msg\n"
]
}
],
"execution_count": 48
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"\n"
],
"id": "9712bfff73582b0c"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\n",
"\n",
"\n",
"Dictionary-based configuration example\n",
"\n"
],
"id": "c0061ed258c740f6"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-29T12:06:58.552529Z",
"start_time": "2024-07-29T12:06:58.546809Z"
}
},
"cell_type": "code",
"source": [
"import logging.config\n",
"\n",
"\n",
"config = {\n",
" \"version\": 1,\n",
" \"disable_existing_handlers\": False,\n",
" \"handlers\": {\n",
" \"stream_handler_0\": {\n",
" \"class\": \"logging.StreamHandler\",\n",
" \"stream\": \"ext://sys.stderr\",\n",
" },\n",
" \"stream_handler_1\": {\n",
" \"class\": \"logging.StreamHandler\",\n",
" \"stream\": \"ext://sys.stderr\",\n",
" },\n",
" \"threaded_handler\": {\n",
" \"()\": \"logging_extension.ThreadedHandler\",\n",
" \"handler_0\": \"cfg://handlers.stream_handler_0\",\n",
" \"handler_1\": \"cfg://handlers.stream_handler_1\",\n",
" },\n",
" },\n",
" \"root\": {\n",
" \"level\": \"DEBUG\",\n",
" \"handlers\": [\n",
" \"threaded_handler\"\n",
" ]\n",
" }\n",
"}\n",
"\n",
"logging.config.dictConfig(config)\n",
"\n",
"logging.getLogger().warning('debug msg')\n",
"print('in main thread')"
],
"id": "f036073f1fcc83e4",
"outputs": [],
"execution_count": 55
},
{
"metadata": {},
"cell_type": "markdown",
"source": "\n",
"id": "e5fa7a7b5ef807d6"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}