✨ init project
parent
9f6ce7fada
commit
774f674524
@ -1,23 +1,4 @@
|
||||
# ---> Go
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
.idea
|
||||
vendor
|
||||
logs
|
||||
cache
|
@ -0,0 +1,25 @@
|
||||
package logger
|
||||
|
||||
import "fmt"
|
||||
|
||||
type mode string
|
||||
|
||||
const (
|
||||
Dev mode = "development"
|
||||
Prod mode = "production"
|
||||
)
|
||||
|
||||
// LogConfig 日志配置
|
||||
type LogConfig struct {
|
||||
Mode mode `env:"LOG_MODE" envDefault:"production"` // dev, prod
|
||||
LokiEnable bool `env:"LOG_LOKI_ENABLE"` // 是否启用Loki
|
||||
FileEnable bool `env:"LOG_FILE_ENABLE"` // 是否输出到文件
|
||||
LokiHost string `env:"LOG_LOKI_HOST"` // Loki地址
|
||||
LokiPort int `env:"LOG_LOKI_PORT"` // Loki端口
|
||||
LokiSource string `env:"LOG_LOKI_SOURCE_NAME"` // Loki的source名称
|
||||
LokiJob string `env:"LOG_LOKI_JOB_NAME"` // Loki的job名称
|
||||
}
|
||||
|
||||
func (c LogConfig) getLokiPushURL() string {
|
||||
return fmt.Sprintf("http://%v:%v/loki/api/v1/push", c.LokiHost, c.LokiPort)
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
module git.echol.cn/loser/logger
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.9.2
|
||||
github.com/go-kit/kit v0.12.0
|
||||
github.com/lixh00/loki-client-go v1.0.1
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/prometheus/common v0.34.0
|
||||
go.uber.org/zap v1.21.0
|
||||
gorm.io/driver/mysql v1.3.2
|
||||
gorm.io/gorm v1.23.5
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v0.4.1 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/go-kit/log v0.2.1 // indirect
|
||||
github.com/go-logfmt/logfmt v0.5.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.4 // indirect
|
||||
github.com/jpillora/backoff v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // 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.12.2 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/procfs v0.7.3 // indirect
|
||||
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
|
||||
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3 // indirect
|
||||
google.golang.org/grpc v1.46.2 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
@ -0,0 +1,45 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
gl "gorm.io/gorm/logger"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGormLogger(t *testing.T) {
|
||||
dsn := "saas:saas123@tcp(10.11.0.10:3307)/saas_tenant?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
|
||||
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: DefaultGormLogger()})
|
||||
if err != nil {
|
||||
log.Panicf("mysql connect error: %s", err.Error())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := engine.Table("t_tenant").Count(&count).Error; err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
t.Logf("count: %d", count)
|
||||
}
|
||||
|
||||
func TestGormLoggerWithConfig(t *testing.T) {
|
||||
dsn := "saas:saas123@tcp(10.11.0.10:3307)/saas_tenant?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
|
||||
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: NewGormLoggerWithConfig(gl.Config{
|
||||
SlowThreshold: time.Second, // Slow SQL threshold
|
||||
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
||||
LogLevel: gl.Warn, // Log level
|
||||
Colorful: false, // Disable color
|
||||
})})
|
||||
if err != nil {
|
||||
log.Panicf("mysql connect error: %s", err.Error())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := engine.Table("t_tenant1").Count(&count).Error; err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
t.Logf("count: %d", count)
|
||||
}
|
@ -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...)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
log.Debug("芜湖")
|
||||
}
|
||||
|
||||
func TestLogger1(t *testing.T) {
|
||||
log.Info("我是测试消息")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
func TestLogger2(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
log.Info("我是测试消息")
|
||||
//time.Sleep(5 * time.Second)
|
||||
}
|
Loading…
Reference in New Issue