作者 tangxuhui

添加日志文件

... ... @@ -91,6 +91,8 @@ spec:
value: ""
- name: LOG_LEVEL
value: "debug"
- name: LOG_TYPE
value: file
- name: ERROR_BASE_CODE
value: "1"
- name: ERROR_BASE_CODE_MULTIPLE
... ...
... ... @@ -70,6 +70,7 @@ func (cooperationApplicationService *CooperationApplicationService) ListCooperat
PageNumber: listCooperationApplicationQuery.PageNumber,
PageSize: listCooperationApplicationQuery.PageSize,
CompanyId: int(listCooperationApplicationQuery.Operator.CompanyId),
OrgIds: listCooperationApplicationQuery.Operator.OrgIds,
IsCanceled: 3,
})
if err != nil {
... ...
... ... @@ -8,7 +8,8 @@ import (
const SERVICE_NAME = "allied-creation-gateway"
var LOG_LEVEL = "debug"
var LOG_TYPE = "console"
var LOG_FILE = "app.log"
var HTTP_PORT int = 8083
//天联共创基础模块
... ... @@ -30,6 +31,9 @@ func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
}
if os.Getenv("LOG_TYPE") != "" {
LOG_TYPE = os.Getenv("LOG_TYPE")
}
if os.Getenv("ALLIED_CREATION_BASIC_HOST") != "" {
ALLIED_CREATION_BASIC_HOST = os.Getenv("ALLIED_CREATION_BASIC_HOST")
}
... ...
... ... @@ -169,6 +169,7 @@ type (
PageSize int `json:"pageSize"`
CompanyId int `json:"companyId"`
OrgId int64 `json:"orgId"` // 组织机构ID
OrgIds []int64 `json:"orgIds"` // 组织机构ID
UserId int64 `json:"userId"`
UserBaseId int64 `json:"userBaseId"`
IsCanceled int `json:"isCanceled"` //1正常,2取消,3所有
... ...
package log
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"github.com/beego/beego/v2/core/logs"
"github.com/linmadan/egglib-go/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
)
type LoggerConfig struct {
Level int `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"maxsize,omitempty"`
MaxBackups int `json:"max_backups,omitempty"`
MaxAge int `json:"max_age,omitempty"`
Compress bool `json:"compress,omitempty"`
}
type BeegoLog struct {
serviceName string
beeLogger *logs.BeeLogger
}
var _ log.Logger = &BeegoLog{}
func NewBeegoLog(conf LoggerConfig) *BeegoLog {
logger := logs.GetBeeLogger()
logger.SetLevel(conf.Level)
if constant.LOG_TYPE == "file" { // 日志存储到文件
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
return nil
}
} else { // 日志输出到控制台
err := logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
if err != nil {
fmt.Println(err.Error())
return nil
}
}
logger.SetPrefix(constant.SERVICE_NAME)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(5)
return &BeegoLog{
beeLogger: logger,
}
}
func (logger *BeegoLog) AddHook(write io.Writer) {
}
func (logger *BeegoLog) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *BeegoLog) SetLevel(level string) {
ilv, err := strconv.Atoi(level)
if err != nil {
ilv = logs.LevelDebug
}
logger.beeLogger.SetLevel(ilv)
}
func (logger *BeegoLog) Trace(msg string, appends ...map[string]interface{}) {
logs.Trace(msg, appends)
}
func (logger *BeegoLog) Debug(msg string, appends ...map[string]interface{}) {
appendsJson, _ := json.Marshal(appends)
if appendsJson == nil {
logs.Debug(msg, make(map[string]interface{}, 0))
} else {
var out bytes.Buffer
err := json.Indent(&out, appendsJson, "", " ")
if err != nil {
fmt.Println(msg)
return
}
logs.Debug(msg, out.String())
}
}
func (logger *BeegoLog) Info(msg string, appends ...map[string]interface{}) {
appendsJson, _ := json.Marshal(appends)
if appendsJson == nil {
logs.Info(msg, make(map[string]interface{}, 0))
} else {
var out bytes.Buffer
err := json.Indent(&out, appendsJson, "", " ")
if err != nil {
fmt.Println(msg)
return
}
logs.Info(msg, out.String())
}
}
func (logger *BeegoLog) Warn(msg string, appends ...map[string]interface{}) {
logs.Warn(msg, appends)
}
func (logger *BeegoLog) Error(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
... ...
... ... @@ -4,13 +4,18 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/log/logrus"
)
var Logger log.Logger
func init() {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
// Logger = logrus.NewLogrusLogger()
// Logger.SetServiceName(constant.SERVICE_NAME)
// Logger.SetLevel(constant.LOG_LEVEL)
// //
Logger = NewBeegoLog(LoggerConfig{
Filename: constant.LOG_FILE,
Level: 7,
MaxSize: 1024 * 1024 * 2,
})
}
... ...
package controllers
import (
"io/ioutil"
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/common/service"
... ... @@ -33,3 +35,8 @@ func (controller *CommonController) AppSharing() {
data, err := commonService.AppSharing()
controller.Response(data, err)
}
func (controller *CommonController) LogData() {
bytes, _ := ioutil.ReadFile("./app.log")
controller.Ctx.WriteString(string(bytes))
}
... ...
... ... @@ -9,4 +9,5 @@ func init() {
web.Router("/v1/common/dictionary/search", &controllers.CommonController{}, "Post:GetDictionaryByCode")
web.Router("/v1/common/version/getLatestVersionInfo", &controllers.CommonController{}, "Post:LatestVersionInfo")
web.Router("/v1/common/app-sharing", &controllers.CommonController{}, "Post:AppSharing")
web.Router("/log", &controllers.CommonController{}, "Get:LogData")
}
... ...