作者 陈志颖

chore:添加日志文件

  1 +2021/09/07 01:40:08.650 [D] [hook.go:130] CREATE TABLE IF NOT EXISTS "login_access" ("login_access_id" bigserial, "account" text, "platform" text, "company_id" bigint, "organization_id" bigint, "access_token" text, "refresh_token" text, "access_expired" bigint, "refresh_expired" bigint, "created_time" timestamptz, "updated_time" timestamptz, PRIMARY KEY ("login_access_id")) null
  2 +2021/09/07 01:40:09.439 [I] [hooks.go:101] generate router from comments
  3 +2021/09/07 01:40:09.440 [I] [???:0] http server Running on http://:8083
  4 +2021/09/07 01:40:12.667 [I] [filter.go:81] http请求 [
  5 + {
  6 + "framework": "beego",
  7 + "method": "GET",
  8 + "url": "/log"
  9 + }
  10 +]
  11 +2021/09/07 01:40:12.667 [D] [filter.go:81] http请求 [
  12 + {
  13 + "framework": "beego",
  14 + "method": "GET",
  15 + "url": "/log"
  16 + }
  17 +]
  18 +2021/09/07 01:40:12.669 [D] [filter.go:81] http响应 [
  19 + {
  20 + "framework": "beego",
  21 + "method": "GET",
  22 + "outputData": null,
  23 + "url": "/log"
  24 + }
  25 +]
  26 +2021/09/07 01:40:12.669 [D] [router.go:664] | ::1| 200 | 2.412278ms| match| GET  /log r:/log
  27 +2021/09/07 01:42:14.324 [D] [hook.go:130] CREATE TABLE IF NOT EXISTS "login_access" ("login_access_id" bigserial, "account" text, "platform" text, "company_id" bigint, "organization_id" bigint, "access_token" text, "refresh_token" text, "access_expired" bigint, "refresh_expired" bigint, "created_time" timestamptz, "updated_time" timestamptz, PRIMARY KEY ("login_access_id")) null
  28 +2021/09/07 01:42:15.705 [I] [hooks.go:101] generate router from comments
  29 +2021/09/07 01:42:15.706 [I] [???:0] http server Running on http://:8083
@@ -91,12 +91,16 @@ spec: @@ -91,12 +91,16 @@ spec:
91 value: "" 91 value: ""
92 - name: LOG_LEVEL 92 - name: LOG_LEVEL
93 value: "debug" 93 value: "debug"
  94 + - name: LOG_FRAMEWORK
  95 + value: "beego"
  96 + - name: LOG_TYPE
  97 + value: "file"
94 - name: ERROR_BASE_CODE 98 - name: ERROR_BASE_CODE
95 value: "1" 99 value: "1"
96 - name: ERROR_BASE_CODE_MULTIPLE 100 - name: ERROR_BASE_CODE_MULTIPLE
97 value: "1000" 101 value: "1000"
98 - name: ENABLE_KAFKA_LOG 102 - name: ENABLE_KAFKA_LOG
99 - value: "true" 103 + value: "false"
100 - name: HTTP_PORT 104 - name: HTTP_PORT
101 value: "8082" 105 value: "8082"
102 - name: ALLIED_CREATION_USER_HOST 106 - name: ALLIED_CREATION_USER_HOST
@@ -48,6 +48,7 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1 @@ -48,6 +48,7 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
48 github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc= 48 github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
49 github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= 49 github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
50 github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= 50 github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
  51 +github.com/elastic/go-elasticsearch/v6 v6.8.5 h1:U2HtkBseC1FNBmDr0TR2tKltL6FxoY+niDAlj5M8TK8=
