145 lines
3.3 KiB
Go
145 lines
3.3 KiB
Go
package grlogs
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/astaxie/beego/logs"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{
|
|
|
|
AdapterSocket: func(l *Logger, level Level) error {
|
|
c := ConnLogConfig{
|
|
ReconnectOnMsg: false,
|
|
Reconnect: true,
|
|
Net: "",
|
|
Addr: "127.0.0.1:9888",
|
|
Level: level,
|
|
}
|
|
return l.SetLogger(logs.AdapterConn, c.String())
|
|
},
|
|
|
|
AdapterFile: func(l *Logger, level Level) error {
|
|
if wd, err := os.Getwd(); err == nil {
|
|
c := FileLogConfig{
|
|
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", l.Lable)),
|
|
Level: level,
|
|
Maxlines: 0,
|
|
Daily: true,
|
|
Maxdays: 7,
|
|
Color: true,
|
|
Rotate: true,
|
|
}
|
|
|
|
return l.SetLogger(logs.AdapterFile, c.String())
|
|
} else {
|
|
return err
|
|
}
|
|
},
|
|
|
|
AdapterConsole: func(l *Logger, level Level) error {
|
|
c := ConsoleLogConfig{Level: level}
|
|
return l.SetLogger(logs.AdapterConsole, c.String())
|
|
},
|
|
|
|
AdapterElasticSearch: func(l *Logger, level Level) error {
|
|
c := GenEsConfig(level)
|
|
return l.SetLogger(logs.AdapterEs, c.String())
|
|
},
|
|
|
|
AdapterAliLs: func(l *Logger, level Level) error {
|
|
c := GenAliConfig(level)
|
|
return l.SetLogger(logs.AdapterAliLS, c.String())
|
|
},
|
|
}
|
|
|
|
func GenAliConfig(level Level) *AliLSConfig {
|
|
var project = "gr-go-project"
|
|
var endpoint string
|
|
|
|
intranet := os.Getenv("GRLOG_INTERNET") != "public"
|
|
if intranet && (os.Getenv(envkey) == "development" || os.Getenv(envkey) == "testing") {
|
|
intranet = false
|
|
}
|
|
|
|
if intranet {
|
|
endpoint = "cn-hongkong-intranet.log.aliyuncs.com"
|
|
} else {
|
|
endpoint = "cn-hongkong.log.aliyuncs.com"
|
|
}
|
|
source := os.Getenv("GRLOG_SOURCE")
|
|
c := &AliLSConfig{
|
|
Project: project,
|
|
Endpoint: endpoint,
|
|
KeyID: "LTAI5tMA7iMGFfuc3xSRVCGQ",
|
|
KeySecret: "z98EpzmYmgiVLKizxP7pMF4BPbHfPV",
|
|
LogStore: os.Getenv("GRLOG_APP_NAME"),
|
|
Topics: []string{os.Getenv("GRLOG_TOPIC")},
|
|
Source: source,
|
|
Level: level,
|
|
FlushWhen: 0,
|
|
}
|
|
return c
|
|
}
|
|
|
|
func GenEsConfig(level Level) *EsConfig {
|
|
dsn := "http://es-cn-tl32xlfmu00015h34.public.elasticsearch.aliyuncs.com:9200/"
|
|
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" {
|
|
dsn = "http://es-cn-tl32xlfmu00015h34.elasticsearch.aliyuncs.com:9200/"
|
|
}
|
|
|
|
c := &EsConfig{
|
|
Username: "kaifa_api",
|
|
Password: "2quYX3bTeahO",
|
|
Dsn: dsn,
|
|
Level: level,
|
|
Index: os.Getenv("GRLOG_APP_NAME"),
|
|
}
|
|
return c
|
|
}
|
|
|
|
var adatperDropMapper = map[Adapter]func(l *Logger) error{
|
|
|
|
AdapterAliLs: func(l *Logger) error {
|
|
return l.BeeLogger.DelLogger(logs.AdapterAliLS)
|
|
},
|
|
|
|
AdapterFile: func(l *Logger) error {
|
|
return l.BeeLogger.DelLogger(logs.AdapterFile)
|
|
},
|
|
|
|
AdapterConsole: func(l *Logger) error {
|
|
return l.BeeLogger.DelLogger(logs.AdapterConsole)
|
|
},
|
|
|
|
AdapterElasticSearch: func(l *Logger) error {
|
|
return l.BeeLogger.DelLogger(logs.AdapterEs)
|
|
},
|
|
}
|
|
|
|
func formatLog(f interface{}, v ...interface{}) string {
|
|
var msg string
|
|
switch f.(type) {
|
|
case string:
|
|
msg = f.(string)
|
|
if len(v) == 0 {
|
|
return msg
|
|
}
|
|
if strings.Contains(msg, "%") && !strings.Contains(msg, "%%") {
|
|
//format string
|
|
} else {
|
|
//do not contain format char
|
|
msg += strings.Repeat(" %v", len(v))
|
|
}
|
|
default:
|
|
msg = fmt.Sprint(f)
|
|
if len(v) == 0 {
|
|
return msg
|
|
}
|
|
msg += strings.Repeat(" %v", len(v))
|
|
}
|
|
return fmt.Sprintf(msg, v...)
|
|
}
|