mirror of
https://gitee.ltd/lxh/logger.git
synced 2025-10-23 14:56:16 +08:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
27dd190fbd | ||
|
3ea919e06c | ||
|
52e9be31c3 | ||
|
b587bc4ceb |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,5 +1,4 @@
|
|||||||
.idea
|
.idea
|
||||||
vendor
|
vendor
|
||||||
logs
|
logs
|
||||||
cache
|
cache
|
||||||
log
|
|
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 {
|
||||||
|
75
log/say.go
Normal file
75
log/say.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import "go.uber.org/zap"
|
||||||
|
|
||||||
|
// Debug uses fmt.Sprint to construct and log a message.
|
||||||
|
func Debug(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Debug(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info uses fmt.Sprint to construct and log a message.
|
||||||
|
func Info(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Info(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn uses fmt.Sprint to construct and log a message.
|
||||||
|
func Warn(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Warn(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error uses fmt.Sprint to construct and log a message.
|
||||||
|
func Error(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Error(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panic uses fmt.Sprint to construct and log a message, then panics.
|
||||||
|
func Panic(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Panic(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
|
||||||
|
func Fatal(args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Fatal(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugf uses fmt.Sprintf to log a templated message.
|
||||||
|
func Debugf(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Debugf(template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infof uses fmt.Sprintf to log a templated message.
|
||||||
|
func Infof(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Infof(template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warnf uses fmt.Sprintf to log a templated message.
|
||||||
|
func Warnf(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Warnf(template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf uses fmt.Sprintf to log a templated message.
|
||||||
|
func Errorf(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Errorf(template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panicf uses fmt.Sprintf to log a templated message, then panics.
|
||||||
|
func Panicf(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Panicf(template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
|
||||||
|
func Fatalf(template string, args ...interface{}) {
|
||||||
|
defer zap.S().Sync()
|
||||||
|
zap.S().Fatalf(template, args...)
|
||||||
|
}
|
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,7 +46,8 @@ func InitLogger(c LogConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 增加 caller 信息
|
// 增加 caller 信息
|
||||||
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller())
|
// AddCallerSkip 输出的文件名和行号是调用封装函数的位置,而不是调用日志函数的位置
|
||||||
|
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddCallerSkip(1))
|
||||||
Say = logger.Sugar()
|
Say = logger.Sugar()
|
||||||
zap.ReplaceGlobals(logger)
|
zap.ReplaceGlobals(logger)
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.uber.org/zap"
|
"gitee.ltd/lxh/logger/log"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -18,6 +18,6 @@ func TestLogger1(t *testing.T) {
|
|||||||
|
|
||||||
func TestLogger2(t *testing.T) {
|
func TestLogger2(t *testing.T) {
|
||||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||||
zap.S().Info("我是测试消息")
|
log.Info("我是测试消息")
|
||||||
time.Sleep(5 * time.Second)
|
//time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user