From 8d7fdb2a9869a197cd5fe3187a97e9ce86711a41 Mon Sep 17 00:00:00 2001 From: liangzy Date: Tue, 28 Apr 2020 17:17:21 +0800 Subject: [PATCH] =?UTF-8?q?v1.2.1=20=E5=A2=9E=E5=8A=A0es=E7=AE=A1=E9=81=93?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++++++++++++++++++++++++++++ log_test.go | 4 ++++ logs/es/es.go | 6 ++++++ logs/es/sys.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 logs/es/sys.go diff --git a/README.md b/README.md index 1a13245..892d529 100644 --- a/README.md +++ b/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/` 文件夹 请务必在项目构建阶段创建该目录 \ No newline at end of file diff --git a/log_test.go b/log_test.go index b7b28a0..cffd49f 100644 --- a/log_test.go +++ b/log_test.go @@ -17,3 +17,7 @@ func TestGetLogger(t *testing.T) { Get("wifi").Error("neoweiwoewe") } } + +func TestGetEs(t *testing.T) { + +} diff --git a/logs/es/es.go b/logs/es/es.go index fb6c75c..a5e2ceb 100644 --- a/logs/es/es.go +++ b/logs/es/es.go @@ -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") diff --git a/logs/es/sys.go b/logs/es/sys.go new file mode 100644 index 0000000..ddd4eec --- /dev/null +++ b/logs/es/sys.go @@ -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 +}