Compare commits
2 Commits
01c35869b8
...
3a09faf4b8
Author | SHA1 | Date | |
---|---|---|---|
|
3a09faf4b8 | ||
|
8998a4938d |
22
README.md
22
README.md
@ -23,7 +23,7 @@ logger.Critical("出错了")
|
||||
logger.Info("出错了")
|
||||
```
|
||||
|
||||
4. 如果需要写入es, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
|
||||
4. 如果需要写入es 或 alils, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
|
||||
```shell script
|
||||
export GRLOG_APP_NAME=mkt.gaore.com;
|
||||
```
|
||||
@ -32,10 +32,19 @@ export GRLOG_APP_NAME=mkt.gaore.com;
|
||||
import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
||||
```
|
||||
|
||||
```go
|
||||
import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
|
||||
```
|
||||
|
||||
5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
|
||||
|
||||
|
||||
6. 完整示例
|
||||
6. `AliLS` 日志接入的是阿里SLS GOSDK , 下面是关于 [https://github.com/aliyun/aliyun-log-go-sdk/tree/master/producer] 描述
|
||||
|
||||
producer提供了两种关闭模式,分为有限关闭和安全关闭,安全关闭会等待producer中缓存的所有的数据全部发送完成以后在关闭producer,有限关闭会接收用户传递的一个参数值,时间单位为秒,当开始关闭producer的时候开始计时,超过传递的设定值还未能完全关闭producer的话会强制退出producer,此时可能会有部分数据未被成功发送而丢失。
|
||||
所以用了aliLS 的 adapter 时,最好调用`grlogs.Close()` 或 `grlogs.CloseAll()` 方法安全关闭通道,以刷新缓冲区
|
||||
|
||||
7. 完整示例
|
||||
```go
|
||||
package grlogs
|
||||
|
||||
@ -71,7 +80,12 @@ func TestGetLogger(t *testing.T) {
|
||||
Get("wifi").Critical("neoweiwoewe")
|
||||
}
|
||||
|
||||
func TestGetEs(t *testing.T) {
|
||||
fmt.Println("hello world")
|
||||
func TestDropAdapter(t *testing.T) {
|
||||
SetAdapter(LevelAll, AdapterAliLs)
|
||||
DropAdapter(AdapterAliLs)
|
||||
Informational(errors.New("he hello"))
|
||||
SetAdapter(LevelAll, AdapterAliLs)
|
||||
Debug(errors.New("he hello"))
|
||||
CloseAll()
|
||||
}
|
||||
```
|
57
funtion.go
57
funtion.go
@ -5,9 +5,21 @@ import (
|
||||
"github.com/astaxie/beego/logs"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
|
||||
var adatperSetMapper = 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 {
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
@ -76,3 +88,46 @@ var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
|
||||
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...)
|
||||
}
|
||||
|
59
grlogs.go
Normal file
59
grlogs.go
Normal file
@ -0,0 +1,59 @@
|
||||
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
11
log.go
@ -17,7 +17,7 @@ type Logger struct {
|
||||
}
|
||||
|
||||
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
||||
if call, ok := adatperMapper[adapter]; ok {
|
||||
if call, ok := adatperSetMapper[adapter]; ok {
|
||||
if err := call(self, level); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
@ -25,6 +25,15 @@ func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
||||
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) {
|
||||
var channellens int64
|
||||
var tmp interface{}
|
||||
|
10
log_test.go
10
log_test.go
@ -1,6 +1,7 @@
|
||||
package grlogs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
_ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
|
||||
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
||||
"testing"
|
||||
@ -49,3 +50,12 @@ func TestGetAliLs(t *testing.T) {
|
||||
|
||||
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user