Skip to content

Instantly share code, notes, and snippets.

@mols3131d
Last active May 10, 2024 08:03
Show Gist options
  • Save mols3131d/a163902fd51f3c939e203cf34e727388 to your computer and use it in GitHub Desktop.
Save mols3131d/a163902fd51f3c939e203cf34e727388 to your computer and use it in GitHub Desktop.
python3-common
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [structlog](https://www.structlog.org/en/stable/index.html)\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2024-04-15 07:00:02 [info ] hello, world! key=value! more_than_strings=[1, 2, 3]\n"
]
}
],
"source": [
"import structlog\n",
"\n",
"log = structlog.get_logger()\n",
"log.info(\"hello, %s!\", \"world\", key=\"value!\", more_than_strings=[1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2024-04-15 07:00:02 [info ] hello, world! key=value! more_than_strings=[1, 2, 3]\n"
]
}
],
"source": [
"import logging\n",
"import structlog\n",
"\n",
"structlog.configure(\n",
" processors=[\n",
" structlog.contextvars.merge_contextvars,\n",
" structlog.processors.add_log_level,\n",
" structlog.processors.StackInfoRenderer(),\n",
" structlog.dev.set_exc_info,\n",
" structlog.processors.TimeStamper(fmt=\"%Y-%m-%d %H:%M:%S\", utc=False),\n",
" structlog.dev.ConsoleRenderer(),\n",
" ],\n",
" wrapper_class=structlog.make_filtering_bound_logger(logging.NOTSET),\n",
" context_class=dict,\n",
" logger_factory=structlog.PrintLoggerFactory(),\n",
" cache_logger_on_first_use=False,\n",
")\n",
"\n",
"log = structlog.get_logger()\n",
"log.info(\"hello, %s!\", \"world\", key=\"value!\", more_than_strings=[1, 2, 3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## my structlog scripts\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from typing import Literal\n",
"\n",
"import sys\n",
"import logging\n",
"import structlog\n",
"\n",
"\n",
"def get_logger(\n",
" name=None,\n",
" level: Literal[\n",
" None,\n",
" \"NOTSET\",\n",
" \"DEBUG\",\n",
" \"INFO\",\n",
" \"WARNING\",\n",
" \"ERROR\",\n",
" \"CRITICAL\",\n",
" ] = \"INFO\",\n",
"):\n",
"\n",
" # name\n",
" if not name:\n",
" try:\n",
" name == __file__\n",
" except:\n",
" name == \"name\"\n",
"\n",
" # level\n",
" if level:\n",
" level = level.upper()\n",
" level = eval(f\"logging.{level}\")\n",
" else:\n",
" level = logging.NOTSET\n",
"\n",
" # ^ processors\n",
" shared_processors = [\n",
" # Processors that have nothing to do with output,\n",
" # e.g., add timestamps or log level names.\n",
" structlog.contextvars.merge_contextvars,\n",
" structlog.processors.add_log_level,\n",
" structlog.processors.StackInfoRenderer(),\n",
" structlog.dev.set_exc_info,\n",
" structlog.processors.TimeStamper(fmt=\"%Y-%m-%d %H:%M:%S\", utc=False),\n",
" ]\n",
"\n",
" if sys.stderr.isatty():\n",
" # Pretty printing when we run in a terminal session.\n",
" # Automatically prints pretty tracebacks when \"rich\" is installed\n",
" processors = shared_processors + [\n",
" structlog.dev.ConsoleRenderer(sort_keys=False),\n",
" ]\n",
" else:\n",
" # Print JSON when we run, e.g., in a Docker container.\n",
" # Also print structured tracebacks.\n",
" processors = shared_processors + [\n",
" structlog.processors.dict_tracebacks,\n",
" structlog.processors.JSONRenderer(),\n",
" ]\n",
"\n",
" structlog.configure(\n",
" processors=processors,\n",
" wrapper_class=structlog.make_filtering_bound_logger(level),\n",
" context_class=dict,\n",
" logger_factory=structlog.PrintLoggerFactory(),\n",
" cache_logger_on_first_use=False,\n",
" )\n",
"\n",
" logger = structlog.get_logger(name)\n",
"\n",
" return logger"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [processors](https://www.structlog.org/en/stable/processors.html)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### my processors\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2024-04-17 09:32:15 [info ] Hello, world! name=mols number=31\n"
]
}
],
"source": [
"import logging\n",
"import structlog\n",
"from pprint import pprint\n",
"from rich.table import Table\n",
"\n",
"name = \"my_processors_test\"\n",
"level = logging.INFO\n",
"\n",
"\n",
"def MyProcessor_start(logger, log_method, event_dict):\n",
" return event_dict\n",
"\n",
"\n",
"def MyProcessor_end(logger, log_method, event_dict):\n",
" return event_dict\n",
"\n",
"\n",
"def MyRenderer(logger, log_method, event_dict):\n",
" return structlog.dev.ConsoleRenderer(sort_keys=False)(\n",
" logger, log_method, event_dict\n",
" )\n",
"\n",
"\n",
"structlog.configure(\n",
" processors=[\n",
" MyProcessor_start,\n",
" structlog.contextvars.merge_contextvars,\n",
" structlog.processors.add_log_level,\n",
" structlog.processors.StackInfoRenderer(),\n",
" structlog.processors.TimeStamper(fmt=\"%Y-%m-%d %H:%M:%S\", utc=False),\n",
" MyProcessor_end,\n",
" MyRenderer,\n",
" ],\n",
" wrapper_class=structlog.make_filtering_bound_logger(level),\n",
" context_class=dict,\n",
" logger_factory=structlog.PrintLoggerFactory(),\n",
" cache_logger_on_first_use=False,\n",
")\n",
"\n",
"logger = structlog.get_logger(name)\n",
"event = {\"name\": \"mols\", \"number\": 31}\n",
"logger.info(f\"Hello, world!\", **event)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "crawler",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment