🎉 创建项目
This commit is contained in:
commit
44f3ff15a1
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.idea
|
||||
vendor
|
||||
logs
|
||||
cache
|
||||
log
|
24
config.go
Normal file
24
config.go
Normal file
@ -0,0 +1,24 @@
|
||||
package logger
|
||||
|
||||
import "fmt"
|
||||
|
||||
type mode int
|
||||
|
||||
var (
|
||||
Dev mode = 0
|
||||
Prod mode = 1
|
||||
)
|
||||
|
||||
// LogConfig 日志配置
|
||||
type LogConfig struct {
|
||||
Mode mode // dev, prod
|
||||
LokiEnable bool
|
||||
FileEnable bool
|
||||
LokiHost string
|
||||
LokiPort int
|
||||
LokiName string // Loki的job和source名称
|
||||
}
|
||||
|
||||
func (c LogConfig) getLokiPushURL() string {
|
||||
return fmt.Sprintf("http://%v:%v/loki/api/v1/push", c.LokiHost, c.LokiPort)
|
||||
}
|
37
console.go
Normal file
37
console.go
Normal file
@ -0,0 +1,37 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 初始化打印到控制台的ZapCore
|
||||
func initConsoleCore() zapcore.Core {
|
||||
// 配置 sugaredLogger
|
||||
writer := zapcore.AddSync(os.Stdout)
|
||||
|
||||
// 自定义时间输出格式
|
||||
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(fmt.Sprintf("[%v]", t.Format("2006-01-02 15:04:05.000")))
|
||||
}
|
||||
|
||||
// 格式相关的配置
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
// 修改时间戳的格式
|
||||
encoderConfig.EncodeTime = customTimeEncoder
|
||||
// 日志级别使用大写带颜色显示
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||
|
||||
// 设置日志等级,如果是release模式,控制台只打印Error级别以上的日志
|
||||
logLevel := zapcore.DebugLevel
|
||||
if config.Mode == Prod {
|
||||
logLevel = zapcore.ErrorLevel
|
||||
}
|
||||
// 设置日志级别
|
||||
core := zapcore.NewCore(encoder, writer, logLevel)
|
||||
return core
|
||||
}
|
44
file.go
Normal file
44
file.go
Normal file
@ -0,0 +1,44 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/natefinch/lumberjack"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 初始化LokiCore,使日志可以输出到文件
|
||||
func initFileCore() zapcore.Core {
|
||||
lumberJackLogger := &lumberjack.Logger{
|
||||
Filename: "logs/runtime.log", // 日志文件的位置
|
||||
MaxSize: 10, // 最大10M
|
||||
MaxBackups: 5, // 保留旧文件的最大个数
|
||||
MaxAge: 30, // 保留旧文件的最大天数
|
||||
Compress: false, // 是否压缩/归档旧文件
|
||||
}
|
||||
// 配置 sugaredLogger
|
||||
writer := zapcore.AddSync(lumberJackLogger)
|
||||
|
||||
// 自定义时间输出格式
|
||||
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(fmt.Sprintf("[%v]", t.Format("2006-01-02 15:04:05.000")))
|
||||
}
|
||||
|
||||
// 格式相关的配置
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
// 修改时间戳的格式
|
||||
encoderConfig.EncodeTime = customTimeEncoder
|
||||
// 日志级别使用大写显示
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||
|
||||
// 设置日志等级,如果是release模式,控制台只打印Error级别以上的日志
|
||||
logLevel := zapcore.DebugLevel
|
||||
if config.Mode == Prod {
|
||||
logLevel = zapcore.ErrorLevel
|
||||
}
|
||||
// 设置日志级别
|
||||
core := zapcore.NewCore(encoder, writer, logLevel)
|
||||
return core
|
||||
}
|
45
go.mod
Normal file
45
go.mod
Normal file
@ -0,0 +1,45 @@
|
||||
module gitee.ltd/lxh/logger
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/go-kit/kit v0.10.0
|
||||
github.com/lixh00/loki-client-go v1.0.1
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/prometheus/common v0.30.0
|
||||
go.uber.org/zap v1.19.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v0.4.1 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.1 // indirect
|
||||
github.com/go-logfmt/logfmt v0.5.0 // indirect
|
||||
github.com/gogo/protobuf v1.3.1 // indirect
|
||||
github.com/golang/protobuf v1.4.3 // indirect
|
||||
github.com/golang/snappy v0.0.2 // indirect
|
||||
github.com/jpillora/backoff v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.11 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/mitchellh/mapstructure v1.2.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_golang v1.11.0 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/procfs v0.6.0 // indirect
|
||||
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
google.golang.org/appengine v1.6.6 // indirect
|
||||
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
|
||||
google.golang.org/grpc v1.32.0 // indirect
|
||||
google.golang.org/protobuf v1.26.0-rc.1 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
32
logger.go
Normal file
32
logger.go
Normal file
@ -0,0 +1,32 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var config LogConfig
|
||||
var Say *zap.SugaredLogger
|
||||
|
||||
// InitLogger 初始化日志工具
|
||||
func InitLogger(c LogConfig) {
|
||||
config = c
|
||||
var cores []zapcore.Core
|
||||
// 生成输出到控制台的Core
|
||||
consoleCore := initConsoleCore()
|
||||
cores = append(cores, consoleCore)
|
||||
// 生成输出到Loki的Core
|
||||
if config.LokiEnable {
|
||||
lokiCore := initLokiCore()
|
||||
cores = append(cores, lokiCore)
|
||||
}
|
||||
// 输出到文件的Core
|
||||
if config.FileEnable {
|
||||
fileCore := initFileCore()
|
||||
cores = append(cores, fileCore)
|
||||
}
|
||||
|
||||
// 增加 caller 信息
|
||||
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller())
|
||||
Say = logger.Sugar()
|
||||
}
|
8
logger_test.go
Normal file
8
logger_test.go
Normal file
@ -0,0 +1,8 @@
|
||||
package logger
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
Say.Debug("芜湖")
|
||||
}
|
90
loki.go
Normal file
90
loki.go
Normal file
@ -0,0 +1,90 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/lixh00/loki-client-go/loki"
|
||||
"github.com/prometheus/common/model"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Loki连接对象
|
||||
var lokiClient *loki.Client
|
||||
|
||||
// 日志输出
|
||||
type lokiWriter struct{}
|
||||
|
||||
// 初始化LokiCore,使日志可以推送到Loki
|
||||
func initLokiCore() zapcore.Core {
|
||||
initLokiClient()
|
||||
// 日志输出到控制台和Loki
|
||||
writer := zapcore.AddSync(newLokiWriter())
|
||||
|
||||
// 自定义时间输出格式
|
||||
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
|
||||
// 格式相关的配置
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
// 修改时间戳的格式
|
||||
encoderConfig.EncodeTime = customTimeEncoder
|
||||
// 日志级别使用大写
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
// 将日志级别设置为 DEBUG
|
||||
return zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), writer, zapcore.DebugLevel)
|
||||
}
|
||||
|
||||
// 初始化LokiClient
|
||||
func initLokiClient() {
|
||||
// 如果Loki配置错误,返回一个nil
|
||||
if config.LokiHost == "" || config.LokiPort < 1 {
|
||||
panic(errors.New("Loki配置错误"))
|
||||
}
|
||||
// 初始化配置
|
||||
cfg, _ := loki.NewDefaultConfig(config.getLokiPushURL())
|
||||
// 创建连接对象
|
||||
client, err := loki.NewWithLogger(cfg, log.NewNopLogger())
|
||||
if err != nil {
|
||||
panic("Loki初始化失败: " + err.Error())
|
||||
}
|
||||
lokiClient = client
|
||||
}
|
||||
|
||||
// 实现Write接口,使lokiClient可以作为zap的扩展
|
||||
func (c lokiWriter) Write(p []byte) (int, error) {
|
||||
type logInfo struct {
|
||||
Level string `json:"level"` // 日志级别
|
||||
Ts string `json:"ts"` // 格式化后的时间(在zap那边配置的)
|
||||
Caller string `json:"caller"` // 日志输出的文件名和行号
|
||||
Msg string `json:"msg"` // 日志内容
|
||||
}
|
||||
var li logInfo
|
||||
err := json.Unmarshal(p, &li)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
label := model.LabelSet{"job": model.LabelValue(config.LokiName)}
|
||||
label["source"] = model.LabelValue(config.LokiName)
|
||||
label["level"] = model.LabelValue(li.Level)
|
||||
label["caller"] = model.LabelValue(li.Caller)
|
||||
// 异步推送消息到服务器
|
||||
go func() {
|
||||
err = lokiClient.Handle(label, time.Now().Local(), li.Msg)
|
||||
if err != nil {
|
||||
fmt.Printf("日志推送到Loki失败: %v\n", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// NewLokiWriter
|
||||
func newLokiWriter() *lokiWriter {
|
||||
return &lokiWriter{}
|
||||
}
|
Loading…
Reference in New Issue
Block a user