v1.2.1 增加es管道写入日志

This commit is contained in:
liangzy 2020-04-28 17:17:21 +08:00
parent f35dc15fb2
commit 8d7fdb2a98
4 changed files with 78 additions and 0 deletions

View File

@ -1,2 +1,30 @@
# grlogs
本库为争游内部日志公共库
## 代码示例
1. 引入
```go
import "golib.gaore.com/GaoreGo/grlogs"
```
2. 简单用法
```go
grlogs.Get("test", 128).Info("hello word")
grlogs.Get("test").Warning("hello word")
```
lable 为通
3. 进阶用法
```go
logger := grlogs.GetEs("wifi")
logger.SetAdapter(LevelAll, AdapterElasticSearch)
logger.SetAdapter(LevelInfo, AdapterFile)
logger.Critical("出错了")
logger.Info("出错了")
```
4. 如果需要写入es 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
```shell script
export GRLOG_APP_NAME=mkt.gaore.com;
```
5. 文件日志会写入到 `./runtime/logs/` 文件夹 请务必在项目构建阶段创建该目录

View File

@ -17,3 +17,7 @@ func TestGetLogger(t *testing.T) {
Get("wifi").Error("neoweiwoewe")
}
}
func TestGetEs(t *testing.T) {
}

View File

@ -65,6 +65,7 @@ func (el *esLogger) Init(jsonconfig string) error {
// WriteMsg will write the msg and level into es
func (el *esLogger) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
if level > el.Level {
return nil
}
@ -76,6 +77,11 @@ func (el *esLogger) WriteMsg(when time.Time, msg string, level int, lable string
vals["level_string"] = logs.GetLevelString(level)
vals["env"] = env
vals["lable"] = lable
vals["hostname"] = GetHostname()
vals["working_idr"] = Getwd()
vals["home_dir"] = GetUserHomename()
vals["hardware_addr"] = GetCurrentInterface().HardwareAddr
vals["client_addrs"] = GetCurrentInterfaceAddrs()
if el.IndexName == "" {
return errors.New("index name is empty")

40
logs/es/sys.go Normal file
View File

@ -0,0 +1,40 @@
package es
import (
"fmt"
"net"
"os"
)
func GetCurrentInterface() *net.Interface {
if inter, err := net.InterfaceByName("eth0"); err == nil {
return inter
} else if inter, err := net.InterfaceByName("en0"); err == nil {
return inter
}
return nil
}
func GetCurrentInterfaceAddrs() string {
if inter := GetCurrentInterface(); inter != nil {
if addrs, err := inter.Addrs(); err == nil {
return fmt.Sprintf("%s", addrs)
}
}
return ""
}
func GetHostname() string {
hostname, _ := os.Hostname()
return hostname
}
func GetUserHomename() string {
homedir, _ := os.UserHomeDir()
return homedir
}
func Getwd() string {
wd, _ := os.Getwd()
return wd
}