grlogs/funtion.go
2020-05-05 16:10:42 +08:00

79 lines
1.9 KiB
Go

package grlogs
import (
"fmt"
"github.com/astaxie/beego/logs"
"os"
"path"
)
var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
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 {
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 l.SetLogger(logs.AdapterEs, c.String())
},
AdapterAliLs: func(l *Logger, level Level) error {
var project string = "gaore-app-logstore"
var endpoint string
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"
}
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 l.SetLogger(logs.AdapterAliLS, c.String())
},
}