{"id":554034,"date":"2022-02-05T08:05:36","date_gmt":"2022-02-05T00:05:36","guid":{"rendered":"http:\/\/4563.org\/?p=554034"},"modified":"2022-02-05T08:05:36","modified_gmt":"2022-02-05T00:05:36","slug":"golang-%e6%97%a5%e5%bf%97%e7%bb%9f%e4%b8%80%e5%a4%84%e7%90%86%ef%bc%9f-7","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=554034","title":{"rendered":"golang \u65e5\u5fd7\u7edf\u4e00\u5904\u7406\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> golang \u65e5\u5fd7\u7edf\u4e00\u5904\u7406\uff1f <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : seaguest <\/span>  <span><i><\/i> 45<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u7cfb\u7edf\u5230\u5904\u90fd\u6253\u5370\u65e5\u5fd7\uff0c\u6709\u5404\u81ea\u6a21\u5757\u7684 logger \uff08 gorm \uff0cgin \uff0c\u4e1a\u52a1\u65e5\u5fd7&#8230;\uff09\uff0c\u76ee\u524d\u7528\u7684\u662f supervisor \u7ba1\u7406\uff0c\u4f46\u662f\u5f53\u7cfb\u7edf\u5e76\u53d1\u6570\u8f83\u9ad8\uff0c\u65e5\u5fd7\u6253\u5370\u8fc7\u591a\uff0c\u4f1a\u5bfc\u81f4 supervisor \u7684 cpu \u98da\u5347\u3002<\/p>\n<p>\u5c3d\u7ba1\u5404\u4e2a\u65e5\u5fd7\u90fd\u6709 logger \uff0c\u53ef\u4ee5\u8bbe\u7f6e SetOutput \u76f4\u63a5\u5199\u5230\u6587\u4ef6\uff0c\u4f46\u662f\u603b\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u8f93\u51fa\u5230 stdout \uff0c\u6240\u4ee5\u6ca1\u6709\u529e\u6cd5\u5b9e\u73b0\u7edf\u4e00\u5b9a\u5411\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u76ee\u524d\u627e\u5230\u7684\u4e00\u4e2a\u529e\u6cd5\u662f\uff1a<\/p>\n<pre><code>file, err := os.OpenFile(\"log\", os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm) syscall.Dup2(int(file.Fd()), int(os.Stdout.Fd())) syscall.Dup2(int(file.Fd()), int(os.Stderr.Fd()))  <\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06 stdout \u76f4\u63a5\u8f93\u51fa\u5230\u65e5\u5fd7\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u4f46\u662f\u611f\u89c9\u4e0d\u50cf\u662f\u4e00\u4e2a\u5e38\u89c4\u7684\u65b9\u6848\uff0c\u6709\u6ca1\u6709\u6bd4\u8f83\u597d\u7684\u65e5\u5fd7\u65b9\u6848\uff0c\u5c3d\u91cf\u7b80\u6d01\uff0c\u80fd\u505a\u5230\u6240\u6709\u65e5\u5fd7\u5199\u5165\u6587\u4ef6\uff0c\u6700\u597d\u80fd\u5b9e\u73b0\u81ea\u52a8 rotate \u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7061907\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : adoal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6362\u4e2a\u601d\u8def\uff0c\u4f60\u662f\u5199\u4e1a\u52a1\u7cfb\u7edf\u7684\u7a0b\u5e8f\u5458\uff0c\u8fd9\u4e9b\u201c\u8fd0\u7ef4\u201d\u6027\u8d28\u7684\u529f\u80fd\u4e0d\u8981\u5728\u4e1a\u52a1\u7cfb\u7edf\u91cc\u81ea\u5df1\u5199\uff0c\u8981\u5584\u7528\u73b0\u6709\u7684\u57fa\u7840\u8bbe\u65bd\u3002<br \/>\u5982\u679c\u53ea\u662f\u5728 Linux \u7528\uff0c\u4e0d\u8003\u8651\u517c\u5bb9 Windows \uff0c\u53ef\u4ee5\u6253\u5230 syslog \u53bb\uff0c\u7136\u540e\u7531 logrotate \u6765\u5377\u3002\u6b63\u5e38\u7684 Linux \u53d1\u884c\u7248\uff0c\u9ed8\u8ba4\u5b89\u88c5\u90fd\u5e26\u4e86 syslog \u670d\u52a1\uff08\u591a\u6570\u662f rsyslogd \uff09\u548c logrotate \uff0c\u4eba\u5bb6\u662f\u4e13\u4e1a\u5e72\u8fd9\u4e2a\u7684\uff0c\u4e45\u7ecf\u751f\u4ea7\u73af\u5883\u8003\u9a8c\u4e86\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7061908\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : masterclock <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u65e5\u5fd7\u5c31\u8f93\u51fa\u5230 stdout \u6216 stderr \uff0c\u5176\u4ed6\u5565\u90fd\u4e0d\u8981\u8003\u8651\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5e94\u8be5\u5173\u5fc3\u7684\u4e8b\u60c5\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7061909\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vvhhaaattt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> supervisor \u5728 std \u8f93\u51fa\u8fc7\u591a\u65f6\uff0c\u6027\u80fd\u786e\u5b9e\u4e0d\u592a\u597d\u3002<br \/>\u4f46\u4f60\u76f4\u63a5\u5199\u5230\u6587\u4ef6\uff0c\u8f6e\u8f6c\u4e5f\u9700\u8981\u6ce8\u610f\u3002<br \/>systemd \u662f\u4e2a\u597d\u4e1c\u897f\uff0c\u4e0d\u7ba1\u662f\u529f\u80fd\u8fd8\u662f\u6027\u80fd\u90fd\u5f88\u597d\uff0c\u800c\u4e14\u4f5c\u4e3a\u7cfb\u7edf\u670d\u52a1\uff0c\u80fd\u529b\u8303\u56f4\u4e5f\u66f4\u5927\uff0c\u4f46 supervisor \u7528\u60ef\u4e86\u7684\u4e5f\u5927\u6709\u4eba\u5728 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7061910\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : seaguest <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div>  @adoal <br \/>\u8c22\u8c22\u4f60\u7684\u5efa\u8bae\uff0c\u76ee\u524d\u56e0\u4e3a\u7cfb\u7edf\u5904\u4e8e\u521d\u7ea7\u9636\u6bb5\uff0c\u8fd8\u662f\u90e8\u7f72\u5728\u4e91\u670d\u52a1\u5668\u4e0a\uff0c\u4f46\u662f\u540e\u9762\u7a33\u5b9a\u4e86\u4f1a\u5f00\u59cb\u90e8\u7f72\u5230 K8S \u91cc\u3002<br \/>syslog \u7684\u8bdd\u4f1a\u4e0d\u4f1a\u548c\u7cfb\u7edf\u5176\u4ed6\u5e94\u7528\u6240\u6709\u7684\u65e5\u5fd7\u6df7\u5408\u5728\u4e00\u8d77\u4e86\uff1f\u5047\u5982\u673a\u5668\u4e0a\u90e8\u7f72\u4e86 A \uff0cB \uff0cC \u4e09\u4e2a\u670d\u52a1\uff0c\u4ed6\u4eec\u7684\u65e5\u5fd7\u4f1a\u6df7\u5728\u4e00\u8d77\u4e48?<br \/>\u53e6\u5916\u8003\u8651\u5230\u672a\u6765\u4f1a\u90e8\u7f72\u5728 Docker \u91cc\u9762\uff0c\u8981\u4e48\u9009\u62e9 loki \u65b9\u6848\uff0c\u6216\u8005 ELK \uff0c\u4f46\u662f\u8fd9\u6837\u4e5f\u9762\u4e34\u4e00\u4e2a\u95ee\u9898\uff0c\u7b2c\u4e09\u65b9\u5e93\u6253\u5370\u7684\u65e5\u5fd7\uff0c\u5c31\u53ef\u80fd\u4e22\u5931\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7061911\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : seaguest <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @vvhhaaattt <br \/>\u770b\u4e86\u51e0\u4e2a golang log-rotate \u7684\u5f00\u6e90\u9879\u76ee\uff0c\u5176\u5b9e\u4e5f\u4e0d\u590d\u6742\uff0c\u7528\u4e86\u51e0\u4e2a\u8bd5\u8bd5\uff0c\u6548\u679c\u6bd4 supervisor \u90a3\u4e2a\u597d\u592a\u591a\u4e86\u3002<br \/>systemd \u5355\u673a\u8fd8\u884c\uff0c\u4f46\u662f\u6211\u4eec\u540e\u9762\u7b49\u7cfb\u7edf\u6210\u719f\u4e86\u4f1a\u90e8\u7f72\u5230 k8s \uff0c\u5c31\u5fc5\u987b\u8003\u8651 Docker \u91cc\u9762\u7684\u65b9\u6848\uff0c<br \/>\u76ee\u524d\u7684\u8fd9\u79cd log.SetOutput()\u65b9\u5f0f\uff0c\u5982\u679c\u5199\u5165 ELK \u4e5f\u662f\u6bd4\u8f83\u65b9\u4fbf\u914d\u7f6e\u7684\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7061912\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : adoal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> syslog \u53ef\u4ee5\u914d\u7f6e\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u914d\u7f6e\u6253\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u3002<br \/>\u5bf9\u8001\u6d3e Unix\/Linux \u7528\u6237\uff08\u751a\u81f3\u53ea\u662f\u7528\u6237\uff0c\u4e0d\u662f\u804c\u4e1a\u8fd0\u7ef4\uff09\u6765\u8bf4\u8fd9\u4e9b\u90fd\u662f\u633a\u57fa\u672c\u7684\u6280\u80fd\u3002<br \/>\u4e0d\u8fc7\u8c8c\u4f3c\u4e91\u65f6\u4ee3\u7684 PPT \u67b6\u6784\u5e08\u4eec\u90fd\u4e0d\u592a\u628a\u8fd9\u4e9b\u8001\u6d3e\u7684\u73a9\u610f\u5f53\u56de\u4e8b <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u65e5\u5fd7\u7edf\u4e00\u5904\u7406\uff1f \u8cc7\u6df1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/554034"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=554034"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/554034\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=554034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=554034"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=554034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}