{"id":208185,"date":"2025-07-08T09:17:55","date_gmt":"2025-07-08T01:17:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208185\/"},"modified":"2025-07-08T09:17:55","modified_gmt":"2025-07-08T01:17:55","slug":"uber-zap-%e8%ae%b0%e5%bd%95%e5%99%a8%e6%9c%aa%e5%9c%a8%e6%97%a5%e5%bf%97%e8%af%ad%e5%8f%a5%e4%b8%ad%e6%89%93%e5%8d%b0%e5%91%bc%e5%8f%ab%e8%80%85%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208185\/","title":{"rendered":"Uber Zap \u8bb0\u5f55\u5668\u672a\u5728\u65e5\u5fd7\u8bed\u53e5\u4e2d\u6253\u5370\u547c\u53eb\u8005\u4fe1\u606f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Uber Zap \u8bb0\u5f55\u5668\u672a\u5728\u65e5\u5fd7\u8bed\u53e5\u4e2d\u6253\u5370\u547c\u53eb\u8005\u4fe1\u606f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 23:45:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aUber Zap \u8bb0\u5f55\u5668\u672a\u5728\u65e5\u5fd7\u8bed\u53e5\u4e2d\u6253\u5370\u547c\u53eb\u8005\u4fe1\u606f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u8bd5\u56fe\u4f7f\u7528\u914d\u7f6e\u7684\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u540c\u65f6\u5c06\u76f8\u540c\u7684\u6d88\u606f\u53d1\u9001\u5230\u63a7\u5236\u53f0\u548c\u65e5\u5fd7\u6587\u4ef6\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u6211\u60f3\u663e\u793a\u547c\u53eb\u8005\u4fe1\u606f\uff0c\u4f46\u5373\u4f7f\u6211\u6309\u7167\u6587\u6863\u4e2d\u7684\u5efa\u8bae\u4f7f\u7528\u4e86 <code>caller<\/code> \u952e\uff0c\u4e5f\u4e0d\u4f1a\u663e\u793a\u76f8\u540c\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u76f8\u540c\u7684\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre>package main\n\nimport (\n    \"os\"\n    \"time\"\n\n    \"go.uber.org\/zap\"\n    \"go.uber.org\/zap\/zapcore\"\n    \"gopkg.in\/natefinch\/lumberjack.v2\"\n)\n\nvar logLevelSeverity = map[zapcore.Level]string{\n    zapcore.DebugLevel:  \"DEBUG\",\n    zapcore.InfoLevel:   \"INFO\",\n    zapcore.WarnLevel:   \"WARNING\",\n    zapcore.ErrorLevel:  \"ERROR\",\n    zapcore.DPanicLevel: \"CRITICAL\",\n    zapcore.PanicLevel:  \"ALERT\",\n    zapcore.FatalLevel:  \"EMERGENCY\",\n}\n\nfunc SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {\n    enc.AppendString(t.Format(\"Jan 01, 2006  15:04:05\"))\n}\n\nfunc CustomEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {\n    enc.AppendString(logLevelSeverity[level])\n}\n\nfunc CustomLevelFileEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {\n    enc.AppendString(\"[\" + logLevelSeverity[level] + \"]\")\n}\n\nfunc main() {\n\n    w := zapcore.AddSync(&amp;lumberjack.Logger{\n        Filename:   \"temp1.log\",\n        MaxSize:    1024,\n        MaxBackups: 20,\n        MaxAge:     28,\n        Compress:   true,\n    })\n\n    \/\/Define config for the console output\n    cfgConsole := zapcore.EncoderConfig{\n        MessageKey:   \"message\",\n        LevelKey:     \"severity\",\n        EncodeLevel:  CustomEncodeLevel,\n        TimeKey:      \"time\",\n        EncodeTime:   SyslogTimeEncoder,\n        CallerKey:    \"caller\",\n        EncodeCaller: zapcore.FullCallerEncoder,\n    }\n    cfgFile := zapcore.EncoderConfig{\n        MessageKey:   \"message\",\n        LevelKey:     \"severity\",\n        EncodeLevel:  CustomLevelFileEncoder,\n        TimeKey:      \"time\",\n        EncodeTime:   SyslogTimeEncoder,\n        CallerKey:    \"caller\",\n        EncodeCaller: zapcore.FullCallerEncoder,\n    }\n\n    consoleDebugging := zapcore.Lock(os.Stdout)\n    \/\/consoleError := zapcore.Lock(os.Stderr)\n    core := zapcore.NewTee(\n        zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel),\n        zapcore.NewCore(zapcore.NewJSONEncoder(cfgConsole), consoleDebugging, zap.DebugLevel),\n        \/\/zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), consoleError, zap.ErrorLevel),\n    )\n    \/\/core := zapcore.NewCore(zapcore.NewConsoleEncoder(encConsole), w, zap.DebugLevel)\n    wlogger := zap.New(core)\n    wlogger.Debug(\"Sample debug for log file and console\")\n    wlogger.Warn(\"An warning message example\")\n    wlogger.Info(\"An info level message\")\n    coreFile := zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel)\n    flogger := zap.New(coreFile)\n    flogger.Debug(\"An exclusive message for file\")\n    \/\/output\n    \/\/{\"severity\":\"DEBUG\",\"time\":\"Nov 11, 2018  20:24:11\",\"message\":\"Sample debug for log file and console\"}\n    \/\/{\"severity\":\"WARNING\",\"time\":\"Nov 11, 2018  20:24:11\",\"message\":\"An warning message example\"}\n    \/\/{\"severity\":\"INFO\",\"time\":\"Nov 11, 2018  20:24:11\",\"message\":\"An info level message\"}\n}<\/pre>\n<p>\u6709\u4ec0\u4e48\u60f3\u6cd5\u4e3a\u4ec0\u4e48\u4e0d\u663e\u793a\u6765\u7535\u8005\u4fe1\u606f\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6839\u636e\u6587\u6863 \u60a8\u53ef\u4ee5\u5728\u521b\u5efa\u8bb0\u5f55\u5668\u65f6\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre>wlogger := zap.new(core, zap.addcaller())<\/pre>\n<p><strong>\u66f4\u65b0\u8bc4\u8bba\u7b54\u6848<\/strong><br \/> \u60a8\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u8c03\u7528\u8005\u7f16\u7801\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre>func mycaller(caller zapcore.entrycaller, enc zapcore.primitivearrayencoder) {\n    enc.appendstring(filepath.base(caller.fullpath()))\n}<\/pre>\n<p>\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<em>cfgconsole<\/em>\u548c<em>cfgfile<\/em><\/p>\n<p>\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8df3\u8fc7\u8c03\u7528\u8005\u7684\u8c03\u7528\u5806\u6808\u6df1\u5ea6\u3002 <\/p>\n<pre>\/\/ before\nzap.New(core, zap.AddCaller())\n\/\/ output: {\"level\":\"panic\",\"time\":\"2020-06-09T16:08:25.677+0800\",\"line\":\"log\/log.go:118\",\"msg\":\"before\")}\n\n\/\/ add caller skip\nzap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))\n\/\/ output: {\"level\":\"panic\",\"time\":\"2020-06-09T16:47:39.559+0800\",\"line\":\"client\/client.go:29\",\"msg\":\"after\"}\n<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aUber Zap \u8bb0\u5f55\u5668\u672a\u5728\u65e5\u5fd7\u8bed\u53e5\u4e2d\u6253\u5370\u547c\u53eb\u8005\u4fe1\u606f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208185","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208185"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208185\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}