微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

golang之zap探索

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

}

原文地址:https://www.jb51.cc/go/188068.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