Compare commits

..

No commits in common. "3a09faf4b863c986066ba357e8193b2453eca93b" and "01c35869b897d0e704c47b3712c0a1ee7f90087d" have entirely different histories.

5 changed files with 6 additions and 153 deletions

View File

@ -23,7 +23,7 @@ logger.Critical("出错了")
logger.Info("出错了") logger.Info("出错了")
``` ```
4. 如果需要写入es 或 alils 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如 4. 如果需要写入es 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
```shell script ```shell script
export GRLOG_APP_NAME=mkt.gaore.com; export GRLOG_APP_NAME=mkt.gaore.com;
``` ```
@ -32,19 +32,10 @@ export GRLOG_APP_NAME=mkt.gaore.com;
import _ "golib.gaore.com/GaoreGo/grlogs/logs/es" import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
``` ```
```go
import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
```
5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录** 5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
6. `AliLS` 日志接入的是阿里SLS GOSDK , 下面是关于 [https://github.com/aliyun/aliyun-log-go-sdk/tree/master/producer] 描述 6. 完整示例
producer提供了两种关闭模式分为有限关闭和安全关闭安全关闭会等待producer中缓存的所有的数据全部发送完成以后在关闭producer有限关闭会接收用户传递的一个参数值时间单位为秒当开始关闭producer的时候开始计时超过传递的设定值还未能完全关闭producer的话会强制退出producer此时可能会有部分数据未被成功发送而丢失。
所以用了aliLS 的 adapter 时,最好调用`grlogs.Close()` 或 `grlogs.CloseAll()` 方法安全关闭通道,以刷新缓冲区
7. 完整示例
```go ```go
package grlogs package grlogs
@ -80,12 +71,7 @@ func TestGetLogger(t *testing.T) {
Get("wifi").Critical("neoweiwoewe") Get("wifi").Critical("neoweiwoewe")
} }
func TestDropAdapter(t *testing.T) { func TestGetEs(t *testing.T) {
SetAdapter(LevelAll, AdapterAliLs) fmt.Println("hello world")
DropAdapter(AdapterAliLs)
Informational(errors.New("he hello"))
SetAdapter(LevelAll, AdapterAliLs)
Debug(errors.New("he hello"))
CloseAll()
} }
``` ```

View File

@ -5,21 +5,9 @@ import (
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"os" "os"
"path" "path"
"strings"
) )
var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{ var adatperMapper = 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())
},
AdapterFile: func(l *Logger, level Level) error { AdapterFile: func(l *Logger, level Level) error {
if wd, err := os.Getwd(); err == nil { if wd, err := os.Getwd(); err == nil {
@ -88,46 +76,3 @@ var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{
return l.SetLogger(logs.AdapterAliLS, c.String()) return l.SetLogger(logs.AdapterAliLS, c.String())
}, },
} }
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...)
}

View File

@ -1,59 +0,0 @@
package grlogs
import "strings"
func Info(v ...interface{}) {
Get("grlogs").Info(generateFmtStr(len(v)), v...)
}
func Informational(v ...interface{}) {
Get("grlogs").Informational(generateFmtStr(len(v)), v...)
}
func Warning(v ...interface{}) {
Get("grlogs").Warning(generateFmtStr(len(v)), v...)
}
func Warn(v ...interface{}) {
Get("grlogs").Warn(generateFmtStr(len(v)), v...)
}
func Notice(v ...interface{}) {
Get("grlogs").Notice(generateFmtStr(len(v)), v...)
}
func Error(v ...interface{}) {
Get("grlogs").Error(generateFmtStr(len(v)), v...)
}
func Critical(v ...interface{}) {
Get("grlogs").Critical(generateFmtStr(len(v)), v...)
}
func Alert(v ...interface{}) {
Get("grlogs").Alert(generateFmtStr(len(v)), v...)
}
func Emergency(v ...interface{}) {
Get("grlogs").Emergency(generateFmtStr(len(v)), v...)
}
func Trace(v ...interface{}) {
Get("grlogs").Trace(generateFmtStr(len(v)), v...)
}
func Debug(v ...interface{}) {
Get("grlogs").Debug(generateFmtStr(len(v)), v...)
}
func generateFmtStr(n int) string {
return strings.Repeat("%v ", n)
}
func SetAdapter(level Level, adapter Adapter) {
Get("grlogs").SetAdapter(level, adapter)
}
func DropAdapter(adapter Adapter) {
Get("grlogs").DropAdapter(adapter)
}

11
log.go
View File

@ -17,7 +17,7 @@ type Logger struct {
} }
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger { func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
if call, ok := adatperSetMapper[adapter]; ok { if call, ok := adatperMapper[adapter]; ok {
if err := call(self, level); err != nil { if err := call(self, level); err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -25,15 +25,6 @@ func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
return self return self
} }
func (self *Logger) DropAdapter(adapter Adapter) *Logger {
if call, ok := adatperDropMapper[adapter]; ok {
if err := call(self); err != nil {
fmt.Println(err)
}
}
return self
}
func New(label string, channelLens ...int64) (l *Logger, loaded bool) { func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
var channellens int64 var channellens int64
var tmp interface{} var tmp interface{}

View File

@ -1,7 +1,6 @@
package grlogs package grlogs
import ( import (
"errors"
_ "golib.gaore.com/GaoreGo/grlogs/logs/alils" _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
_ "golib.gaore.com/GaoreGo/grlogs/logs/es" _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
"testing" "testing"
@ -50,12 +49,3 @@ func TestGetAliLs(t *testing.T) {
Close(lable, lable, lable) Close(lable, lable, lable)
} }
func TestDropAdapter(t *testing.T) {
SetAdapter(LevelAll, AdapterAliLs)
DropAdapter(AdapterAliLs)
Informational(errors.New("he hello"))
SetAdapter(LevelAll, AdapterAliLs)
Debug(errors.New("he hello"))
CloseAll()
}