Skip to content

Commit

Permalink
refactor: use zap instead of logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
mutezebra committed Oct 14, 2024
1 parent ac69d0e commit 064c56b
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 168 deletions.
26 changes: 15 additions & 11 deletions cmd/template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ import (
"fmt"
"net"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/west2-online/fzuhelper-server/pkg/eszap"

"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/limit"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/elastic/go-elasticsearch"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
kitexzap "github.com/kitex-contrib/obs-opentelemetry/logging/zap"
etcd "github.com/kitex-contrib/registry-etcd"
trace "github.com/kitex-contrib/tracer-opentracing"
"github.com/sirupsen/logrus"

"github.com/west2-online/fzuhelper-server/cmd/template/dal"
"github.com/west2-online/fzuhelper-server/cmd/template/rpc"
"github.com/west2-online/fzuhelper-server/config"
"github.com/west2-online/fzuhelper-server/kitex_gen/template/templateservice"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/eslogrus"
"github.com/west2-online/fzuhelper-server/pkg/tracer"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)
Expand Down Expand Up @@ -67,7 +70,7 @@ func Init() {
// log
EsInit()
klog.SetLevel(klog.LevelDebug)
klog.SetLogger(kitexlogrus.NewLogger(kitexlogrus.WithHook(EsHookLog())))
klog.SetLogger(kitexzap.NewLogger(EsHookLog()...))
}

func main() {
Expand Down Expand Up @@ -115,13 +118,14 @@ func main() {
}
}

func EsHookLog() *eslogrus.ElasticHook {
hook, err := eslogrus.NewElasticHook(EsClient, config.Elasticsearch.Host, logrus.DebugLevel, serviceName)
if err != nil {
panic(err)
}

return hook
func EsHookLog() []kitexzap.Option {
hook := eszap.NewElasticHook(EsClient, config.Elasticsearch.Host, serviceName, zapcore.DebugLevel)
var options []kitexzap.Option
options = append(options, kitexzap.WithCoreEnc(hook.Enc()))
options = append(options, kitexzap.WithCoreLevel(hook.Lvl()))
options = append(options, kitexzap.WithCoreWs(hook.Ws()))
options = append(options, kitexzap.WithZapOptions(zap.Hooks(hook.Fire)))
return options
}

// InitEs 初始化es
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ require (
github.com/cloudwego/kitex v0.11.3
github.com/elastic/go-elasticsearch v0.0.0
github.com/fsnotify/fsnotify v1.7.0
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20240515092919-1f776656cb66
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241010093641-c58c6426d043
github.com/kitex-contrib/registry-etcd v0.2.5
github.com/kitex-contrib/tracer-opentracing v0.0.3
github.com/opentracing/opentracing-go v1.2.0
github.com/rabbitmq/amqp091-go v1.10.0
github.com/redis/go-redis/v9 v9.6.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.19.0
github.com/uber/jaeger-client-go v2.30.0+incompatible
github.com/west2-online/jwch v0.1.4
Expand Down Expand Up @@ -57,6 +56,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20240515092919-1f776656cb66 h1:KHEWSTCleXom38JQodofbpY7HNR2KQ4+ypIurCPFy/A=
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20240515092919-1f776656cb66/go.mod h1:naiLX9zwKBzVA41wtvvmtSZdbCihqIT9RnOb8GZv8A4=
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241010093641-c58c6426d043 h1:JlH4bwMt51FxWgMAmoCYmorPvYsaDVGSiyA5JPwusoc=
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241010093641-c58c6426d043/go.mod h1:iedxajZANlvR0u8ODF8TJ7ZAcx6Fg+CMgJT+WsGH5eg=
github.com/kitex-contrib/registry-etcd v0.2.5 h1:16On5GQAmN9tdbFy95QtwGH3aTWnW/BfdvdDmzLIytE=
github.com/kitex-contrib/registry-etcd v0.2.5/go.mod h1:Imgvy+EkqExHuJs4+VFK0g1bFWHaCJ0KuK1sofWYZf4=
github.com/kitex-contrib/tracer-opentracing v0.0.3 h1:8SI0OrHTQ8u6eWpemnuaecMBSEVEvWKCWxg2HnnONQU=
Expand Down Expand Up @@ -367,7 +367,6 @@ github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
github.com/nyaruka/phonenumbers v1.4.0 h1:ddhWiHnHCIX3n6ETDA58Zq5dkxkjlvgrDWM2OHHPCzU=
Expand Down Expand Up @@ -716,8 +715,9 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
Expand Down
151 changes: 0 additions & 151 deletions pkg/eslogrus/eslogrus.go

This file was deleted.

130 changes: 130 additions & 0 deletions pkg/eszap/eszap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eszap

import (
"bytes"
"context"
"encoding/json"
"log"
"os"
"strings"
"time"

elastic "github.com/elastic/go-elasticsearch"
"github.com/elastic/go-elasticsearch/esapi"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// ElasticHook is a zap hook for ElasticSearch
type ElasticHook struct {
client *elastic.Client // es 的客户端
host string // es 的 host
index string // 获取索引的名字
ctx context.Context
cancel func()

enc zapcore.Encoder
lvl zap.AtomicLevel
ws zapcore.WriteSyncer
}

// IndexNameFunc get index name
type IndexNameFunc func() string

func NewElasticHook(client *elastic.Client, host string, index string, level zapcore.Level) *ElasticHook {
hook := defaultHookConfig()

hook.client = client
hook.host = host
hook.index = index
hook.lvl = zap.NewAtomicLevelAt(level)

return hook
}

// 发送到 es 的信息结构
type message struct {
Host string
Timestamp string `json:"@timestamp"`
Message string
Level string
}

func createMessage(entry *zapcore.Entry, hook *ElasticHook) *message {
return &message{
Host: hook.host,
Timestamp: entry.Time.UTC().Format(time.RFC3339),
Message: entry.Message,
Level: strings.ToUpper(entry.Level.String()),
}
}

func (hook *ElasticHook) Fire(entry zapcore.Entry) error {
data, err := json.Marshal(createMessage(&entry, hook))
if err != nil {
return err
}

req := esapi.IndexRequest{
Index: hook.index,
Body: bytes.NewReader(data),
Refresh: "true",
}

res, err := req.Do(hook.ctx, hook.client)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}

var r map[string]interface{}
if err = json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Printf("Error parsing the response body: %s", err)
} else {
// Print the response status and indexed document version.
log.Printf("[%s] %s; version=%d", res.Status(), r["result"], int(r["_version"].(float64)))
}
return err
}

func (hook *ElasticHook) Enc() zapcore.Encoder {
return hook.enc
}

func (hook *ElasticHook) Lvl() zap.AtomicLevel {
return hook.lvl
}

func (hook *ElasticHook) Ws() zapcore.WriteSyncer {
return hook.ws
}

func defaultHookConfig() *ElasticHook {
ctx, cancel := context.WithCancel(context.Background())
enc := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
lvl := zap.NewAtomicLevelAt(zap.DebugLevel)
ws := zapcore.AddSync(os.Stdout)

return &ElasticHook{
ctx: ctx,
cancel: cancel,
enc: enc,
lvl: lvl,
ws: ws,
}
}

0 comments on commit 064c56b

Please sign in to comment.