PageSourceSearch

https://nonebot.dev/assets/js/26706289.ea9403c3.js

js nonebot.dev collected 2026-10-03 23:19:50 UTC 9,643 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunknonebot=self.webpackChunknonebot||[]).push([["3808"],{86784(e,n,o){o.r(n),o.d(n,{metadata:()=>r,default:()=>g,frontMatter:()=>l,contentTitle:()=>d,toc:()=>t,assets:()=>c});var r=JSON.parse('{"id":"appendices/log","title":"\u65E5\u5FD7","description":"\u8BB0\u5F55\u4E0E\u63A7\u5236\u65E5\u5FD7","source":"@site/versioned_docs/version-2.4.3/appendices/log.md","sourceDirName":"appendices","slug":"/appendices/log","permalink":"/docs/2.4.3/appendices/log","draft":false,"unlisted":false,"editUrl":"https://github.com/nonebot/nonebot2/edit/master/website/versioned_docs/version-2.4.3/appendices/log.md","tags":[],"version":"2.4.3","lastUpdatedBy":"pre-commit-ci[bot]","lastUpdatedAt":1789135437000,"sidebarPosition":6,"frontMatter":{"sidebar_position":6,"description":"\u8BB0\u5F55\u4E0E\u63A7\u5236\u65E5\u5FD7","options":{"menu":[{"category":"appendices","weight":70}]}},"sidebar":"tutorial","previous":{"title":"\u6743\u9650\u63A7\u5236","permalink":"/docs/2.4.3/appendices/permission"},"next":{"title":"\u4E8B\u4EF6\u7C7B\u578B\u4E0E\u91CD\u8F7D","permalink":"/docs/2.4.3/appendices/overload"}}'),s=o(62540),i=o(43023);let l={sidebar_position:6,description:"\u8BB0\u5F55\u4E0E\u63A7\u5236\u65E5\u5FD7",options:{menu:[{category:"appendices",weight:70}]}},d="\u65E5\u5FD7",c={},t=[{value:"\u8BB0\u5F55\u65E5\u5FD7",id:"\u8BB0\u5F55\u65E5\u5FD7",level:2},{value:"\u81EA\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA",id:"\u81EA\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA",level:2},{value:"\u91CD\u5B9A\u5411 logging \u65E5\u5FD7",id:"\u91CD\u5B9A\u5411-logging-\u65E5\u5FD7",level:2}];function a(e){let n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",strong:"strong",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"\u65E5\u5FD7",children:"\u65E5\u5FD7"})}),"\n",(0,s.jsxs)(n.p,{children:["\u65E0\u8BBA\u662F\u5728\u5F00\u53D1\u8FD8\u662F\u5728\u751F\u4EA7\u73AF\u5883\u4E2D\uFF0C\u65E5\u5FD7\u90FD\u662F\u4E00\u4E2A\u91CD\u8981\u7684\u529F\u80FD\uFF0C\u53EF\u4EE5\u5E2E\u52A9\u6211\u4EEC\u4E86\u89E3\u8FD0\u884C\u72B6\u51B5\u3001\u6392\u67E5\u95EE\u9898\u7B49\u3002\u867D\u7136\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528 ",(0,s.jsx)(n.code,{children:"print"})," \u6765\u5C06\u9700\u8981\u7684\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0\uFF0C\u4F46\u662F\u8FD9\u79CD\u65B9\u5F0F\u96BE\u4EE5\u63A7\u5236\uFF0C\u800C\u4E14\u4E0D\u5229\u4E8E\u65E5\u5FD7\u7684\u5F52\u6863\u3001\u5206\u6790\u7B49\u3002NoneBot \u4F7F\u7528\u4F18\u79C0\u7684 ",(0,s.jsx)(n.a,{href:"https://loguru.readthedocs.io/",children:"Loguru"})," \u5E93\u6765\u8FDB\u884C\u65E5\u5FD7\u8BB0\u5F55\u3002"]}),"\n",(0,s.jsx)(n.h2,{id:"\u8BB0\u5F55\u65E5\u5FD7",children:"\u8BB0\u5F55\u65E5\u5FD7"}),"\n",(0,s.jsxs)(n.p,{children:["\u6211\u4EEC\u53EF\u4EE5\u4ECE NoneBot \u4E2D\u5BFC\u5165 ",(0,s.jsx)(n.code,{children:"logger"})," \u5BF9\u8C61\uFF0C\u7136\u540E\u4F7F\u7528 ",(0,s.jsx)(n.code,{children:"logger"})," \u5BF9\u8C61\u7684\u65B9\u6CD5\u6765\u8BB0\u5F55\u65E5\u5FD7\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:'from nonebot import logger\n\nlogger.trace("This is a trace message")\nlogger.debug("This is a debug message")\nlogger.info("This is an info message")\nlogger.success("This is a success message")\nlogger.warning("This is a warning message")\nlogger.error("This is an error message")\nlogger.critical("This is a critical message")\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u6211\u4EEC\u4EC5\u9700\u4E00\u884C\u4EE3\u7801\u5373\u53EF\u8BB0\u5F55\u5BF9\u5E94\u7EA7\u522B\u7684\u65E5\u5FD7\u3002\u65E5\u5FD7\u53EF\u4EE5\u901A\u8FC7\u914D\u7F6E ",(0,s.jsxs)(n.a,{href:"/docs/2.4.3/appendices/config#log-level",children:[(0,s.jsx)(n.code,{children:"LOG_LEVEL"})," \u914D\u7F6E\u9879"]}),"\u6765\u8FC7\u6EE4\u8F93\u51FA\u7B49\u7EA7\uFF0C\u63A7\u5236\u53F0\u4E2D\u4EC5\u4F1A\u8F93\u51FA\u5927\u4E8E\u7B49\u4E8E ",(0,s.jsx)(n.code,{children:"LOG_LEVEL"})," \u7684\u65E5\u5FD7\u3002\u9ED8\u8BA4\u7684 ",(0,s.jsx)(n.code,{children:"LOG_LEVEL"})," \u4E3A ",(0,s.jsx)(n.code,{children:"INFO"}),"\uFF0C\u5373\u53EA\u4F1A\u8F93\u51FA ",(0,s.jsx)(n.code,{children:"INFO"}),"\u3001",(0,s.jsx)(n.code,{children:"SUCCESS"}),"\u3001",(0,s.jsx)(n.code,{children:"WARNING"}),"\u3001",(0,s.jsx)(n.code,{children:"ERROR"}),"\u3001",(0,s.jsx)(n.code,{children:"CRITICAL"})," \u7EA7\u522B\u7684\u65E5\u5FD7\u3002"]}),"\n",(0,s.jsxs)(n.p,{children:["\u5982\u679C\u9700\u8981\u8BB0\u5F55 ",(0,s.jsx)(n.code,{children:"Exception traceback"})," \u65E5\u5FD7\uFF0C\u53EF\u4EE5\u5411 ",(0,s.jsx)(n.code,{children:"logger"})," \u6DFB\u52A0 ",(0,s.jsx)(n.code,{children:"exception"})," \u9009\u9879\uFF1A"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",metastring:"{4}",children:'try:\n    1 / 0\nexcept ZeroDivisionError:\n    logger.opt(exception=True).error("ZeroDivisionError")\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u5982\u679C\u9700\u8981\u8F93\u51FA\u5F69\u8272\u65E5\u5FD7\uFF0C\u53EF\u4EE5\u5411 ",(0,s.jsx)(n.code,{children:"logger"})," \u6DFB\u52A0 ",(0,s.jsx)(n.code,{children:"colors"})," \u9009\u9879\uFF1A"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:'logger.opt(colors=True).warning("We got a <red>BIG</red> problem")\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u66F4\u591A\u65E5\u5FD7\u8BB0\u5F55\u65B9\u6CD5\u8BF7\u53C2\u8003 ",(0,s.jsx)(n.a,{href:"https://loguru.readthedocs.io/",children:"Loguru \u6587\u6863"}),"\u3002"]}),"\n",(0,s.jsx)(n.h2,{id:"\u81EA\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA",children:"\u81EA\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA"}),"\n",(0,s.jsxs)(n.p,{children:["NoneBot \u5728\u542F\u52A8\u65F6\u4F1A\u
16DFB\u52A0\u4E00\u4E2A\u9ED8\u8BA4\u7684\u65E5\u5FD7\u5904\u7406\u5668\uFF0C\u8BE5\u5904\u7406\u5668\u4F1A\u5C06\u65E5\u5FD7\u8F93\u51FA\u5230",(0,s.jsx)(n.strong,{children:"stdout"}),"\uFF0C\u5E76\u4E14\u6839\u636E ",(0,s.jsx)(n.code,{children:"LOG_LEVEL"})," \u914D\u7F6E\u9879\u8FC7\u6EE4\u65E5\u5FD7\u7B49\u7EA7\u3002"]}),"\n",(0,s.jsx)(n.p,{children:"\u9ED8\u8BA4\u7684\u65E5\u5FD7\u683C\u5F0F\u4E3A\uFF1A"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-text",children:"<g>{time:MM-DD HH:mm:ss}</g> [<lvl>{level}</lvl>] <c><u>{name}</u></c> | {message}\n"})}),"\n",(0,s.jsxs)(n.p,{children:["\u6211\u4EEC\u53EF\u4EE5\u4ECE ",(0,s.jsx)(n.code,{children:"nonebot.log"})," \u6A21\u5757\u5BFC\u5165\u4EE5\u4F7F\u7528 NoneBot \u7684\u9ED8\u8BA4\u683C\u5F0F\u548C\u8FC7\u6EE4\u5668\uFF1A"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:"from nonebot.log import default_format, default_filter\n"})}),"\n",(0,s.jsxs)(n.p,{children:["\u5982\u679C\u9700\u8981\u81EA\u5B9A\u4E49\u65E5\u5FD7\u683C\u5F0F\uFF0C\u6211\u4EEC\u9700\u8981\u79FB\u9664 NoneBot \u9ED8\u8BA4\u7684\u65E5\u5FD7\u5904\u7406\u5668\u5E76\u6DFB\u52A0\u65B0\u7684\u65E5\u5FD7\u5904\u7406\u5668\u3002\u4F8B\u5982\uFF0C\u5728\u673A\u5668\u4EBA\u5165\u53E3\u6587\u4EF6\u4E2D ",(0,s.jsx)(n.code,{children:"nonebot.init"})," \u4E4B\u524D\u6DFB\u52A0\u4EE5\u4E0B\u5185\u5BB9\uFF1A"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",metastring:"title=bot.py",children:'from nonebot.log import logger_id\n\n# \u79FB\u9664 NoneBot \u9ED8\u8BA4\u7684\u65E5\u5FD7\u5904\u7406\u5668\nlogger.remove(logger_id)\n# \u6DFB\u52A0\u65B0\u7684\u65E5\u5FD7\u5904\u7406\u5668\nlogger.add(\n    sys.stdout,\n    level=0,\n    diagnose=True,\n    format="<g>{time:MM-DD HH:mm:ss}</g> [<lvl>{level}</lvl>] <c><u>{name}</u></c> | {message}",\n    filter=default_filter,\n)\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u5982\u679C\u60F3\u8981\u8F93\u51FA\u65E5\u5FD7\u5230\u6587\u4EF6\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528 ",(0,s.jsx)(n.code,{children:"logger.add"})," \u65B9\u6CD5\u6DFB\u52A0\u6587\u4EF6\u5904\u7406\u5668\uFF1A"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",metastring:"title=bot.py",children:'logger.add("error.log", level="ERROR", format=default_format, rotation="1 week")\n'})}),"\n",(0,s.jsxs)(n.p,{children:["\u66F4\u591A\u65E5\u5FD7\u5904\u7406\u5668\u7684\u4F7F\u7528\u65B9\u6CD5\u8BF7\u53C2\u8003 ",(0,s.jsx)(n.a,{href:"https://loguru.readthedocs.io/",children:"Loguru \u6587\u6863"}),"\u3002"]}),"\n",(0,s.jsx)(n.h2,{id:"\u91CD\u5B9A\u5411-logging-\u65E5\u5FD7",children:"\u91CD\u5B9A\u5411 logging \u65E5\u5FD7"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.code,{children:"logging"})," \u662F Python \u6807\u51C6\u5E93\u4E2D\u7684\u65E5\u5FD7\u6A21\u5757\uFF0CNoneBot \u63D0\u4F9B\u4E86\u4E00\u4E2A logging handler \u7528\u4E8E\u5C06 ",(0,s.jsx)(n.code,{children:"logging"})," \u65E5\u5FD7\u91CD\u5B9A\u5411\u5230 ",(0,s.jsx)(n.code,{children:"loguru"})," \u5904\u7406\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:"from nonebot.log import LoguruHandler\n\n# root logger \u6DFB\u52A0 LoguruHandler\nlogging.basicConfig(handlers=[LoguruHandler()])\n# \u6216\u8005\u4E3A\u5176\u4ED6 logging.Logger \u6DFB\u52A0 LoguruHandler\nlogger.addHandler(LoguruHandler())\n"})})]})}function g(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(a,{...e})}):a(e)}},43023(e,n,o){o.d(n,{R:()=>l,x:()=>d});var r=o(63696);let s={},i=r.createContext(s);function l(e){let n=r.useContext(i);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:l(e.components),r.createElement(i.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.