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

}

相关文章

程序目录结构 简单实现,用户登录后返回一个jwt的token,下次请求带上token请求用户信息接口并返回信息...
本篇博客的主要内容是用go写一个简单的Proof-of-Work共识机制,不涉及到网络通信环节,只是一个本地的简...
简介 默克尔树(MerkleTree)是一种典型的二叉树结构,其主要特点为: 最下面的叶节点包含存储数据或其...
接下来学习并发编程, 并发编程是go语言最有特色的地方, go对并发编程是原生支持. goroutine是go中最近本...
先普及一下, 什么是广度优先搜索 广度优先搜索类似于树的层次遍历。从图中的某一顶点出发,遍历每一个顶...
第一天: 接口的定义和实现 第二天: 一. go语言是面向接口编程. 在学习继承的时候说过, go语言只有封装,...