v1.2.1 增加es管道写入日志
This commit is contained in:
parent
f35dc15fb2
commit
8d7fdb2a98
28
README.md
28
README.md
@ -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/` 文件夹 请务必在项目构建阶段创建该目录
|
@ -17,3 +17,7 @@ func TestGetLogger(t *testing.T) {
|
||||
Get("wifi").Error("neoweiwoewe")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEs(t *testing.T) {
|
||||
|
||||
}
|
||||
|
@ -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
40
logs/es/sys.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user