Skip to content

Instantly share code, notes, and snippets.

@pollenjp
Last active June 10, 2020 17:05
Show Gist options
  • Save pollenjp/6f814ded2c9bf5b5fd44acb152d845b1 to your computer and use it in GitHub Desktop.
Save pollenjp/6f814ded2c9bf5b5fd44acb152d845b1 to your computer and use it in GitHub Desktop.
logging.conf.yml サンプル
# https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema
version: 1
formatters:
console_formatter:
# https://docs.python.org/ja/3/howto/logging.html#logging.logging.Formatter.__init__
format: "%(asctime)s | %(name)20s | %(levelname)10s | %(threadName)8s | %(filename)20s:%(lineno)4d | %(message)s"
file_formatter:
format: "%(asctime)s | %(name)20s | %(levelname)10s | %(threadName)8s | %(filename)20s:%(lineno)4d | %(message)s"
handlers:
console_handler:
class: logging.StreamHandler
level: INFO
formatter: console_formatter
file_handler:
class: logging.handlers.RotatingFileHandler
filename: "app.log"
mode: "a"
maxBytes: 52428800 # 50 * 1024 * 1024
backupCount: 100
encoding: "utf-8"
level: DEBUG
formatter: file_formatter
# loggers:
root:
level: DEBUG
handlers:
- console_handler
- file_handler
disable_existing_loggers: False # https://qiita.com/propella/items/5ad25eca11de2a871a06
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"></ul></div>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T19:05:04.678924Z",
"start_time": "2020-06-09T19:05:04.647671Z"
}
},
"outputs": [],
"source": [
"import logging.config\n",
"import yaml\n",
"import pprint"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T18:23:35.167986Z",
"start_time": "2020-06-09T18:23:35.149084Z"
}
},
"source": [
"- `logging.conf` ではなくyaml経由で configure する理由\n",
" - `logging.config.fileConfig` によるファイルロードでは, キーワード引数を Hnadler 等に渡せない!\n",
" - `logging.config.dictConfig` ならできる.\n",
" - どちらの場合でも実行する位置には気をつけること.\n",
" - https://ahyt910.hateblo.jp/entry/2019/04/16/134725\n",
" - https://qiita.com/propella/items/5ad25eca11de2a871a06\n",
" - https://docs.python.org/3/howto/logging.html#configuring-logging\n",
" - [Pass keyword arg to log handler in python logging configuration file - Stack Overflow](https://stackoverflow.com/questions/29236267/pass-keyword-arg-to-log-handler-in-python-logging-configuration-file)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T19:05:04.690911Z",
"start_time": "2020-06-09T19:05:04.680493Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'disable_existing_loggers': False,\n",
" 'formatters': {'console_formatter': {'format': '%(asctime)s | %(name)20s | '\n",
" '%(levelname)10s | '\n",
" '%(threadName)8s | '\n",
" '%(filename)20s:%(lineno)4d | '\n",
" '%(message)s'},\n",
" 'file_formatter': {'format': '%(asctime)s | %(name)20s | '\n",
" '%(levelname)10s | '\n",
" '%(threadName)8s | '\n",
" '%(filename)20s:%(lineno)4d | '\n",
" '%(message)s'}},\n",
" 'handlers': {'console_handler': {'class': 'logging.StreamHandler',\n",
" 'formatter': 'console_formatter',\n",
" 'level': 'INFO'},\n",
" 'file_handler': {'backupCount': 100,\n",
" 'class': 'logging.handlers.RotatingFileHandler',\n",
" 'encoding': 'utf-8',\n",
" 'filename': 'app.log',\n",
" 'formatter': 'file_formatter',\n",
" 'level': 'DEBUG',\n",
" 'maxBytes': 52428800,\n",
" 'mode': 'a'}},\n",
" 'root': {'handlers': ['console_handler', 'file_handler'], 'level': 'DEBUG'},\n",
" 'version': 1}\n"
]
}
],
"source": [
"with open(file=\"./logging.conf.yml\", mode=\"rt\") as f:\n",
" config_dict = yaml.safe_load(f)\n",
"pprint.pprint(config_dict)\n",
"logging.config.dictConfig(config=config_dict)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T19:05:04.698950Z",
"start_time": "2020-06-09T19:05:04.697014Z"
}
},
"outputs": [],
"source": [
"logger = logging.getLogger(\"LOG\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T19:05:04.704580Z",
"start_time": "2020-06-09T19:05:04.700903Z"
},
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2020-06-10 04:05:04,702 | root | INFO | MainThread | <ipython-input-5-37519995f3ff>: 1 | hello\n"
]
}
],
"source": [
"logging.info(\"hello\")\n",
"logging.debug(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2020-06-09T19:05:04.708944Z",
"start_time": "2020-06-09T19:05:04.705776Z"
},
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2020-06-10 04:05:04,706 | LOG | INFO | MainThread | <ipython-input-6-d2a7ba7e4519>: 1 | hello\n"
]
}
],
"source": [
"logger.info(\"hello\")\n",
"logger.debug(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.2 64-bit ('my3.8.2': venv)",
"language": "python",
"name": "python38264bitmy382venvc056a88477f942a48d4539eac164503d"
},
"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.8.2"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment