作者 陈志颖

fix conflict

  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,14 +91,16 @@ spec: @@ -91,14 +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"
94 - name: LOG_TYPE 96 - name: LOG_TYPE
95 - value: file 97 + value: "file"
96 - name: ERROR_BASE_CODE 98 - name: ERROR_BASE_CODE
97 value: "1" 99 value: "1"
98 - name: ERROR_BASE_CODE_MULTIPLE 100 - name: ERROR_BASE_CODE_MULTIPLE
99 value: "1000" 101 value: "1000"
100 - name: ENABLE_KAFKA_LOG 102 - name: ENABLE_KAFKA_LOG
101 - value: "true" 103 + value: "false"
102 - name: HTTP_PORT 104 - name: HTTP_PORT
103 value: "8082" 105 value: "8082"
104 - 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,8 +8,11 @@ import ( @@ -8,8 +8,11 @@ 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 = "console" 11 +var LOG_TYPE = "console"// file, console, es
  12 +var LOG_FRAMEWORK = "beego" // beego logrus
12 var LOG_FILE = "app.log" 13 var LOG_FILE = "app.log"
  14 +var LOG_PREFIX = "[allied-creation-gateway]"
  15 +
13 var HTTP_PORT int = 8083 16 var HTTP_PORT int = 8083
14 17
15 //天联共创基础模块 18 //天联共创基础模块
@@ -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 - ENABLE_KAFKA_LOG = true 22 + if os.Getenv("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 }
@@ -4,12 +4,11 @@ import ( @@ -4,12 +4,11 @@ import (
4 "bytes" 4 "bytes"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
7 - "io"  
8 - "strconv"  
9 -  
10 "github.com/beego/beego/v2/core/logs" 7 "github.com/beego/beego/v2/core/logs"
11 - "github.com/linmadan/egglib-go/log" 8 + _ "github.com/beego/beego/v2/core/logs/es"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
  10 + "io"
  11 + "strconv"
13 ) 12 )
14 13
15 type LoggerConfig struct { 14 type LoggerConfig struct {
@@ -26,35 +25,7 @@ type BeegoLog struct { @@ -26,35 +25,7 @@ type BeegoLog struct {
26 beeLogger *logs.BeeLogger 25 beeLogger *logs.BeeLogger
27 } 26 }
28 27
29 -var _ log.Logger = &BeegoLog{}  
30 -  
31 -func NewBeegoLog(conf LoggerConfig) *BeegoLog {  
32 - logger := logs.GetBeeLogger()  
33 - logger.SetLevel(conf.Level)  
34 - if constant.LOG_TYPE == "file" { // 日志存储到文件  
35 - confByte, _ := json.Marshal(conf)  
36 - err := logger.SetLogger(logs.AdapterFile, string(confByte))  
37 - if err != nil {  
38 - fmt.Println(err.Error())  
39 - return nil  
40 - }  
41 - } else { // 日志输出到控制台  
42 - err := logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)  
43 - if err != nil {  
44 - fmt.Println(err.Error())  
45 - return nil  
46 - }  
47 - }  
48 - logger.SetPrefix(constant.SERVICE_NAME)  
49 - logger.EnableFuncCallDepth(true)  
50 - logger.SetLogFuncCallDepth(5)  
51 - return &BeegoLog{  
52 - beeLogger: logger,  
53 - }  
54 -}  
55 -  
56 func (logger *BeegoLog) AddHook(write io.Writer) { 28 func (logger *BeegoLog) AddHook(write io.Writer) {
57 -  
58 } 29 }
59 30
60 func (logger *BeegoLog) SetServiceName(serviceName string) { 31 func (logger *BeegoLog) SetServiceName(serviceName string) {
@@ -118,3 +89,35 @@ func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) { @@ -118,3 +89,35 @@ func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) {
118 func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) { 89 func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) {
119 logs.Error(msg, appends) 90 logs.Error(msg, appends)
120 } 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 +}
1 package log 1 package log
2 2
3 import ( 3 import (
  4 + "github.com/linmadan/egglib-go/log/logrus"
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
5 6
6 "github.com/linmadan/egglib-go/log" 7 "github.com/linmadan/egglib-go/log"
@@ -9,13 +10,15 @@ import ( @@ -9,13 +10,15 @@ import (
9 var Logger log.Logger 10 var Logger log.Logger
10 11
11 func init() { 12 func init() {
12 - // Logger = logrus.NewLogrusLogger()  
13 - // Logger.SetServiceName(constant.SERVICE_NAME)  
14 - // Logger.SetLevel(constant.LOG_LEVEL)  
15 - // //  
16 - Logger = NewBeegoLog(LoggerConfig{  
17 - Filename: constant.LOG_FILE,  
18 - Level: 7,  
19 - MaxSize: 1024 * 1024 * 2,  
20 - }) 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" {
  20 + Logger = logrus.NewLogrusLogger()
  21 + Logger.SetServiceName(constant.SERVICE_NAME)
  22 + Logger.SetLevel(constant.LOG_LEVEL)
  23 + }
21 } 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 +}