2020-04-27 21:26:13 +08:00
|
|
|
package grlogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/astaxie/beego/logs"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-05-06 14:32:26 +08:00
|
|
|
"strings"
|
2020-04-27 21:26:13 +08:00
|
|
|
)
|
|
|
|
|
2020-05-06 14:32:26 +08:00
|
|
|
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())
|
|
|
|
},
|
2020-04-27 21:26:13 +08:00
|
|
|
|
2020-04-28 10:37:47 +08:00
|
|
|
AdapterFile: func(l *Logger, level Level) error {
|
2020-04-27 21:26:13 +08:00
|
|
|
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,
|
|
|
|
}
|
2020-04-28 10:37:47 +08:00
|
|
|
|
2020-04-27 21:26:13 +08:00
|
|
|
return l.SetLogger(logs.AdapterFile, c.String())
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-04-28 10:37:47 +08:00
|
|
|
AdapterConsole: func(l *Logger, level Level) error {
|
2020-04-27 21:26:13 +08:00
|
|
|
c := ConsoleLogConfig{Level: level}
|
|
|
|
return l.SetLogger(logs.AdapterConsole, c.String())
|
|
|
|
},
|
|
|
|
|
2020-04-28 10:37:47 +08:00
|
|
|
AdapterElasticSearch: func(l *Logger, level Level) error {
|
2020-05-06 15:30:24 +08:00
|
|
|
c := GenEsConfig(level)
|
2020-04-27 21:26:13 +08:00
|
|
|
return l.SetLogger(logs.AdapterEs, c.String())
|
|
|
|
},
|
2020-05-04 21:10:20 +08:00
|
|
|
|
|
|
|
AdapterAliLs: func(l *Logger, level Level) error {
|
2020-05-06 15:30:24 +08:00
|
|
|
c := GenAliConfig(level)
|
|
|
|
return l.SetLogger(logs.AdapterAliLS, c.String())
|
|
|
|
},
|
|
|
|
}
|
2020-05-04 21:10:20 +08:00
|
|
|
|
2020-05-06 15:30:24 +08:00
|
|
|
func GenAliConfig(level Level) AliLSConfig {
|
|
|
|
var project string = "gaore-app-logstore"
|
|
|
|
var endpoint string
|
2020-05-05 16:10:42 +08:00
|
|
|
|
2020-05-06 15:30:24 +08:00
|
|
|
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" {
|
|
|
|
endpoint = project + ".cn-shenzhen-intranet.log.aliyuncs.com"
|
|
|
|
} else if os.Getenv(envkey) == "dev" {
|
|
|
|
endpoint = project + ".cn-shenzhen.log.aliyuncs.com"
|
|
|
|
}
|
2020-05-05 16:10:42 +08:00
|
|
|
|
2020-05-06 15:30:24 +08:00
|
|
|
c := AliLSConfig{
|
|
|
|
Project: project,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
KeyID: "LTAI4GCHwcqtrFD4DHRHxR4k",
|
|
|
|
KeySecret: "Ln19xfVYy6OMlJeF9aBvFl4fhRUKBl",
|
|
|
|
LogStore: "gaore-app-logstore",
|
|
|
|
Topics: []string{os.Getenv("GRLOG_APP_NAME")},
|
|
|
|
Source: "",
|
|
|
|
Level: level,
|
|
|
|
FlushWhen: 0,
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2020-05-04 21:10:20 +08:00
|
|
|
|
2020-05-06 15:30:24 +08:00
|
|
|
func GenEsConfig(level Level) EsConfig {
|
|
|
|
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
|
|
|
|
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" {
|
|
|
|
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
|
|
|
|
}
|
|
|
|
|
|
|
|
c := EsConfig{
|
|
|
|
Username: "elastic",
|
|
|
|
Password: "Hellogaore@",
|
|
|
|
Dsn: dsn,
|
|
|
|
Level: level,
|
|
|
|
Index: os.Getenv("GRLOG_APP_NAME"),
|
|
|
|
}
|
|
|
|
return c
|
2020-04-27 21:26:13 +08:00
|
|
|
}
|
2020-05-06 14:32:26 +08:00
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|