{"id":3199,"date":"2019-04-30T10:33:58","date_gmt":"2019-04-30T02:33:58","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=3199"},"modified":"2019-04-30T12:17:44","modified_gmt":"2019-04-30T04:17:44","slug":"go%e6%97%a5%e5%bf%97%e6%a1%86%e6%9e%b6zap%e9%85%8d%e7%bd%ae%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/3199","title":{"rendered":"go\u65e5\u5fd7\u6846\u67b6zap\u914d\u7f6e\u793a\u4f8b\uff08\u6b63\u5728\u4f7f\u7528\uff09"},"content":{"rendered":"<div class=\"author\">\n<div class=\"info\">\n<div class=\"meta\">\u5982\u679c\u4f60\u6b63\u5728\u5bf9golang\u7684\u65e5\u5fd7\u6846\u67b6\u505a\u9009\u578b\uff0c\u6211\u63a8\u8350zap<\/div>\n<\/div>\n<\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package log\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"go.uber.org\/zap\"\r\n    \"go.uber.org\/zap\/zapcore\"\r\n    \"gopkg.in\/natefinch\/lumberjack.v2\"\r\n    \"os\"\r\n    \"time\"\r\n)\r\n\r\nvar (\r\n    errorLogger *zap.SugaredLogger\r\n    atom zap.AtomicLevel\r\n)\r\n\r\nfunc TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {\r\n    enc.AppendString(t.Format(\"2006-01-02 15:04:05.000\"))\r\n}\r\n\r\nfunc init() {\r\n    atom = zap.NewAtomicLevel()\r\n    fileName := fmt.Sprintf(\".\/log\/%04d-%02d-%02d.log\", time.Now().Year(), time.Now().Month(), time.Now().Day())\r\n    syncWriter := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&amp;lumberjack.Logger{\r\n        Filename:   fileName,\r\n        MaxSize:    500, \/\/ megabytes\r\n        MaxBackups: 3,\r\n        MaxAge:     28, \/\/ days\r\n        LocalTime: true,\r\n    }))\r\n\r\n    encoderConfig := zapcore.EncoderConfig{\r\n        TimeKey:        \"T\",\r\n        LevelKey:       \"L\",\r\n        NameKey:        \"N\",\r\n        CallerKey:      \"C\",\r\n        MessageKey:     \"M\",\r\n        StacktraceKey:  \"S\",\r\n        LineEnding:     zapcore.DefaultLineEnding,\r\n        EncodeLevel:    zapcore.CapitalLevelEncoder,\r\n        EncodeTime:     TimeEncoder,\r\n        EncodeDuration: zapcore.StringDurationEncoder,\r\n        EncodeCaller:   zapcore.ShortCallerEncoder,\r\n    }\r\n\r\n    core := zapcore.NewCore(\r\n        zapcore.NewConsoleEncoder(encoderConfig),\r\n        syncWriter,\r\n        atom,\r\n    )\r\n    logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))\r\n    errorLogger = logger.Sugar()\r\n}\r\n\r\nfunc SetLogLevel(level zapcore.Level) {\r\n    atom.SetLevel(level)\r\n}\r\n\r\nfunc Debug(args ...interface{}) {\r\n    errorLogger.Debug(args...)\r\n}\r\n\r\nfunc Debugf(template string, args ...interface{}) {\r\n    errorLogger.Debugf(template, args...)\r\n}\r\n\r\nfunc Info(args ...interface{}) {\r\n    errorLogger.Info(args...)\r\n}\r\n\r\nfunc Infof(template string, args ...interface{}) {\r\n    errorLogger.Infof(template, args...)\r\n}\r\n\r\nfunc Warn(args ...interface{}) {\r\n    errorLogger.Warn(args...)\r\n}\r\n\r\nfunc Warnf(template string, args ...interface{}) {\r\n    errorLogger.Warnf(template, args...)\r\n}\r\n\r\nfunc Error(args ...interface{}) {\r\n    errorLogger.Error(args...)\r\n}\r\n\r\nfunc Errorf(template string, args ...interface{}) {\r\n    errorLogger.Errorf(template, args...)\r\n}\r\n\r\nfunc DPanic(args ...interface{}) {\r\n    errorLogger.DPanic(args...)\r\n}\r\n\r\nfunc DPanicf(template string, args ...interface{}) {\r\n    errorLogger.DPanicf(template, args...)\r\n}\r\n\r\nfunc Panic(args ...interface{}) {\r\n    errorLogger.Panic(args...)\r\n}\r\n\r\nfunc Panicf(template string, args ...interface{}) {\r\n    errorLogger.Panicf(template, args...)\r\n}\r\n\r\nfunc Fatal(args ...interface{}) {\r\n    errorLogger.Fatal(args...)\r\n}\r\n\r\nfunc Fatalf(template string, args ...interface{}) {\r\n    errorLogger.Fatalf(template, args...)\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u8c03\u7528<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">log.Infof(\"admin listening on %v\", addr)<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"show-content\" data-note-content=\"\">\n<div class=\"show-content-free\">\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">2019-04-30 10:36:23.397\tINFO\tcore\/admin.go:59\tadmin listening on :8101<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u6b63\u5728\u5bf9golang\u7684\u65e5\u5fd7\u6846\u67b6\u505a\u9009\u578b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[122],"class_list":["post-3199","post","type-post","status-publish","format-standard","hentry","tag-06-godocker"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=3199"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3199\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=3199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=3199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=3199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}