{"id":3792,"date":"2022-02-15T23:44:21","date_gmt":"2022-02-15T14:44:21","guid":{"rendered":"https:\/\/www.python.ambitious-engineer.com\/?p=3792"},"modified":"2022-02-15T23:44:21","modified_gmt":"2022-02-15T14:44:21","slug":"%e3%83%ad%e3%82%b0%e3%82%92json%e5%bd%a2%e5%bc%8f%e3%81%a7%e5%87%ba%e5%8a%9b%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/3792","title":{"rendered":"\u30ed\u30b0\u3092JSON\u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b"},"content":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306f\u30ed\u30b0\u3092JSON\u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306bJSON\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f<a href=\"https:\/\/www.python.ambitious-engineer.com\/archives\/725\" title=\"logging \u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\">\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>JSON Lines\u5f62\u5f0f\u3067\u306e\u30ed\u30b0\u51fa\u529b<\/h2>\n<p>\u30ed\u30b0\u306e\u5f62\u5f0f\u306f\u4f1d\u7d71\u7684\u306a\u30b9\u30da\u30fc\u30b9\u533a\u5207\u308a\u304b\u3089LTSV\u7b49\u3055\u307e\u3056\u307e\u306a\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u304c\u30012022\u5e74\u6642\u70b9Web\u306e\u754c\u9688\u3067\u306f1\u884c1\u884c\u304cJSON\u3068\u306a\u3063\u3066\u3044\u308bJSON Lines\u5f62\u5f0f\u304c\u6bd4\u8f03\u7684\u4eba\u6c17\u304c\u3042\u308b\u5f62\u5f0f\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p><a href=\"https:\/\/jsonlines.org\/\" title=\"https:\/\/jsonlines.org\/\"><\/a><\/p>\n<p>\u4e88\u3081\u69cb\u9020\u5316\u3055\u308c\u305f\u5f62\u5f0f\u3067\u3042\u308b\u305f\u3081\u3001\u6a19\u6e96\u5316\u3057\u3066\u304a\u304f\u3068\u52b9\u7387\u7684\u306b\u8ee2\u9001\u30fb\u53ce\u96c6\u3057\u305f\u30ed\u30b0\u306e\u89e3\u6790\u3001\u96c6\u8a08\u7b49\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>python-json-logger<\/h2>\n<p>\u3084\u308a\u65b9\u306f\u3044\u304f\u3064\u304b\u3042\u308a\u3001Python\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u3067\u3082\u3042\u308b\u7a0b\u5ea6\u306f\u306a\u3093\u3068\u304b\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u500b\u4eba\u7684\u306b\u306fpython-json-logger\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4f7f\u7528\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/pypi.org\/project\/python-json-logger\/\" title=\"https:\/\/pypi.org\/project\/python-json-logger\/\">python-json-logger<\/a><\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install python-json-logger\r\n<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u3068\u3057\u3066jsonlogger.JsonFormatter\u3092\u751f\u6210\u3057\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067\u3059\u3002jsonlogger.JsonFormatter\u306f\u7d44\u307f\u8fbc\u307f\u306elogging.Formatter\u3092\u7d99\u627f\u3057\u3066\u304a\u308a\u3001fmt\u7b49\u305d\u306e\u307e\u307e\u5f15\u6570\u3067\u8a2d\u5b9a\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u3001\u51fa\u529b\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306fmessage\u304cASCII\u30a8\u30f3\u30b3\u30fc\u30c9\u3055\u308c\u3066\u3057\u307e\u3046\u305f\u3081json_ensure_ascii\u3092False\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport logging\r\nfrom pythonjsonlogger import jsonlogger\r\n\r\n# \u30ed\u30ac\u30fc\u3092\u751f\u6210\r\nlogger = logging.getLogger(__name__)\r\nlogger.setLevel(logging.DEBUG)  # \u51fa\u529b\u30ec\u30d9\u30eb\u6307\u5b9a\r\n\r\n# \u30cf\u30f3\u30c9\u30e9\u3092\u751f\u6210\r\nh = logging.StreamHandler()\r\nh.setLevel(logging.DEBUG)  # \u51fa\u529b\u30ec\u30d9\u30eb\u3092\u8a2d\u5b9a\r\n\r\n# \u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u3092\u751f\u6210\r\njson_fmt = jsonlogger.JsonFormatter(fmt='%(asctime)s %(levelname)s %(name)s %(message)s', json_ensure_ascii=False)\r\n\r\n# \u30cf\u30f3\u30c9\u30e9\u306b\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30fc\u3092\u8a2d\u5b9a\r\nh.setFormatter(json_fmt)\r\n\r\n# \u30ed\u30ac\u30fc\u306b\u30cf\u30f3\u30c9\u30e9\u3092\u8a2d\u5b9a\r\nlogger.addHandler(h)\r\n\r\n# \u30ed\u30b0\u51fa\u529b\r\nlogger.info(&quot;\u30ed\u30b0\u3092\u51fa\u529b&quot;)\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306bJSON Lines\u5f62\u5f0f\u3067\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{&quot;asctime&quot;: &quot;2022-02-14 21:53:14,147&quot;, &quot;levelname&quot;: &quot;INFO&quot;, &quot;name&quot;: &quot;__main__&quot;, &quot;message&quot;: &quot;\u30ed\u30b0\u3092\u51fa\u529b&quot;}\r\n<\/pre>\n<p>JsonFormatter\u7d99\u627f\u3057\u3066JSON\u306e\u30ad\u30fc\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u3001PyPI\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304b\u3089\u306e\u5f15\u7528\u3067\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass CustomJsonFormatter(jsonlogger.JsonFormatter):\r\n    def add_fields(self, log_record, record, message_dict):\r\n        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)\r\n        if not log_record.get('timestamp'):\r\n            # this doesn't use record.created, so it is slightly off\r\n            now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')\r\n            log_record['timestamp'] = now\r\n        if log_record.get('level'):\r\n            log_record['level'] = log_record['level'].upper()\r\n        else:\r\n            log_record['level'] = record.levelname\r\n<\/pre>\n<h2>Django\u3067\u306e\u8a2d\u5b9a\u4f8b<\/h2>\n<p>\u304a\u305d\u3089\u304f\u30cb\u30fc\u30ba\u304c\u9ad8\u3044\u3067\u3042\u308d\u3046Django\u3067\u306e\u8a2d\u5b9a\u4f8b\u3092\u88dc\u8db3\u3067\u89e3\u8aac\u3057\u307e\u3059\u3002\u6587\u5b57\u5217\u3067\u30d5\u30a9\u30fc\u30de\u30c3\u30bf\u30af\u30e9\u30b9\"pythonjsonlogger.jsonlogger.JsonFormatter\"\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u305d\u306e\u4ed6\u306e\u5f15\u6570\u3068\u3057\u3066json_ensure_ascii\u3068\u306b\u306f\u51fa\u529b\u3055\u305b\u305f\u3044\u9805\u76ee\u3092fmt\u306b\u5217\u6319\u3057\u3066\u6307\u5b9a\u3057\u307e\u3059\u3002settings.py\u306eLOGGING\u3067\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nformatters = {\r\n   &quot;json_fmt&quot;: {&quot;()&quot;: &quot;pythonjsonlogger.jsonlogger.JsonFormatter&quot;,\r\n                  &quot;json_ensure_ascii&quot;: False,\r\n                  &quot;fmt&quot;: &quot;%(asctime)s %(levelname)s %(module)s %(process)d %(thread)d %(message)s&quot;}\r\n}\r\nLOGGING = {\r\n   'version': 1,\r\n   'disable_existing_loggers': False,\r\n   'formatters': formatters,\r\n   'handlers': {\r\n       'my_handler': {\r\n           'level': 'INFO',\r\n           'class': 'logging.StreamHandler',\r\n           'formatter': 'json_fmt',\r\n       }\r\n   },\r\n   # logger\u306e\u8a2d\u5b9a\r\n   'loggers': {\r\n       # \u753b\u9762\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30ed\u30b0\r\n       'mylog': {\r\n           'handlers': ['my_handler'],\r\n           'level': 'INFO',\r\n       }\r\n   }\r\n}\r\n<\/pre>\n<p>\u30ed\u30b0\u51fa\u529b\u7b87\u6240<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport logging\r\nlogger = logging.getLogger(&quot;mylog&quot;)\r\nlogger.info(&quot;\u30e1\u30c3\u30bb\u30fc\u30b8&quot;)\r\n<\/pre>\n<p>\u51fa\u529b\u7d50\u679c<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{&quot;asctime&quot;: &quot;2022-02-15 23:27:21,250&quot;, &quot;levelname&quot;: &quot;INFO&quot;, &quot;module&quot;: &quot;urls&quot;, &quot;process&quot;: 9568, &quot;thread&quot;: 9824, &quot;message&quot;: &quot;\u30e1\u30c3\u30bb\u30fc\u30b8&quot;}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306f\u30ed\u30b0\u3092JSON\u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306bJSON\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002 JSON Lines\u5f62\u5f0f\u3067\u306e\u30ed\u30b0\u51fa\u529b \u30ed\u30b0\u306e\u5f62\u5f0f\u306f\u4f1d\u7d71\u7684\u306a\u30b9\u30da\u30fc\u30b9\u533a\u5207\u308a\u304b\u3089LT...<\/p>\n","protected":false},"author":1,"featured_media":3795,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[215,216,144],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/3792"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=3792"}],"version-history":[{"count":9,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/3792\/revisions"}],"predecessor-version":[{"id":3802,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/3792\/revisions\/3802"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3795"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=3792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=3792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=3792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}