golang之zap探索

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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 mainimport (    "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

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.