正在显示
6 个修改的文件
包含
255 行增加
和
250 行删除
@@ -24,7 +24,6 @@ require ( | @@ -24,7 +24,6 @@ require ( | ||
24 | github.com/onsi/gomega v1.11.0 | 24 | github.com/onsi/gomega v1.11.0 |
25 | github.com/sergi/go-diff v1.2.0 // indirect | 25 | github.com/sergi/go-diff v1.2.0 // indirect |
26 | github.com/shopspring/decimal v1.2.0 | 26 | github.com/shopspring/decimal v1.2.0 |
27 | - github.com/sirupsen/logrus v1.8.0 | ||
28 | github.com/smartystreets/goconvey v1.7.2 // indirect | 27 | github.com/smartystreets/goconvey v1.7.2 // indirect |
29 | github.com/stretchr/testify v1.7.0 | 28 | github.com/stretchr/testify v1.7.0 |
30 | github.com/tidwall/gjson v1.13.0 | 29 | github.com/tidwall/gjson v1.13.0 |
@@ -37,4 +36,4 @@ require ( | @@ -37,4 +36,4 @@ require ( | ||
37 | golang.org/x/text v0.3.6 | 36 | golang.org/x/text v0.3.6 |
38 | ) | 37 | ) |
39 | 38 | ||
40 | -replace github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 => github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987 | 39 | +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= | @@ -380,8 +380,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= | ||
380 | github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= | 380 | github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= |
381 | github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= | 381 | github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= |
382 | github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= | 382 | github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= |
383 | -github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987 h1:0e2hOSL+//5AL7e1r3xCGEugsOPsw2POAm82VZvWLe4= | ||
384 | -github.com/tiptok/egglib-go v0.0.0-20220120032512-24dfab2b4987/go.mod h1:xl9i83IKNUkwlobRF6XLKn1RRbZsT+7yhCicpTGWTKc= | 383 | +github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1 h1:mUikg1B4YHx79eAqj17OoyfLvOfYCiXmhtHd6ywu6Rc= |
384 | +github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1/go.mod h1:jfeZYXCWwE7u3bUeyJlfhlzC25b9699lUMURP7pdE3I= | ||
385 | github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= | 385 | github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= |
386 | github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= | 386 | github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= |
387 | github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= | 387 | github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= |
@@ -3,7 +3,6 @@ package main | @@ -3,7 +3,6 @@ package main | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "github.com/beego/beego/v2/server/web" | 5 | "github.com/beego/beego/v2/server/web" |
6 | - "github.com/linmadan/egglib-go/log/logrus" | ||
7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" | 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" |
8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" | 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" |
9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" | 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" |
@@ -25,16 +24,8 @@ func main() { | @@ -25,16 +24,8 @@ func main() { | ||
25 | log.Logger.Error(fmt.Sprintf("%v", r)) | 24 | log.Logger.Error(fmt.Sprintf("%v", r)) |
26 | } | 25 | } |
27 | }() | 26 | }() |
28 | - if constant.ENABLE_KAFKA_LOG { | ||
29 | - w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false) | ||
30 | - log.Logger.AddHook(w) | ||
31 | - } | ||
32 | - bw := log.NewBeegoLogWriter(log.LoggerConfig{ | ||
33 | - Filename: constant.LOG_FILE, | ||
34 | - Level: 7, | ||
35 | - MaxSize: 1024 * 1024 * 2, | ||
36 | - }) | ||
37 | - log.Logger.AddHook(bw) | 27 | + |
28 | + log.InitLogHook(constant.ENABLE_KAFKA_LOG, true) | ||
38 | redis.InitRedis() | 29 | redis.InitRedis() |
39 | log.Logger.Info("server start ....") | 30 | log.Logger.Info("server start ....") |
40 | log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG)) | 31 | log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG)) |
1 | package log | 1 | package log |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "github.com/linmadan/egglib-go/log" | ||
5 | + "github.com/linmadan/egglib-go/log/logrus" | ||
4 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" | 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" |
7 | + "io" | ||
5 | ) | 8 | ) |
6 | 9 | ||
7 | -//var Logger log.Logger | ||
8 | -// | ||
9 | -//func init() { | ||
10 | -// Logger = logrus.NewLogrusLogger() | ||
11 | -// Logger.SetServiceName(constant.SERVICE_NAME) | ||
12 | -// Logger.SetLevel(constant.LOG_LEVEL) | ||
13 | -//} | ||
14 | - | ||
15 | -var Logger *LogrusLogger | 10 | +var Logger log.Logger |
16 | 11 | ||
17 | func init() { | 12 | func init() { |
18 | - Logger = NewLogrusLogger() | 13 | + Logger = logrus.NewLogrusLogger() |
19 | Logger.SetServiceName(constant.SERVICE_NAME) | 14 | Logger.SetServiceName(constant.SERVICE_NAME) |
20 | Logger.SetLevel(constant.LOG_LEVEL) | 15 | Logger.SetLevel(constant.LOG_LEVEL) |
21 | } | 16 | } |
17 | + | ||
18 | +func InitLogHook(enableKafka bool, enableBeeLog bool) { | ||
19 | + if enableKafka { | ||
20 | + if w, err := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false); err == nil { | ||
21 | + Logger.AddHook(w) | ||
22 | + } else { | ||
23 | + Logger.Error(err.Error()) | ||
24 | + } | ||
25 | + } | ||
26 | + | ||
27 | + if enableBeeLog { | ||
28 | + if w, err := logrus.NewWrapHook(true, func() (io.Writer, error) { | ||
29 | + return logrus.NewBeegoLogWriter("", `{"filename":"app.log","maxsize":2048000}`) | ||
30 | + }); err == nil { | ||
31 | + Logger.AddHook(w) | ||
32 | + } else { | ||
33 | + Logger.Error(err.Error()) | ||
34 | + } | ||
35 | + } | ||
36 | +} | ||
37 | + | ||
38 | +//var Logger *LogrusLogger | ||
39 | +// | ||
40 | +//func init() { | ||
41 | +// Logger = NewLogrusLogger() | ||
42 | +// Logger.SetServiceName(constant.SERVICE_NAME) | ||
43 | +// Logger.SetLevel(constant.LOG_LEVEL) | ||
44 | +//} |
1 | package log | 1 | package log |
2 | 2 | ||
3 | -import ( | ||
4 | - "github.com/linmadan/egglib-go/log" | ||
5 | - "github.com/sirupsen/logrus" | ||
6 | - "github.com/sirupsen/logrus/hooks/writer" | ||
7 | - "io" | ||
8 | - "os" | ||
9 | -) | ||
10 | - | ||
11 | -func init() { | ||
12 | - logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
13 | - logrus.SetOutput(os.Stdout) | ||
14 | - logrus.SetLevel(logrus.WarnLevel) | ||
15 | -} | ||
16 | - | ||
17 | -type LogrusLogger struct { | ||
18 | - serviceName string | ||
19 | - logrus *logrus.Logger | ||
20 | -} | ||
21 | - | ||
22 | -func (logger *LogrusLogger) SetServiceName(serviceName string) { | ||
23 | - logger.serviceName = serviceName | ||
24 | -} | ||
25 | - | ||
26 | -func (logger *LogrusLogger) SetLevel(level string) { | ||
27 | - switch level { | ||
28 | - case "trace": | ||
29 | - logger.logrus.Level = logrus.TraceLevel | ||
30 | - case "debug": | ||
31 | - logger.logrus.Level = logrus.DebugLevel | ||
32 | - case "info": | ||
33 | - logger.logrus.Level = logrus.InfoLevel | ||
34 | - case "warn": | ||
35 | - logger.logrus.Level = logrus.WarnLevel | ||
36 | - case "error": | ||
37 | - logger.logrus.Level = logrus.ErrorLevel | ||
38 | - case "fatal": | ||
39 | - logger.logrus.Level = logrus.FatalLevel | ||
40 | - case "panic": | ||
41 | - logger.logrus.Level = logrus.PanicLevel | ||
42 | - default: | ||
43 | - logger.logrus.Level = logrus.DebugLevel | ||
44 | - } | ||
45 | -} | ||
46 | - | ||
47 | -func (logger *LogrusLogger) AddHook(w io.Writer) { | ||
48 | - level := logger.logrus.Level | ||
49 | - var levels []logrus.Level | ||
50 | - // 默认已经添加了一个当前log level的hook,所以此处 level+1 | ||
51 | - for i := 0; i <= (int(level)); i++ { | ||
52 | - levels = append(levels, logrus.Level(i)) | ||
53 | - } | ||
54 | - logger.logrus.AddHook(&writer.Hook{ | ||
55 | - Writer: w, | ||
56 | - LogLevels: levels, | ||
57 | - }) | ||
58 | -} | ||
59 | - | ||
60 | -func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) { | ||
61 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
62 | - for _, append := range appends { | ||
63 | - contextLogger = contextLogger.WithFields(append) | ||
64 | - } | ||
65 | - contextLogger.Trace(msg) | ||
66 | -} | ||
67 | - | ||
68 | -func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) { | ||
69 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
70 | - for _, append := range appends { | ||
71 | - contextLogger = contextLogger.WithFields(append) | ||
72 | - } | ||
73 | - contextLogger.Debug(msg) | ||
74 | -} | ||
75 | - | ||
76 | -func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) { | ||
77 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
78 | - for _, append := range appends { | ||
79 | - contextLogger = contextLogger.WithFields(append) | ||
80 | - } | ||
81 | - contextLogger.Info(msg) | ||
82 | -} | ||
83 | - | ||
84 | -func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) { | ||
85 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
86 | - for _, append := range appends { | ||
87 | - contextLogger = contextLogger.WithFields(append) | ||
88 | - } | ||
89 | - contextLogger.Warn(msg) | ||
90 | -} | ||
91 | - | ||
92 | -func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) { | ||
93 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
94 | - for _, append := range appends { | ||
95 | - contextLogger = contextLogger.WithFields(append) | ||
96 | - } | ||
97 | - contextLogger.Error(msg) | ||
98 | -} | ||
99 | - | ||
100 | -func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) { | ||
101 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
102 | - for _, append := range appends { | ||
103 | - contextLogger = contextLogger.WithFields(append) | ||
104 | - } | ||
105 | - contextLogger.Fatal(msg) | ||
106 | -} | ||
107 | - | ||
108 | -func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) { | ||
109 | - contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
110 | - for _, append := range appends { | ||
111 | - contextLogger = contextLogger.WithFields(append) | ||
112 | - } | ||
113 | - contextLogger.Panic(msg) | ||
114 | -} | ||
115 | - | ||
116 | -func NewLogrusLogger() *LogrusLogger { | ||
117 | - logger := logrus.New() | ||
118 | - logger.Formatter = &logrus.JSONFormatter{} | ||
119 | - logger.Out = os.Stdout | ||
120 | - return &LogrusLogger{ | ||
121 | - logrus: logger, | ||
122 | - } | ||
123 | -} | ||
124 | - | ||
125 | -var _ log.Logger = (*LogrusLogger)(nil) | 3 | +//func init() { |
4 | +// logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
5 | +// logrus.SetOutput(os.Stdout) | ||
6 | +// logrus.SetLevel(logrus.WarnLevel) | ||
7 | +//} | ||
8 | +// | ||
9 | +//type LogrusLogger struct { | ||
10 | +// serviceName string | ||
11 | +// logrus *logrus.Logger | ||
12 | +//} | ||
13 | +// | ||
14 | +//func (logger *LogrusLogger) SetServiceName(serviceName string) { | ||
15 | +// logger.serviceName = serviceName | ||
16 | +//} | ||
17 | +// | ||
18 | +//func (logger *LogrusLogger) SetLevel(level string) { | ||
19 | +// switch level { | ||
20 | +// case "trace": | ||
21 | +// logger.logrus.Level = logrus.TraceLevel | ||
22 | +// case "debug": | ||
23 | +// logger.logrus.Level = logrus.DebugLevel | ||
24 | +// case "info": | ||
25 | +// logger.logrus.Level = logrus.InfoLevel | ||
26 | +// case "warn": | ||
27 | +// logger.logrus.Level = logrus.WarnLevel | ||
28 | +// case "error": | ||
29 | +// logger.logrus.Level = logrus.ErrorLevel | ||
30 | +// case "fatal": | ||
31 | +// logger.logrus.Level = logrus.FatalLevel | ||
32 | +// case "panic": | ||
33 | +// logger.logrus.Level = logrus.PanicLevel | ||
34 | +// default: | ||
35 | +// logger.logrus.Level = logrus.DebugLevel | ||
36 | +// } | ||
37 | +//} | ||
38 | +// | ||
39 | +//func (logger *LogrusLogger) AddHook(w io.Writer) { | ||
40 | +// level := logger.logrus.Level | ||
41 | +// var levels []logrus.Level | ||
42 | +// // 默认已经添加了一个当前log level的hook,所以此处 level+1 | ||
43 | +// for i := 0; i <= (int(level)); i++ { | ||
44 | +// levels = append(levels, logrus.Level(i)) | ||
45 | +// } | ||
46 | +// logger.logrus.AddHook(&writer.Hook{ | ||
47 | +// Writer: w, | ||
48 | +// LogLevels: levels, | ||
49 | +// }) | ||
50 | +//} | ||
51 | +// | ||
52 | +//func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) { | ||
53 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
54 | +// for _, append := range appends { | ||
55 | +// contextLogger = contextLogger.WithFields(append) | ||
56 | +// } | ||
57 | +// contextLogger.Trace(msg) | ||
58 | +//} | ||
59 | +// | ||
60 | +//func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) { | ||
61 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
62 | +// for _, append := range appends { | ||
63 | +// contextLogger = contextLogger.WithFields(append) | ||
64 | +// } | ||
65 | +// contextLogger.Debug(msg) | ||
66 | +//} | ||
67 | +// | ||
68 | +//func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) { | ||
69 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
70 | +// for _, append := range appends { | ||
71 | +// contextLogger = contextLogger.WithFields(append) | ||
72 | +// } | ||
73 | +// contextLogger.Info(msg) | ||
74 | +//} | ||
75 | +// | ||
76 | +//func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) { | ||
77 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
78 | +// for _, append := range appends { | ||
79 | +// contextLogger = contextLogger.WithFields(append) | ||
80 | +// } | ||
81 | +// contextLogger.Warn(msg) | ||
82 | +//} | ||
83 | +// | ||
84 | +//func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) { | ||
85 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
86 | +// for _, append := range appends { | ||
87 | +// contextLogger = contextLogger.WithFields(append) | ||
88 | +// } | ||
89 | +// contextLogger.Error(msg) | ||
90 | +//} | ||
91 | +// | ||
92 | +//func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) { | ||
93 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
94 | +// for _, append := range appends { | ||
95 | +// contextLogger = contextLogger.WithFields(append) | ||
96 | +// } | ||
97 | +// contextLogger.Fatal(msg) | ||
98 | +//} | ||
99 | +// | ||
100 | +//func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) { | ||
101 | +// contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName}) | ||
102 | +// for _, append := range appends { | ||
103 | +// contextLogger = contextLogger.WithFields(append) | ||
104 | +// } | ||
105 | +// contextLogger.Panic(msg) | ||
106 | +//} | ||
107 | +// | ||
108 | +//func NewLogrusLogger() *LogrusLogger { | ||
109 | +// logger := logrus.New() | ||
110 | +// logger.Formatter = &logrus.JSONFormatter{} | ||
111 | +// logger.Out = os.Stdout | ||
112 | +// return &LogrusLogger{ | ||
113 | +// logrus: logger, | ||
114 | +// } | ||
115 | +//} | ||
116 | +// | ||
117 | +//var _ log.Logger = (*LogrusLogger)(nil) |
1 | package log | 1 | package log |
2 | 2 | ||
3 | -import ( | ||
4 | - "encoding/json" | ||
5 | - "errors" | ||
6 | - "fmt" | ||
7 | - "github.com/beego/beego/v2/core/logs" | ||
8 | - "io" | ||
9 | - "sync/atomic" | ||
10 | -) | ||
11 | - | ||
12 | -var errOutOfMaxSize = errors.New("msg size is out of limit ") | ||
13 | - | ||
14 | -type BaseHook struct { | ||
15 | - syncWrite bool | ||
16 | - msgChan chan []byte | ||
17 | - maxSize int32 | ||
18 | - currentSize int32 | ||
19 | - closeChan chan struct{} | ||
20 | - w io.Writer | ||
21 | -} | ||
22 | - | ||
23 | -func (b *BaseHook) Write(p []byte) (n int, err error) { | ||
24 | - if b.syncWrite { | ||
25 | - return b.w.Write(p) | ||
26 | - } | ||
27 | - if b.currentSize >= b.maxSize { | ||
28 | - fmt.Println(errOutOfMaxSize.Error(), b.currentSize) | ||
29 | - return 0, errOutOfMaxSize | ||
30 | - } | ||
31 | - b.msgChan <- p | ||
32 | - atomic.AddInt32(&b.currentSize, 1) | ||
33 | - | ||
34 | - return len(p), nil | ||
35 | -} | ||
36 | - | ||
37 | -// NewBaseHook | ||
38 | -// syncWriteFlag 同步写标识 true:同步写 false:异步写 | ||
39 | -func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) { | ||
40 | - writer := &BaseHook{ | ||
41 | - syncWrite: syncWriteFlag, | ||
42 | - maxSize: 10000, | ||
43 | - msgChan: make(chan []byte, 10000), | ||
44 | - closeChan: make(chan struct{}), | ||
45 | - w: internalIo, | ||
46 | - } | ||
47 | - go writer.ConsumeMsg() | ||
48 | - return writer, nil | ||
49 | -} | ||
50 | - | ||
51 | -func (b *BaseHook) ConsumeMsg() { | ||
52 | - for { | ||
53 | - select { | ||
54 | - case <-b.closeChan: | ||
55 | - return | ||
56 | - case m, ok := <-b.msgChan: | ||
57 | - if ok { | ||
58 | - atomic.AddInt32(&b.currentSize, -1) | ||
59 | - if _, err := b.w.Write(m); err != nil { | ||
60 | - fmt.Println(err) | ||
61 | - } | ||
62 | - } | ||
63 | - } | ||
64 | - } | ||
65 | -} | ||
66 | - | ||
67 | -func (b *BaseHook) Close() { | ||
68 | - close(b.msgChan) | ||
69 | - b.closeChan <- struct{}{} | ||
70 | - //b.wc.Close() | ||
71 | -} | ||
72 | - | ||
73 | -type LoggerConfig struct { | ||
74 | - Level int `json:"level,omitempty"` | ||
75 | - Filename string `json:"filename,omitempty"` | ||
76 | - MaxSize int `json:"maxsize,omitempty"` | ||
77 | - //MaxBackups int `json:"max_backups,omitempty"` | ||
78 | - //MaxAge int `json:"max_age,omitempty"` | ||
79 | - //Compress bool `json:"compress,omitempty"` | ||
80 | - //文件最多保存多少天,默认保存 7 天 | ||
81 | - //MaxDays int `json:"maxdays"` | ||
82 | -} | ||
83 | - | ||
84 | -type internalLog struct { | ||
85 | - *logs.BeeLogger | ||
86 | -} | ||
87 | - | ||
88 | -func NewBeegoLogWriter(conf LoggerConfig) io.Writer { | ||
89 | - logger := logs.GetBeeLogger() | ||
90 | - logger.SetLevel(conf.Level) | ||
91 | - logger.EnableFuncCallDepth(true) | ||
92 | - logger.SetLogFuncCallDepth(2) | ||
93 | - confByte, _ := json.Marshal(conf) | ||
94 | - err := logger.SetLogger(logs.AdapterFile, string(confByte)) | ||
95 | - if err != nil { | ||
96 | - fmt.Println(err.Error()) | ||
97 | - } | ||
98 | - return &internalLog{logger} | ||
99 | -} | ||
100 | - | ||
101 | -func (l *internalLog) Write(p []byte) (n int, err error) { | ||
102 | - l.Debug(string(p)) | ||
103 | - return len(p), nil | ||
104 | -} | 3 | +//import ( |
4 | +// "encoding/json" | ||
5 | +// "errors" | ||
6 | +// "fmt" | ||
7 | +// "github.com/beego/beego/v2/core/logs" | ||
8 | +// "io" | ||
9 | +// "sync/atomic" | ||
10 | +//) | ||
11 | +// | ||
12 | +//var errOutOfMaxSize = errors.New("msg size is out of limit ") | ||
13 | +// | ||
14 | +//type BaseHook struct { | ||
15 | +// syncWrite bool | ||
16 | +// msgChan chan []byte | ||
17 | +// maxSize int32 | ||
18 | +// currentSize int32 | ||
19 | +// closeChan chan struct{} | ||
20 | +// w io.Writer | ||
21 | +//} | ||
22 | +// | ||
23 | +//func (b *BaseHook) Write(p []byte) (n int, err error) { | ||
24 | +// if b.syncWrite { | ||
25 | +// return b.w.Write(p) | ||
26 | +// } | ||
27 | +// if b.currentSize >= b.maxSize { | ||
28 | +// fmt.Println(errOutOfMaxSize.Error(), b.currentSize) | ||
29 | +// return 0, errOutOfMaxSize | ||
30 | +// } | ||
31 | +// b.msgChan <- p | ||
32 | +// atomic.AddInt32(&b.currentSize, 1) | ||
33 | +// | ||
34 | +// return len(p), nil | ||
35 | +//} | ||
36 | +// | ||
37 | +//// NewBaseHook | ||
38 | +//// syncWriteFlag 同步写标识 true:同步写 false:异步写 | ||
39 | +//func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) { | ||
40 | +// writer := &BaseHook{ | ||
41 | +// syncWrite: syncWriteFlag, | ||
42 | +// maxSize: 10000, | ||
43 | +// msgChan: make(chan []byte, 10000), | ||
44 | +// closeChan: make(chan struct{}), | ||
45 | +// w: internalIo, | ||
46 | +// } | ||
47 | +// go writer.ConsumeMsg() | ||
48 | +// return writer, nil | ||
49 | +//} | ||
50 | +// | ||
51 | +//func (b *BaseHook) ConsumeMsg() { | ||
52 | +// for { | ||
53 | +// select { | ||
54 | +// case <-b.closeChan: | ||
55 | +// return | ||
56 | +// case m, ok := <-b.msgChan: | ||
57 | +// if ok { | ||
58 | +// atomic.AddInt32(&b.currentSize, -1) | ||
59 | +// if _, err := b.w.Write(m); err != nil { | ||
60 | +// fmt.Println(err) | ||
61 | +// } | ||
62 | +// } | ||
63 | +// } | ||
64 | +// } | ||
65 | +//} | ||
66 | +// | ||
67 | +//func (b *BaseHook) Close() { | ||
68 | +// close(b.msgChan) | ||
69 | +// b.closeChan <- struct{}{} | ||
70 | +// //b.wc.Close() | ||
71 | +//} | ||
72 | +// | ||
73 | +//type LoggerConfig struct { | ||
74 | +// Level int `json:"level,omitempty"` | ||
75 | +// Filename string `json:"filename,omitempty"` | ||
76 | +// MaxSize int `json:"maxsize,omitempty"` | ||
77 | +// //MaxBackups int `json:"max_backups,omitempty"` | ||
78 | +// //MaxAge int `json:"max_age,omitempty"` | ||
79 | +// //Compress bool `json:"compress,omitempty"` | ||
80 | +// //文件最多保存多少天,默认保存 7 天 | ||
81 | +// //MaxDays int `json:"maxdays"` | ||
82 | +//} | ||
83 | +// | ||
84 | +//type internalLog struct { | ||
85 | +// *logs.BeeLogger | ||
86 | +//} | ||
87 | +// | ||
88 | +//func NewBeegoLogWriter(conf LoggerConfig) io.Writer { | ||
89 | +// logger := logs.GetBeeLogger() | ||
90 | +// logger.SetLevel(conf.Level) | ||
91 | +// logger.EnableFuncCallDepth(true) | ||
92 | +// logger.SetLogFuncCallDepth(2) | ||
93 | +// confByte, _ := json.Marshal(conf) | ||
94 | +// err := logger.SetLogger(logs.AdapterFile, string(confByte)) | ||
95 | +// if err != nil { | ||
96 | +// fmt.Println(err.Error()) | ||
97 | +// } | ||
98 | +// return &internalLog{logger} | ||
99 | +//} | ||
100 | +// | ||
101 | +//func (l *internalLog) Write(p []byte) (n int, err error) { | ||
102 | +// l.Debug(string(p)) | ||
103 | +// return len(p), nil | ||
104 | +//} |
-
请 注册 或 登录 后发表评论