作者 yangfu

Merge branch 'test_lib' into test

... ... @@ -24,7 +24,6 @@ require (
github.com/onsi/gomega v1.11.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0
github.com/sirupsen/logrus v1.8.0
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.13.0
... ... @@ -37,4 +36,4 @@ require (
golang.org/x/text v0.3.6
)
replace github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 => github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987
replace github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 => github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1
... ...
... ... @@ -380,8 +380,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987 h1:0e2hOSL+//5AL7e1r3xCGEugsOPsw2POAm82VZvWLe4=
github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987/go.mod h1:xl9i83IKNUkwlobRF6XLKn1RRbZsT+7yhCicpTGWTKc=
github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1 h1:mUikg1B4YHx79eAqj17OoyfLvOfYCiXmhtHd6ywu6Rc=
github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1/go.mod h1:jfeZYXCWwE7u3bUeyJlfhlzC25b9699lUMURP7pdE3I=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
... ...
... ... @@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/beego/beego/v2/server/web"
"github.com/linmadan/egglib-go/log/logrus"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
... ... @@ -25,16 +24,8 @@ func main() {
log.Logger.Error(fmt.Sprintf("%v", r))
}
}()
if constant.ENABLE_KAFKA_LOG {
w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false)
log.Logger.AddHook(w)
}
bw := log.NewBeegoLogWriter(log.LoggerConfig{
Filename: constant.LOG_FILE,
Level: 7,
MaxSize: 1024 * 1024 * 2,
})
log.Logger.AddHook(bw)
log.InitLogHook(constant.ENABLE_KAFKA_LOG, true)
redis.InitRedis()
log.Logger.Info("server start ....")
log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG))
... ...
package log
import (
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/log/logrus"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"io"
)
//var Logger log.Logger
//
//func init() {
// Logger = logrus.NewLogrusLogger()
// Logger.SetServiceName(constant.SERVICE_NAME)
// Logger.SetLevel(constant.LOG_LEVEL)
//}
var Logger *LogrusLogger
var Logger log.Logger
func init() {
Logger = NewLogrusLogger()
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
}
func InitLogHook(enableKafka bool, enableBeeLog bool) {
if enableKafka {
if w, err := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false); err == nil {
Logger.AddHook(w)
} else {
Logger.Error(err.Error())
}
}
if enableBeeLog {
if w, err := logrus.NewWrapHook(true, func() (io.Writer, error) {
return logrus.NewBeegoLogWriter("", `{"filename":"app.log","maxsize":2048000}`)
}); err == nil {
Logger.AddHook(w)
} else {
Logger.Error(err.Error())
}
}
}
//var Logger *LogrusLogger
//
//func init() {
// Logger = NewLogrusLogger()
// Logger.SetServiceName(constant.SERVICE_NAME)
// Logger.SetLevel(constant.LOG_LEVEL)
//}
... ...
package log
import (
"github.com/linmadan/egglib-go/log"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"io"
"os"
)
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.WarnLevel)
}
type LogrusLogger struct {
serviceName string
logrus *logrus.Logger
}
func (logger *LogrusLogger) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *LogrusLogger) SetLevel(level string) {
switch level {
case "trace":
logger.logrus.Level = logrus.TraceLevel
case "debug":
logger.logrus.Level = logrus.DebugLevel
case "info":
logger.logrus.Level = logrus.InfoLevel
case "warn":
logger.logrus.Level = logrus.WarnLevel
case "error":
logger.logrus.Level = logrus.ErrorLevel
case "fatal":
logger.logrus.Level = logrus.FatalLevel
case "panic":
logger.logrus.Level = logrus.PanicLevel
default:
logger.logrus.Level = logrus.DebugLevel
}
}
func (logger *LogrusLogger) AddHook(w io.Writer) {
level := logger.logrus.Level
var levels []logrus.Level
// 默认已经添加了一个当前log level的hook,所以此处 level+1
for i := 0; i <= (int(level)); i++ {
levels = append(levels, logrus.Level(i))
}
logger.logrus.AddHook(&writer.Hook{
Writer: w,
LogLevels: levels,
})
}
func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Trace(msg)
}
func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Debug(msg)
}
func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Info(msg)
}
func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Warn(msg)
}
func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Error(msg)
}
func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Fatal(msg)
}
func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Panic(msg)
}
func NewLogrusLogger() *LogrusLogger {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
logger.Out = os.Stdout
return &LogrusLogger{
logrus: logger,
}
}
var _ log.Logger = (*LogrusLogger)(nil)
//func init() {
// logrus.SetFormatter(&logrus.JSONFormatter{})
// logrus.SetOutput(os.Stdout)
// logrus.SetLevel(logrus.WarnLevel)
//}
//
//type LogrusLogger struct {
// serviceName string
// logrus *logrus.Logger
//}
//
//func (logger *LogrusLogger) SetServiceName(serviceName string) {
// logger.serviceName = serviceName
//}
//
//func (logger *LogrusLogger) SetLevel(level string) {
// switch level {
// case "trace":
// logger.logrus.Level = logrus.TraceLevel
// case "debug":
// logger.logrus.Level = logrus.DebugLevel
// case "info":
// logger.logrus.Level = logrus.InfoLevel
// case "warn":
// logger.logrus.Level = logrus.WarnLevel
// case "error":
// logger.logrus.Level = logrus.ErrorLevel
// case "fatal":
// logger.logrus.Level = logrus.FatalLevel
// case "panic":
// logger.logrus.Level = logrus.PanicLevel
// default:
// logger.logrus.Level = logrus.DebugLevel
// }
//}
//
//func (logger *LogrusLogger) AddHook(w io.Writer) {
// level := logger.logrus.Level
// var levels []logrus.Level
// // 默认已经添加了一个当前log level的hook,所以此处 level+1
// for i := 0; i <= (int(level)); i++ {
// levels = append(levels, logrus.Level(i))
// }
// logger.logrus.AddHook(&writer.Hook{
// Writer: w,
// LogLevels: levels,
// })
//}
//
//func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Trace(msg)
//}
//
//func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Debug(msg)
//}
//
//func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Info(msg)
//}
//
//func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Warn(msg)
//}
//
//func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Error(msg)
//}
//
//func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Fatal(msg)
//}
//
//func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) {
// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
// for _, append := range appends {
// contextLogger = contextLogger.WithFields(append)
// }
// contextLogger.Panic(msg)
//}
//
//func NewLogrusLogger() *LogrusLogger {
// logger := logrus.New()
// logger.Formatter = &logrus.JSONFormatter{}
// logger.Out = os.Stdout
// return &LogrusLogger{
// logrus: logger,
// }
//}
//
//var _ log.Logger = (*LogrusLogger)(nil)
... ...
package log
import (
"encoding/json"
"errors"
"fmt"
"github.com/beego/beego/v2/core/logs"
"io"
"sync/atomic"
)
var errOutOfMaxSize = errors.New("msg size is out of limit ")
type BaseHook struct {
syncWrite bool
msgChan chan []byte
maxSize int32
currentSize int32
closeChan chan struct{}
w io.Writer
}
func (b *BaseHook) Write(p []byte) (n int, err error) {
if b.syncWrite {
return b.w.Write(p)
}
if b.currentSize >= b.maxSize {
fmt.Println(errOutOfMaxSize.Error(), b.currentSize)
return 0, errOutOfMaxSize
}
b.msgChan <- p
atomic.AddInt32(&b.currentSize, 1)
return len(p), nil
}
// NewBaseHook
// syncWriteFlag 同步写标识 true:同步写 false:异步写
func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) {
writer := &BaseHook{
syncWrite: syncWriteFlag,
maxSize: 10000,
msgChan: make(chan []byte, 10000),
closeChan: make(chan struct{}),
w: internalIo,
}
go writer.ConsumeMsg()
return writer, nil
}
func (b *BaseHook) ConsumeMsg() {
for {
select {
case <-b.closeChan:
return
case m, ok := <-b.msgChan:
if ok {
atomic.AddInt32(&b.currentSize, -1)
if _, err := b.w.Write(m); err != nil {
fmt.Println(err)
}
}
}
}
}
func (b *BaseHook) Close() {
close(b.msgChan)
b.closeChan <- struct{}{}
//b.wc.Close()
}
type LoggerConfig struct {
Level int `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"maxsize,omitempty"`
//MaxBackups int `json:"max_backups,omitempty"`
//MaxAge int `json:"max_age,omitempty"`
//Compress bool `json:"compress,omitempty"`
//文件最多保存多少天,默认保存 7 天
//MaxDays int `json:"maxdays"`
}
type internalLog struct {
*logs.BeeLogger
}
func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
logger := logs.GetBeeLogger()
logger.SetLevel(conf.Level)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(2)
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
}
return &internalLog{logger}
}
func (l *internalLog) Write(p []byte) (n int, err error) {
l.Debug(string(p))
return len(p), nil
}
//import (
// "encoding/json"
// "errors"
// "fmt"
// "github.com/beego/beego/v2/core/logs"
// "io"
// "sync/atomic"
//)
//
//var errOutOfMaxSize = errors.New("msg size is out of limit ")
//
//type BaseHook struct {
// syncWrite bool
// msgChan chan []byte
// maxSize int32
// currentSize int32
// closeChan chan struct{}
// w io.Writer
//}
//
//func (b *BaseHook) Write(p []byte) (n int, err error) {
// if b.syncWrite {
// return b.w.Write(p)
// }
// if b.currentSize >= b.maxSize {
// fmt.Println(errOutOfMaxSize.Error(), b.currentSize)
// return 0, errOutOfMaxSize
// }
// b.msgChan <- p
// atomic.AddInt32(&b.currentSize, 1)
//
// return len(p), nil
//}
//
//// NewBaseHook
//// syncWriteFlag 同步写标识 true:同步写 false:异步写
//func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) {
// writer := &BaseHook{
// syncWrite: syncWriteFlag,
// maxSize: 10000,
// msgChan: make(chan []byte, 10000),
// closeChan: make(chan struct{}),
// w: internalIo,
// }
// go writer.ConsumeMsg()
// return writer, nil
//}
//
//func (b *BaseHook) ConsumeMsg() {
// for {
// select {
// case <-b.closeChan:
// return
// case m, ok := <-b.msgChan:
// if ok {
// atomic.AddInt32(&b.currentSize, -1)
// if _, err := b.w.Write(m); err != nil {
// fmt.Println(err)
// }
// }
// }
// }
//}
//
//func (b *BaseHook) Close() {
// close(b.msgChan)
// b.closeChan <- struct{}{}
// //b.wc.Close()
//}
//
//type LoggerConfig struct {
// Level int `json:"level,omitempty"`
// Filename string `json:"filename,omitempty"`
// MaxSize int `json:"maxsize,omitempty"`
// //MaxBackups int `json:"max_backups,omitempty"`
// //MaxAge int `json:"max_age,omitempty"`
// //Compress bool `json:"compress,omitempty"`
// //文件最多保存多少天,默认保存 7 天
// //MaxDays int `json:"maxdays"`
//}
//
//type internalLog struct {
// *logs.BeeLogger
//}
//
//func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
// logger := logs.GetBeeLogger()
// logger.SetLevel(conf.Level)
// logger.EnableFuncCallDepth(true)
// logger.SetLogFuncCallDepth(2)
// confByte, _ := json.Marshal(conf)
// err := logger.SetLogger(logs.AdapterFile, string(confByte))
// if err != nil {
// fmt.Println(err.Error())
// }
// return &internalLog{logger}
//}
//
//func (l *internalLog) Write(p []byte) (n int, err error) {
// l.Debug(string(p))
// return len(p), nil
//}
... ...