作者 tangxuhui

添加日志文件

@@ -91,6 +91,8 @@ spec: @@ -91,6 +91,8 @@ spec:
91 value: "" 91 value: ""
92 - name: LOG_LEVEL 92 - name: LOG_LEVEL
93 value: "debug" 93 value: "debug"
  94 + - name: LOG_TYPE
  95 + value: file
94 - name: ERROR_BASE_CODE 96 - name: ERROR_BASE_CODE
95 value: "1" 97 value: "1"
96 - name: ERROR_BASE_CODE_MULTIPLE 98 - name: ERROR_BASE_CODE_MULTIPLE
@@ -70,6 +70,7 @@ func (cooperationApplicationService *CooperationApplicationService) ListCooperat @@ -70,6 +70,7 @@ func (cooperationApplicationService *CooperationApplicationService) ListCooperat
70 PageNumber: listCooperationApplicationQuery.PageNumber, 70 PageNumber: listCooperationApplicationQuery.PageNumber,
71 PageSize: listCooperationApplicationQuery.PageSize, 71 PageSize: listCooperationApplicationQuery.PageSize,
72 CompanyId: int(listCooperationApplicationQuery.Operator.CompanyId), 72 CompanyId: int(listCooperationApplicationQuery.Operator.CompanyId),
  73 + OrgIds: listCooperationApplicationQuery.Operator.OrgIds,
