作者 陈志颖

chore:添加日志文件

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
2021/09/07 01:40:09.439 [I] [hooks.go:101] generate router from comments
2021/09/07 01:40:09.440 [I] [???:0] http server Running on http://:8083
2021/09/07 01:40:12.667 [I] [filter.go:81] http请求 [
{
"framework": "beego",
"method": "GET",
"url": "/log"
}
]
2021/09/07 01:40:12.667 [D] [filter.go:81] http请求 [
{
"framework": "beego",
"method": "GET",
"url": "/log"
}
]
2021/09/07 01:40:12.669 [D] [filter.go:81] http响应 [
{
"framework": "beego",
"method": "GET",
"outputData": null,
"url": "/log"
}
]
2021/09/07 01:40:12.669 [D] [router.go:664] | ::1| 200 | 2.412278ms| match| GET  /log r:/log
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
2021/09/07 01:42:15.705 [I] [hooks.go:101] generate router from comments
2021/09/07 01:42:15.706 [I] [???:0] http server Running on http://:8083
... ...
... ... @@ -91,12 +91,16 @@ spec:
value: ""
- name: LOG_LEVEL
value: "debug"
- name: LOG_FRAMEWORK
value: "beego"
- name: LOG_TYPE
value: "file"
- name: ERROR_BASE_CODE
value: "1"
- name: ERROR_BASE_CODE_MULTIPLE
value: "1000"
- name: ENABLE_KAFKA_LOG
value: "true"
value: "false"
- name: HTTP_PORT
value: "8082"
- name: ALLIED_CREATION_USER_HOST
... ...
... ... @@ -48,6 +48,7 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elastic/go-elasticsearch/v6 v6.8.5 h1:U2HtkBseC1FNBmDr0TR2tKltL6FxoY+niDAlj5M8TK8=
github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox93HoX8utj1kxD9aFUcI=
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
... ...
... ... @@ -8,6 +8,10 @@ import (
const SERVICE_NAME = "allied-creation-gateway"
var LOG_LEVEL = "debug"
var LOG_TYPE = "terminal" // file, terminal, es
var LOG_FRAMEWORK = "beego" // beego logrus
var LOG_FILE = "app.log"
var LOG_PREFIX = "[allied-creation-gateway]"
var HTTP_PORT int = 8083
... ...
... ... @@ -19,6 +19,10 @@ func init() {
// POSTGRESQL_PORT = os.Getenv("TOPIC_LOG_STASH")
//}
if os.Getenv("ENABLE_KAFKA_LOG") != "" {
ENABLE_KAFKA_LOG = true
if os.Getenv("ENABLE_KAFKA_LOG") == "true" {
ENABLE_KAFKA_LOG = true
} else if os.Getenv("ENABLE_KAFKA_LOG") == "false" {
ENABLE_KAFKA_LOG = false
}
}
}
... ...
package log
import (
"bytes"
"encoding/json"
"fmt"
"github.com/beego/beego/v2/core/logs"
_ "github.com/beego/beego/v2/core/logs/es"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
"io"
"strconv"
)
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
}
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)
}
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 if constant.LOG_TYPE == "es" { // 日志存储到ES
err := logger.SetLogger(logs.AdapterEs, `{"dsn":"http://192.168.100.222:9200/","level":7}`)
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.LOG_PREFIX)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(5)
logger.SetPrefix(constant.LOG_PREFIX)
return &BeegoLog{
beeLogger: logger,
}
}
... ...
... ... @@ -10,7 +10,15 @@ import (
var Logger log.Logger
func init() {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
if constant.LOG_FRAMEWORK == "beego" {
Logger = NewBeegoLog(LoggerConfig{
Filename: constant.LOG_FILE,
Level: 7,
MaxSize: 1024 * 1024 * 2,
})
} else if constant.LOG_FRAMEWORK == "logrus" {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
}
}
... ...
package controllers
import "io/ioutil"
type LoggerController struct {
BaseController
}
func (controller LoggerController) Index() {
bytes, _ := ioutil.ReadFile("./app.log")
controller.Ctx.WriteString(string(bytes))
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers"
)
func init() {
web.Router("/log", &controllers.LoggerController{}, "Get:Index")
}
... ...