{"id":9051,"date":"2023-12-21T11:21:54","date_gmt":"2023-12-21T03:21:54","guid":{"rendered":"\/?p=9051"},"modified":"2023-12-21T11:23:23","modified_gmt":"2023-12-21T03:23:23","slug":"fastapi%e5%85%a8%e5%b1%80%e6%97%a5%e5%bf%97%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"\/?p=9051","title":{"rendered":"fastapi\u5168\u5c40\u65e5\u5fd7\u7ba1\u7406"},"content":{"rendered":"<h3>\u6982\u8ff0<\/h3>\n<p>\u4f7f\u7528fastapi\u7684\u65e5\u5fd7\u63a5\u53e3\uff0c\u5176\u4ed6\u6587\u4ef6\u5185\u7684<code>logger<\/code>\u4e5f\u80fd\u6309\u6587\u4ef6\u7b49\u914d\u7f6e\u4fe1\u606f\u5b58\u50a8\u3002<\/p>\n<p>\u9996\u5148\u5728<code>server.py<\/code>\u4e4b\u7c7b\u7684\u5165\u53e3\u6587\u4ef6\u5185\u5199\u597d\uff0c\u542f\u52a8\u65f6\u52a0\u8f7d\u65e5\u5fd7\u5668\uff0c\u5728\u5165\u53e3\u6587\u4ef6\u548c\u5176\u4ed6\u5f15\u7528\u7684\u6587\u4ef6\u7684\u6587\u5934\u5199\u597d<\/p>\n<pre><code class=\"language-python\">import logging\nlogger = logging.getLogger()<\/code><\/pre>\n<p><strong>\u4e0a\u4ee3\u7801\uff1a<\/strong><\/p>\n<h3>server.py<\/h3>\n<p>\u91cd\u70b9\u5728\u4e8e<\/p>\n<pre><code class=\"language-python\">uvicorn.run(&quot;regulation_standardization_server:app&quot;, \n            host=&quot;0.0.0.0&quot;, port=8057,\n            log_config=log_config, log_level=&#039;debug&#039;)<\/code><\/pre>\n<p>\u7684<code>log_config=log_config, log_level=&#039;debug&#039;<\/code><\/p>\n<p><code>server.py<\/code><\/p>\n<pre><code class=\"language-python\"># -*- coding: UTF-8 -*-\n&quot;&quot;&quot;=========================================================\n@Project -&gt; File: regulation_standardization -&gt; regulation_standardization_server\n@IDE: PyCharm\n@author: lxc\n@date: 2023-10-27 \u4e0b\u5348 4:34\n@Desc:\n1-\u529f\u80fd\u63cf\u8ff0\uff1a\n\n2-\u5b9e\u73b0\u6b65\u9aa4\n    1-\n&quot;&quot;&quot;\nfrom fastapi import FastAPI, File, UploadFile\nfrom text_to_tree import text_to_tree\nfrom utils.log_util import log_config\nimport os\nimport uuid\nimport uvicorn\nimport traceback\nimport logging\nlogger = logging.getLogger()\napp = FastAPI()\n\ndef parse_file(file_path):\n    # \u5728\u8fd9\u91cc\u7f16\u5199\u5904\u7406\u6587\u4ef6\u7684\u4ee3\u7801\n    # \u8fd4\u56de\u5904\u7406\u540e\u7684\u7ed3\u679c\uff0c\u8fd9\u91cc\u5047\u8bbe\u7ed3\u679c\u4e3a\u4e00\u4e2a\u5b57\u5178\n    result = text_to_tree(file_path)\n    return result\n\n@app.post(&quot;\/doc_analysis&quot;)\nasync def create_upload_file(file: UploadFile = File(...)):\n    try:\n        # \u751f\u6210\u552f\u4e00\u7684UUID\u4f5c\u4e3a\u6587\u4ef6\u540d\n        unique_filename = str(uuid.uuid4())\n        file_name, file_extension = os.path.splitext(file.filename)\n        file_path = f&quot;temp\/{unique_filename}{file_extension}&quot;\n        logger.info(&quot;\u5f53\u524d\u6b63\u5728\u5904\u7406\u6587\u4ef6\uff1a\u3010%s\u3011&quot; % file_name)\n        # \u521b\u5efatemp\u6587\u4ef6\u5939\n        if not os.path.exists(&quot;temp&quot;):\n            os.makedirs(&quot;temp&quot;)\n        # \u5c06\u6587\u4ef6\u4fdd\u5b58\u5230\u4e34\u65f6\u8def\u5f84\n        with open(file_path, &quot;wb&quot;) as f:\n            f.write(await file.read())\n\n        # \u8c03\u7528\u89e3\u6790\u51fd\u6570\u5904\u7406\u6587\u4ef6\n        result = parse_file(file_path)\n        if not result.get(&quot;title&quot;):\n            result[&#039;title&#039;] = file_name\n        # \u5220\u9664\u4e34\u65f6\u6587\u4ef6\n        os.remove(file_path)\n\n        return {&#039;errcode&#039;: &#039;200&#039;, &#039;errmsg&#039;: &#039;&#039;, &#039;data&#039;: result}\n    except Exception as e:\n        traceback.print_exc()\n        return {&#039;errcode&#039;: &#039;400&#039;, &#039;errmsg&#039;: &#039;\u89e3\u6790\u9519\u8bef&#039;, &#039;data&#039;: &#039;&#039;}\n\nif __name__ == &#039;__main__&#039;:\n    uvicorn.run(&quot;regulation_standardization_server:app&quot;, host=&quot;0.0.0.0&quot;, port=8057,\n                log_config=log_config, log_level=&#039;debug&#039;)<\/code><\/pre>\n<h3>log_util<\/h3>\n<pre><code class=\"language-python\"># -*- coding: UTF-8 -*-\nimport os, sys, time\nlog_path = &#039;.\/logs&#039;\nif not os.path.exists(log_path):\n    os.mkdir(log_path)\nlog_config = {\n    &quot;version&quot;: 1,\n    &quot;disable_existing_loggers&quot;: False,\n    &quot;formatters&quot;: {\n        &quot;default&quot;: {\n            &quot;format&quot;: &#039;[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] &#039;\n                      &#039;[%(levelname)s]- %(message)s&#039;\n        }\n    },\n    &quot;handlers&quot;: {\n        &quot;all&quot;: {\n            &quot;level&quot;: &quot;DEBUG&quot;,\n            &quot;class&quot;: &quot;logging.handlers.TimedRotatingFileHandler&quot;,\n            &quot;formatter&quot;: &quot;default&quot;,\n            &quot;filename&quot;: os.path.join(log_path, &#039;all-{}.log&#039;.format(time.strftime(&#039;%Y-%m-%d&#039;))),\n            &quot;when&quot;: &quot;midnight&quot;,\n            &quot;backupCount&quot;: 1,\n            &#039;encoding&#039;: &#039;utf-8&#039;,  # \u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\n        },\n        &#039;error&#039;: {\n            &#039;level&#039;: &#039;ERROR&#039;,\n            &#039;class&#039;: &#039;logging.handlers.TimedRotatingFileHandler&#039;,\n            &#039;filename&#039;: os.path.join(log_path, &#039;error-{}.log&#039;.format(time.strftime(&#039;%Y-%m-%d&#039;))),\n            &#039;backupCount&#039;: 1,\n            &#039;formatter&#039;: &#039;default&#039;,  # \u8f93\u51fa\u683c\u5f0f\n            &#039;encoding&#039;: &#039;utf-8&#039;,  # \u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\n            &quot;when&quot;: &quot;midnight&quot;,\n        },\n        &#039;info&#039;: {\n            &#039;level&#039;: &#039;INFO&#039;,\n            &#039;class&#039;: &#039;logging.handlers.TimedRotatingFileHandler&#039;,\n            &#039;filename&#039;: os.path.join(log_path, &#039;info-{}.log&#039;.format(time.strftime(&#039;%Y-%m-%d&#039;))),\n            &#039;backupCount&#039;: 1,\n            &#039;formatter&#039;: &#039;default&#039;,\n            &#039;encoding&#039;: &#039;utf-8&#039;,  # \u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\n            &quot;when&quot;: &quot;midnight&quot;,\n        },\n        &quot;console_handler&quot;: {\n            &quot;class&quot;: &quot;logging.StreamHandler&quot;,\n            &quot;level&quot;: &quot;INFO&quot;,\n            &quot;formatter&quot;: &quot;default&quot;,\n            &quot;stream&quot;: &quot;ext:\/\/sys.stdout&quot;\n        }\n    },\n    &quot;root&quot;: {\n        &quot;level&quot;: &quot;DEBUG&quot;,\n        &quot;handlers&quot;: [&quot;all&quot;, &quot;error&quot;, &quot;info&quot;, &quot;console_handler&quot;],\n        &#039;propagate&#039;: True\n    }\n}<\/code><\/pre>\n<h3>\u5176\u4ed6\u6587\u4ef6<\/h3>\n<pre><code class=\"language-python\">import logging\nlogger = logging.getLogger()\n\n...\nlogger.info(11111111111)<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31ok\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>[rhino@localhost]$ ll logs\ntotal 20\n-rw-rw-r-- 1 rhino rhino 14974 Dec 21 11:14 all-2023-12-21.log\n-rw-rw-r-- 1 rhino rhino     0 Dec 21 11:11 error-2023-12-21.log\n-rw-rw-r-- 1 rhino rhino  3392 Dec 21 11:14 info-2023-12-21.log<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 \u4f7f\u7528fastapi\u7684\u65e5\u5fd7\u63a5\u53e3\uff0c\u5176\u4ed6\u6587\u4ef6\u5185\u7684logger\u4e5f\u80fd\u6309\u6587\u4ef6\u7b49\u914d\u7f6e\u4fe1\u606f\u5b58\u50a8\u3002 \u9996\u5148\u5728server.py\u4e4b\u7c7b\u7684\u5165\u53e3\u6587\u4ef6\u5185\u5199\u597d\uff0c\u542f   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[115],"tags":[116],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9051"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9051"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9051\/revisions"}],"predecessor-version":[{"id":9053,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9051\/revisions\/9053"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9051"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}