支持初始化的时候从环境变量读取配置信息

This commit is contained in:
李寻欢 2021-12-29 14:33:48 +08:00
parent 3f6003edd7
commit 6253de5599
6 changed files with 31 additions and 12 deletions

View File

@ -11,12 +11,13 @@ var (
// LogConfig 日志配置 // LogConfig 日志配置
type LogConfig struct { type LogConfig struct {
Mode mode // dev, prod Mode mode `env:"LOG_MODE"` // dev, prod
LokiEnable bool LokiEnable bool `env:"LOG_LOKI_ENABLE"` // 是否启用Loki
FileEnable bool FileEnable bool `env:"LOG_FILE_ENABLE"` // 是否输出到文件
LokiHost string LokiHost string `env:"LOG_LOKI_HOST"` // Loki地址
LokiPort int LokiPort int `env:"LOG_LOKI_PORT"` // Loki端口
LokiName string // Loki的job和source名称 LokiSource string `env:"LOG_LOKI_SOURCE_NAME"` // Loki的source名称
LokiJob string `env:"LOG_LOKI_JOB_NAME"` // Loki的job名称
} }
func (c LogConfig) getLokiPushURL() string { func (c LogConfig) getLokiPushURL() string {

1
go.mod
View File

@ -13,6 +13,7 @@ require (
require ( require (
github.com/BurntSushi/toml v0.4.1 // indirect github.com/BurntSushi/toml v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/caarlos0/env/v6 v6.8.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect github.com/go-logfmt/logfmt v0.5.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect github.com/gogo/protobuf v1.3.1 // indirect

2
go.sum
View File

@ -114,6 +114,8 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34= github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
github.com/caarlos0/env/v6 v6.8.0 h1:abF9JinEXaibthiOowf4uSnRBWN66aJOxSpHLH67jeI=
github.com/caarlos0/env/v6 v6.8.0/go.mod h1:FE0jGiAnQqtv2TenJ4KTa8+/T2Ss8kdS5s1VEjasoN0=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=

View File

@ -1,6 +1,8 @@
package logger package logger
import ( import (
"fmt"
"github.com/caarlos0/env/v6"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
@ -11,7 +13,13 @@ var Say *zap.SugaredLogger
// 避免异常在第一次调用时初始化一个只打印到控制台的logger // 避免异常在第一次调用时初始化一个只打印到控制台的logger
func init() { func init() {
if Say == nil { if Say == nil {
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: false}) // 从环境变量读取配置
var c LogConfig
if err := env.Parse(&c); err != nil {
fmt.Println("日志配置解析错误: " + err.Error())
c = LogConfig{Mode: Dev, LokiEnable: false, FileEnable: false}
}
InitLogger(c)
} }
} }

View File

@ -1,8 +1,16 @@
package logger package logger
import "testing" import (
"testing"
"time"
)
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true}) InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
Say.Debug("芜湖") Say.Debug("芜湖")
} }
func TestLogger1(t *testing.T) {
Say.Info("我是测试消息")
time.Sleep(5 * time.Second)
}

View File

@ -69,14 +69,13 @@ func (c lokiWriter) Write(p []byte) (int, error) {
return 0, err return 0, err
} }
label := model.LabelSet{"job": model.LabelValue(config.LokiName)} label := model.LabelSet{"job": model.LabelValue(config.LokiJob)}
label["source"] = model.LabelValue(config.LokiName) label["source"] = model.LabelValue(config.LokiSource)
label["level"] = model.LabelValue(li.Level) label["level"] = model.LabelValue(li.Level)
label["caller"] = model.LabelValue(li.Caller) label["caller"] = model.LabelValue(li.Caller)
// 异步推送消息到服务器 // 异步推送消息到服务器
go func() { go func() {
err = lokiClient.Handle(label, time.Now().Local(), li.Msg) if err = lokiClient.Handle(label, time.Now().Local(), li.Msg); err != nil {
if err != nil {
fmt.Printf("日志推送到Loki失败: %v\n", err.Error()) fmt.Printf("日志推送到Loki失败: %v\n", err.Error())
} }
}() }()