🎨 配置优化
This commit is contained in:
parent
52e9be31c3
commit
3ea919e06c
22
config.go
22
config.go
@ -2,22 +2,22 @@ package logger
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type mode int
|
type mode string
|
||||||
|
|
||||||
var (
|
const (
|
||||||
Dev mode = 0
|
Dev mode = "development"
|
||||||
Prod mode = 1
|
Prod mode = "production"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogConfig 日志配置
|
// LogConfig 日志配置
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
Mode mode `env:"LOG_MODE"` // dev, prod
|
Mode mode `env:"LOG_MODE" envDefault:"production"` // dev, prod
|
||||||
LokiEnable bool `env:"LOG_LOKI_ENABLE"` // 是否启用Loki
|
LokiEnable bool `env:"LOG_LOKI_ENABLE"` // 是否启用Loki
|
||||||
FileEnable bool `env:"LOG_FILE_ENABLE"` // 是否输出到文件
|
FileEnable bool `env:"LOG_FILE_ENABLE"` // 是否输出到文件
|
||||||
LokiHost string `env:"LOG_LOKI_HOST"` // Loki地址
|
LokiHost string `env:"LOG_LOKI_HOST"` // Loki地址
|
||||||
LokiPort int `env:"LOG_LOKI_PORT"` // Loki端口
|
LokiPort int `env:"LOG_LOKI_PORT"` // Loki端口
|
||||||
LokiSource string `env:"LOG_LOKI_SOURCE_NAME"` // Loki的source名称
|
LokiSource string `env:"LOG_LOKI_SOURCE_NAME"` // Loki的source名称
|
||||||
LokiJob string `env:"LOG_LOKI_JOB_NAME"` // Loki的job名称
|
LokiJob string `env:"LOG_LOKI_JOB_NAME"` // Loki的job名称
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c LogConfig) getLokiPushURL() string {
|
func (c LogConfig) getLokiPushURL() string {
|
||||||
|
@ -19,6 +19,10 @@ func init() {
|
|||||||
fmt.Println("日志配置解析错误: " + err.Error())
|
fmt.Println("日志配置解析错误: " + err.Error())
|
||||||
c = LogConfig{Mode: Dev, LokiEnable: false, FileEnable: false}
|
c = LogConfig{Mode: Dev, LokiEnable: false, FileEnable: false}
|
||||||
}
|
}
|
||||||
|
// 如果值错了,直接默认为Prod
|
||||||
|
if c.Mode != Dev && c.Mode != Prod {
|
||||||
|
c.Mode = Prod
|
||||||
|
}
|
||||||
InitLogger(c)
|
InitLogger(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user