{"id":609,"date":"2024-05-16T18:59:53","date_gmt":"2024-05-16T10:59:53","guid":{"rendered":"http:\/\/www.madbull.site\/?p=609"},"modified":"2024-08-22T11:14:51","modified_gmt":"2024-08-22T03:14:51","slug":"python%e6%97%a5%e5%bf%97%e5%af%b9%e8%b1%a1%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=609","title":{"rendered":"Python\u65e5\u5fd7\u5bf9\u8c61\u4f7f\u7528"},"content":{"rendered":"\n<div class=\"wp-block-group has-global-padding is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><strong>\u751f\u6210logger\u5bf9\u8c61\uff0c\u5728\u5176\u4ed6python\u6587\u4ef6\u4e2d\u5f15\u7528\u975e\u5e38\u65b9\u4fbf\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n\nimport logging\nfrom concurrent_log_handler import ConcurrentRotatingFileHandler\n\n# \u7cfb\u7edf\u65e5\u5fd7\u7ea7\u522b 5\u90091\nLOGGER_LEVEL = logging.DEBUG\n#LOGGER_LEVEL = logging.INFO\n#LOGGER_LEVEL = logging.WARN\n#LOGGER_LEVEL = logging.ERROR\n#LOGGER_LEVEL = logging.CRITICAL\n\ndef get_logger( filename=None, log_level=LOGGER_LEVEL, max_bytes=50000000, bk_cnt=22 ) :\n\n    # 1\u3001\u83b7\u53d6logger\u5bf9\u8c61\n    logger = logging.getLogger()\n    # 2\u3001\u8bbe\u7f6e\u544a\u8b66\u7b49\u7ea7\uff0c\u5927\u4e8e\u7b49\u4e8e\u6b64\u7b49\u7ea7\u5c31\u544a\u8b66\n    logger.setLevel(log_level)\n\n    # 3\u3001\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\uff0clogger\u5bf9\u8c61\u662f\u5171\u7528\u7684\uff0c\u5982\u679c\u5728\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u8fc7\uff0c\u5c31\u76f4\u63a5\u8fd4\u56de\uff0c\u9632\u6b62\u591a\u4e2apython\u6587\u4ef6\u91cd\u590d\u8bbe\u7f6e\uff0c\u5982\u679c\u91cd\u590d\u4e0b\u8fb9\u7684\u7b2c4\u6b65\uff0c\u4f1a\u589e\u52a0\u591a\u4e2a\u8f93\u51fa\u6a21\u5757\uff0c\u5bfc\u81f4\u6570\u636e\u8f93\u51fa\u591a\u4efd\u3002\n    if len(logger.handlers) != 0 :\n        return logger\n\n    if filename == None :\n        filename = \"mylog.log\"\n\n    # 4\u3001\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\n    handler = ConcurrentRotatingFileHandler( filename, maxBytes=max_bytes, backupCount=bk_cnt )\n    formatter = logging.Formatter(\"%(asctime)s - pid:%(process)d %(filename)s:%(funcName)s(%(lineno)s) - %(levelname)s : %(message)s\")\n    handler.setFormatter(formatter)\n    logger.addHandler(handler)\n\n    # 5\u3001\u8fd4\u56de\u6b64\u5bf9\u8c61\n    return logger\n\nlogger=get_logger()\n\nif \"__main__\" == __name__ :\n    logger.debug(\"dddddddddddddd\")\n    logger.info(\"iiiiiiiiiiiii\")\n    logger.error(\"eeeeeeeeeeee\")\n    logger.critical(\"ccccccccccccc\")<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff0c\u65e5\u5fd7\u7b49\u7ea7\u4e3aINFO\uff0c\u6240\u4ee5DEBUG\u7ea7\u522b\u7684\u4e0d\u5c55\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"111\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/logger.png\" alt=\"\" class=\"wp-image-610\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/logger.png 734w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/05\/logger-300x45.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p># 1\u3001\u83b7\u53d6logger\u5bf9\u8c61 # 2\u3001\u8bbe\u7f6e\u544a\u8b66\u767b\u8bb0\uff0c\u5927\u4e8e\u7b49\u4e8e\u6b64\u7b49\u7ea7\u5c31\u544a\u8b66 # 3\u3001\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\uff0clogger\u5bf9\u8c61\u662f\u5171\u7528\u7684\uff0c\u5982\u679c\u5728\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u8fc7\uff0c\u5c31\u76f4\u63a5\u8fd4\u56de\uff0c\u9632\u6b62\u591a\u4e2apython\u6587\u4ef6\u91cd\u590d\u8bbe\u7f6e\uff0c\u5982\u679c\u91cd\u590d\u4e0b\u8fb9\u7684\u7b2c4\u6b65\uff0c\u4f1a\u589e\u52a0\u591a\u4e2a\u8f93\u51fa\u6a21\u5757\uff0c\u5bfc\u81f4\u6570\u636e\u8f93\u51fa\u591a\u4efd\u3002 # 4\u3001\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[206,142],"class_list":["post-609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-logger","tag-python"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=609"}],"version-history":[{"count":2,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/609\/revisions\/983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}