51 github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox93HoX8utj1kxD9aFUcI= 52 github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox93HoX8utj1kxD9aFUcI=
52 github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk= 53 github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
53 github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= 54 github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
@@ -8,6 +8,10 @@ import ( @@ -8,6 +8,10 @@ import (
8 const SERVICE_NAME = "allied-creation-gateway" 8 const SERVICE_NAME = "allied-creation-gateway"
9 9
10 var LOG_LEVEL = "debug" 10 var LOG_LEVEL = "debug"
  11 +var LOG_TYPE = "terminal" // file, terminal, es
  12 +var LOG_FRAMEWORK = "beego" // beego logrus
  13 +var LOG_FILE = "app.log"
  14 +var LOG_PREFIX = "[allied-creation-gateway]"
11 15
12 var HTTP_PORT int = 8083 16 var HTTP_PORT int = 8083
13 17
@@ -19,6 +19,10 @@ func init() { @@ -19,6 +19,10 @@ func init() {
19 // POSTGRESQL_PORT = os.Getenv("TOPIC_LOG_STASH") 19 // POSTGRESQL_PORT = os.Getenv("TOPIC_LOG_STASH")
20 //} 20 //}
21 if os.Getenv("ENABLE_KAFKA_LOG") != "" { 21 if os.Getenv("ENABLE_KAFKA_LOG") != "" {
  22 + if os.Getenv("ENABLE_KAFKA_LOG") == "true" {
22 ENABLE_KAFKA_LOG = true 23 ENABLE_KAFKA_LOG = true
  24 + } else if os.Getenv("ENABLE_KAFKA_LOG") == "false" {
  25 + ENABLE_KAFKA_LOG = false
  26 + }
23 } 27 }
24 } 28 }
  1 +package log
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "fmt"
  7 + "github.com/beego/beego/v2/core/logs"
  8 + _ "github.com/beego/beego/v2/core/logs/es"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
  10 + "io"
  11 + "strconv"
  12 +)
  13 +
  14 +type LoggerConfig struct {
  15 + Level int `json:"level,omitempty"`
  16 + Filename string `json:"filename,omitempty"`
  17 + MaxSize int `json:"maxsize,omitempty"`
  18 + MaxBackups int `json:"max_backups,omitempty"`
  19 + MaxAge int `json:"max_age,omitempty"`
  20 + Compress bool `json:"compress,omitempty"`
  21 +}
  22 +
  23 +type BeegoLog struct {
  24 + serviceName string
  25 + beeLogger *logs.BeeLogger
  26 +}
  27 +
  28 +func (logger *BeegoLog) AddHook(write io.Writer) {
  29 +}
  30 +
  31 +func (logger *BeegoLog) SetServiceName(serviceName string) {
  32 + logger.serviceName = serviceName
  33 +}
  34 +
  35 +func (logger *BeegoLog) SetLevel(level string) {
  36 + ilv, err := strconv.Atoi(level)
  37 + if err != nil {
  38 + ilv = logs.LevelDebug
  39 + }
  40 + logger.beeLogger.SetLevel(ilv)
  41 +}
  42 +
  43 +func (logger *BeegoLog) Trace(msg string, appends ...map[string]interface{}) {
  44 + logs.Trace(msg, appends)
  45 +}
  46 +
  47 +func (logger *BeegoLog) Debug(msg string, appends ...map[string]interface{}) {
  48 + appendsJson, _ := json.Marshal(appends)
  49 + if appendsJson == nil {
  50 + logs.Debug(msg, make(map[string]interface{}, 0))
  51 + } else {
  52 + var out bytes.Buffer
  53 + err := json.Indent(&out, appendsJson, "", " ")
  54 + if err != nil {
  55 + fmt.Println(msg)
  56 + return
  57 + }
  58 + logs.Debug(msg, out.String())
  59 + }
  60 +}
  61 +
  62 +func (logger *BeegoLog) Info(msg string, appends ...map[string]interface{}) {
  63 + appendsJson, _ := json.Marshal(appends)
  64 + if appendsJson == nil {
  65 + logs.Info(msg, make(map[string]interface{}, 0))
  66 + } else {
  67 + var out bytes.Buffer
  68 + err := json.Indent(&out, appendsJson, "", " ")
  69 + if err != nil {
  70 + fmt.Println(msg)
  71 + return
  72 + }
  73 + logs.Info(msg, out.String())
  74 + }
  75 +}
  76 +
  77 +func (logger *BeegoLog) Warn(msg string, appends ...map[string]interface{}) {
  78 + logs.Warn(msg, appends)
  79 +}
  80 +
  81 +func (logger *BeegoLog) Error(msg string, appends ...map[string]interface{}) {
  82 + logs.Error(msg, appends)
  83 +}
  84 +
  85 +func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) {
  86 + logs.Error(msg, appends)
  87 +}
  88 +
  89 +func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) {
  90 + logs.Error(msg, appends)
  91 +}
  92 +
  93 +func NewBeegoLog(conf LoggerConfig) *BeegoLog {
  94 + logger := logs.GetBeeLogger()
  95 + logger.SetLevel(conf.Level)
  96 + if constant.LOG_TYPE == "file" { // 日志存储到文件
  97 + confByte, _ := json.Marshal(conf)
  98 + err := logger.SetLogger(logs.AdapterFile, string(confByte))
  99 + if err != nil {
  100 + fmt.Println(err.Error())
  101 + return nil
  102 + }
  103 + } else if constant.LOG_TYPE == "es" { // 日志存储到ES
  104 + err := logger.SetLogger(logs.AdapterEs, `{"dsn":"http://192.168.100.222:9200/","level":7}`)
  105 + if err != nil {
  106 + fmt.Println(err.Error())
  107 + return nil
  108 + }
  109 + } else { // 日志输出到控制台
  110 + err := logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
  111 + if err != nil {
  112 + fmt.Println(err.Error())
  113 + return nil
  114 + }
  115 + }
  116 + logger.SetPrefix(constant.LOG_PREFIX)
  117 + logger.EnableFuncCallDepth(true)
  118 + logger.SetLogFuncCallDepth(5)
  119 + logger.SetPrefix(constant.LOG_PREFIX)
  120 + return &BeegoLog{
  121 + beeLogger: logger,
  122 + }
  123 +}
@@ -10,7 +10,15 @@ import ( @@ -10,7 +10,15 @@ import (
10 var Logger log.Logger 10 var Logger log.Logger
11 11
12 func init() { 12 func init() {
  13 + if constant.LOG_FRAMEWORK == "beego" {
  14 + Logger = NewBeegoLog(LoggerConfig{
  15 + Filename: constant.LOG_FILE,
  16 + Level: 7,
  17 + MaxSize: 1024 * 1024 * 2,
  18 + })
  19 + } else if constant.LOG_FRAMEWORK == "logrus" {
13 Logger = logrus.NewLogrusLogger() 20 Logger = logrus.NewLogrusLogger()
14 Logger.SetServiceName(constant.SERVICE_NAME) 21 Logger.SetServiceName(constant.SERVICE_NAME)
15 Logger.SetLevel(constant.LOG_LEVEL) 22 Logger.SetLevel(constant.LOG_LEVEL)
  23 + }
16 } 24 }
  1 +package controllers
  2 +
  3 +import "io/ioutil"
  4 +
  5 +type LoggerController struct {
  6 + BaseController
  7 +}
  8 +
  9 +func (controller LoggerController) Index() {
  10 + bytes, _ := ioutil.ReadFile("./app.log")
  11 + controller.Ctx.WriteString(string(bytes))
  12 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/log", &controllers.LoggerController{}, "Get:Index")
  10 +}