golang之zap探索

京东技术 · · 4683 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

GO Web 编程:http://www.kancloud.cn/kancloud/web-application-with-golang/44105

我的golang工程:https://github.com/javahongxi/go.web.red.git

 

uber zap test

 

 

package main

import (
    "go.uber.org/zap"
    "time"
    "go.uber.org/zap/zapcore"
    "net/http"
    "bufio"
    "os"
)

func main() {
    //logger, _ := zap.NewProduction()
    //defer logger.Sync() // flushes buffer, if any
    //sugar := logger.Sugar()
    //url := "www.baidu.com"
    //sugar.Infow("Failed to fetch URL.",
    //    // Structured context as loosely-typed key-value pairs.
    //    "url", url,
    //    "attempt", 3,
    //    "backoff", time.Second,
    //)
    //sugar.Infof("Failed to fetch URL: %s", url)

    encoder_cfg := zapcore.EncoderConfig{
        // Keys can be anything except the empty string.
        TimeKey:        "T",
        LevelKey:       "L",
        NameKey:        "N",
        CallerKey:      "C",
        MessageKey:     "M",
        StacktraceKey:  "S",
        LineEnding:     zapcore.DefaultLineEnding,
        EncodeLevel:    zapcore.CapitalLevelEncoder,
        EncodeTime:     TimeEncoder,
        EncodeDuration: zapcore.StringDurationEncoder,
        EncodeCaller:   zapcore.ShortCallerEncoder,
    }

    Curr_level := zap.NewAtomicLevelAt(zap.DebugLevel)

    go func() {
        http.ListenAndServe(":9090", &Curr_level)
    }()


    custom_cfg := zap.Config{
        Level:            Curr_level,
        Development:      true,
        Encoding:         "console",
        EncoderConfig:    encoder_cfg,
        OutputPaths:      []string{"stderr", "qihu-secret-business.log"},
        ErrorOutputPaths: []string{"stderr"},
    }


    url := "www.baidu.com"

    logger, _ := custom_cfg.Build()
    new_logger := logger.Named("qihu-secret-business")
    defer new_logger.Sync()

    new_logger.Debug("adv_event_type_handle", zap.String("a", "1"))
    new_logger.Info("adv_event_type_handle",
        // Structured context as strongly-typed Field values.
        zap.String("url", url),
        zap.Int("attempt", 3),
        zap.Duration("backoff", time.Second),
    )

    reader := bufio.NewReader(os.Stdin)
    for {
        data, _, _ := reader.ReadLine()
        command := string(data)
        if command == "start" {
            break
        }
    }


    new_logger.Debug("adv_event_type_handle", zap.String("b", "2"))
    new_logger.Info("adv_event_type_handle", zap.String("c", "3"))
}

func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
    enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")
}

 

 

[2017-05-23 12:08:26]DEBUGadv_os_businesszap_t/zap_t.go:63adv_event_type_handle{"a": "1"}

[2017-05-23 12:08:26]INFOadv_os_businesszap_t/zap_t.go:69adv_event_type_handle{"url": "www.baidu.com", "attempt": 3, "backoff": "1s"}

 

其他:进程启动管理supervisor, 日志分割logrotate, 性能监控https://github.com/grafana/grafana

 

/etc/supervisor.conf

[program:simpletest]

command=/home/shenhongxi/go/bin/a

autostart=true

autorestart=true

startsecs=10

 

;logfile=/home/shenhongxi/log/simpletest.log

 

/etc/logrotate.d/simpletest

/home/shenhongxi/go/*.log {

  missingok

  notifempty

  nocompress

  daily

  rotate 5

  size 204800

}

 


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:京东技术

查看原文:golang之zap探索

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4683 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传