作者 yangfu

add log file

@@ -21,7 +21,7 @@ _testmain.go @@ -21,7 +21,7 @@ _testmain.go
21 21
22 *.exe 22 *.exe
23 *.test 23 *.test
24 -.log 24 +*.log
25 .idea 25 .idea
26 *.tmp 26 *.tmp
27 27
@@ -21,6 +21,13 @@ func main() { @@ -21,6 +21,13 @@ func main() {
21 w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false) 21 w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false)
22 log.Logger.AddHook(w) 22 log.Logger.AddHook(w)
23 } 23 }
  24 + bw := log.NewBeegoLogWriter(log.LoggerConfig{
  25 + Filename: constant.LOG_FILE,
  26 + Level: 7,
  27 + MaxSize: 1024 * 1024 * 2,
  28 + })
  29 + log.Logger.AddHook(bw)
  30 +
24 log.Logger.Info("server start!") 31 log.Logger.Info("server start!")
25 web.Run() 32 web.Run()
26 } 33 }
@@ -12,6 +12,7 @@ var SERVICE_ENV = "dev" @@ -12,6 +12,7 @@ var SERVICE_ENV = "dev"
12 var HTTP_PORT int = 8081 12 var HTTP_PORT int = 8081
13 var CACHE_PREFIX = "allied-creation-user-dev" 13 var CACHE_PREFIX = "allied-creation-user-dev"
14 var LOG_LEVEL = "debug" 14 var LOG_LEVEL = "debug"
  15 +var LOG_FILE = "app.log"
15 16
16 var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384} 17 var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384}
17 18
@@ -3,6 +3,7 @@ package pg @@ -3,6 +3,7 @@ package pg
3 import ( 3 import (
4 "context" 4 "context"
5 "fmt" 5 "fmt"
  6 + "github.com/beego/beego/v2/core/logs"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
7 "log" 8 "log"
8 "reflect" 9 "reflect"
@@ -63,6 +64,7 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent) @@ -63,6 +64,7 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent)
63 } 64 }
64 //log.Logger.Debug(string(sqlStr)) 65 //log.Logger.Debug(string(sqlStr))
65 log.Println(string(sqlStr)) 66 log.Println(string(sqlStr))
  67 + logs.Debug(string(sqlStr))
66 return nil 68 return nil
67 } 69 }
68 70
  1 +package log
  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 +}
  81 +
  82 +type internalLog struct {
  83 + *logs.BeeLogger
  84 +}
  85 +
  86 +func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
  87 + logger := logs.GetBeeLogger()
  88 + logger.SetLevel(conf.Level)
  89 + logger.EnableFuncCallDepth(true)
  90 + logger.SetLogFuncCallDepth(2)
  91 + confByte, _ := json.Marshal(conf)
  92 + err := logger.SetLogger(logs.AdapterFile, string(confByte))
  93 + if err != nil {
  94 + fmt.Println(err.Error())
  95 + }
  96 + return &internalLog{logger}
  97 +}
  98 +
  99 +func (l *internalLog) Write(p []byte) (n int, err error) {
  100 + l.Debug(string(p))
  101 + return len(p), nil
  102 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
  6 +)
  7 +
  8 +func init() {
  9 + web.SetStaticPath("/log", constant.LOG_FILE)
  10 +}