73 IsCanceled: 3, 74 IsCanceled: 3,
74 }) 75 })
75 if err != nil { 76 if err != nil {
@@ -8,7 +8,8 @@ import ( @@ -8,7 +8,8 @@ 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 - 11 +var LOG_TYPE = "console"
  12 +var LOG_FILE = "app.log"
12 var HTTP_PORT int = 8083 13 var HTTP_PORT int = 8083
13 14
14 //天联共创基础模块 15 //天联共创基础模块
@@ -30,6 +31,9 @@ func init() { @@ -30,6 +31,9 @@ func init() {
30 if os.Getenv("LOG_LEVEL") != "" { 31 if os.Getenv("LOG_LEVEL") != "" {
31 LOG_LEVEL = os.Getenv("LOG_LEVEL") 32 LOG_LEVEL = os.Getenv("LOG_LEVEL")
32 } 33 }
  34 + if os.Getenv("LOG_TYPE") != "" {
  35 + LOG_TYPE = os.Getenv("LOG_TYPE")
  36 + }
33 if os.Getenv("ALLIED_CREATION_BASIC_HOST") != "" { 37 if os.Getenv("ALLIED_CREATION_BASIC_HOST") != "" {
34 ALLIED_CREATION_BASIC_HOST = os.Getenv("ALLIED_CREATION_BASIC_HOST") 38 ALLIED_CREATION_BASIC_HOST = os.Getenv("ALLIED_CREATION_BASIC_HOST")
35 } 39 }
@@ -161,17 +161,18 @@ type ( @@ -161,17 +161,18 @@ type (
161 //查询共创申请 161 //查询共创申请
162 type ( 162 type (
163 ReqCooperationApplicationSearch struct { 163 ReqCooperationApplicationSearch struct {
164 - ApplicantName string `json:"applicantName"` //申请人姓名  
165 - CooperationApplicationStatus int `json:"cooperationApplicationStatus"` //共创申请审核状态,1待审核,2已同意,3已拒绝  
166 - CooperationProjectName string `json:"cooperationProjectName"` //共创项目名称  
167 - CooperationProjectNumber string `json:"cooperationProjectNumber"` //共创项目编号  
168 - PageNumber int `json:"pageNumber"`  
169 - PageSize int `json:"pageSize"`  
170 - CompanyId int `json:"companyId"`  
171 - OrgId int64 `json:"orgId"` // 组织机构ID  
172 - UserId int64 `json:"userId"`  
173 - UserBaseId int64 `json:"userBaseId"`  
174 - IsCanceled int `json:"isCanceled"` //1正常,2取消,3所有 164 + ApplicantName string `json:"applicantName"` //申请人姓名
  165 + CooperationApplicationStatus int `json:"cooperationApplicationStatus"` //共创申请审核状态,1待审核,2已同意,3已拒绝
  166 + CooperationProjectName string `json:"cooperationProjectName"` //共创项目名称
  167 + CooperationProjectNumber string `json:"cooperationProjectNumber"` //共创项目编号
  168 + PageNumber int `json:"pageNumber"`
  169 + PageSize int `json:"pageSize"`
  170 + CompanyId int `json:"companyId"`
  171 + OrgId int64 `json:"orgId"` // 组织机构ID
  172 + OrgIds []int64 `json:"orgIds"` // 组织机构ID
  173 + UserId int64 `json:"userId"`
  174 + UserBaseId int64 `json:"userBaseId"`
  175 + IsCanceled int `json:"isCanceled"` //1正常,2取消,3所有
175 } 176 }
176 177
177 DataCooperationApplicationSearch struct { 178 DataCooperationApplicationSearch struct {
  1 +package log
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "fmt"
  7 + "io"
  8 + "strconv"
  9 +
  10 + "github.com/beego/beego/v2/core/logs"
  11 + "github.com/linmadan/egglib-go/log"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
  13 +)
  14 +
  15 +type LoggerConfig struct {
  16 + Level int `json:"level,omitempty"`
  17 + Filename string `json:"filename,omitempty"`
  18 + MaxSize int `json:"maxsize,omitempty"`
  19 + MaxBackups int `json:"max_backups,omitempty"`
  20 + MaxAge int `json:"max_age,omitempty"`
  21 + Compress bool `json:"compress,omitempty"`
  22 +}
  23 +
  24 +type BeegoLog struct {
  25 + serviceName string
  26 + beeLogger *logs.BeeLogger
  27 +}
  28 +
  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) {
  57 +
  58 +}
  59 +
  60 +func (logger *BeegoLog) SetServiceName(serviceName string) {
  61 + logger.serviceName = serviceName
  62 +}
  63 +
  64 +func (logger *BeegoLog) SetLevel(level string) {
  65 + ilv, err := strconv.Atoi(level)
  66 + if err != nil {
  67 + ilv = logs.LevelDebug
  68 + }
  69 + logger.beeLogger.SetLevel(ilv)
  70 +}
  71 +
  72 +func (logger *BeegoLog) Trace(msg string, appends ...map[string]interface{}) {
  73 + logs.Trace(msg, appends)
  74 +}
  75 +
  76 +func (logger *BeegoLog) Debug(msg string, appends ...map[string]interface{}) {
  77 + appendsJson, _ := json.Marshal(appends)
  78 + if appendsJson == nil {
  79 + logs.Debug(msg, make(map[string]interface{}, 0))
  80 + } else {
  81 + var out bytes.Buffer
  82 + err := json.Indent(&out, appendsJson, "", " ")
  83 + if err != nil {
  84 + fmt.Println(msg)
  85 + return
  86 + }
  87 + logs.Debug(msg, out.String())
  88 + }
  89 +}
  90 +
  91 +func (logger *BeegoLog) Info(msg string, appends ...map[string]interface{}) {
  92 + appendsJson, _ := json.Marshal(appends)
  93 + if appendsJson == nil {
  94 + logs.Info(msg, make(map[string]interface{}, 0))
  95 + } else {
  96 + var out bytes.Buffer
  97 + err := json.Indent(&out, appendsJson, "", " ")
  98 + if err != nil {
  99 + fmt.Println(msg)
  100 + return
  101 + }
  102 + logs.Info(msg, out.String())
  103 + }
  104 +}
  105 +
  106 +func (logger *BeegoLog) Warn(msg string, appends ...map[string]interface{}) {
  107 + logs.Warn(msg, appends)
  108 +}
  109 +
  110 +func (logger *BeegoLog) Error(msg string, appends ...map[string]interface{}) {
  111 + logs.Error(msg, appends)
  112 +}
  113 +
  114 +func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) {
  115 + logs.Error(msg, appends)
  116 +}
  117 +
  118 +func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) {
  119 + logs.Error(msg, appends)
  120 +}
@@ -4,13 +4,18 @@ import ( @@ -4,13 +4,18 @@ import (
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant" 4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
5 5
6 "github.com/linmadan/egglib-go/log" 6 "github.com/linmadan/egglib-go/log"
7 - "github.com/linmadan/egglib-go/log/logrus"  
8 ) 7 )
9 8
10 var Logger log.Logger 9 var Logger log.Logger
11 10
12 func init() { 11 func init() {
13 - Logger = logrus.NewLogrusLogger()  
14 - Logger.SetServiceName(constant.SERVICE_NAME)  
15 - Logger.SetLevel(constant.LOG_LEVEL) 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 + })
16 } 21 }
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "io/ioutil"
  5 +
4 "github.com/linmadan/egglib-go/web/beego" 6 "github.com/linmadan/egglib-go/web/beego"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/query" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/query"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/service" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/service"
@@ -33,3 +35,8 @@ func (controller *CommonController) AppSharing() { @@ -33,3 +35,8 @@ func (controller *CommonController) AppSharing() {
33 data, err := commonService.AppSharing() 35 data, err := commonService.AppSharing()
34 controller.Response(data, err) 36 controller.Response(data, err)
35 } 37 }
  38 +
  39 +func (controller *CommonController) LogData() {
  40 + bytes, _ := ioutil.ReadFile("./app.log")
  41 + controller.Ctx.WriteString(string(bytes))
  42 +}
@@ -9,4 +9,5 @@ func init() { @@ -9,4 +9,5 @@ func init() {
9 web.Router("/v1/common/dictionary/search", &controllers.CommonController{}, "Post:GetDictionaryByCode") 9 web.Router("/v1/common/dictionary/search", &controllers.CommonController{}, "Post:GetDictionaryByCode")
10 web.Router("/v1/common/version/getLatestVersionInfo", &controllers.CommonController{}, "Post:LatestVersionInfo") 10 web.Router("/v1/common/version/getLatestVersionInfo", &controllers.CommonController{}, "Post:LatestVersionInfo")
11 web.Router("/v1/common/app-sharing", &controllers.CommonController{}, "Post:AppSharing") 11 web.Router("/v1/common/app-sharing", &controllers.CommonController{}, "Post:AppSharing")
  12 + web.Router("/log", &controllers.CommonController{}, "Get:LogData")
12 } 13 }