Compare commits

..

No commits in common. "f0f0b1db40ad3d897d47cd573ff0796eba2484a1" and "3acbffe5f410c334899da906a178be8e0e77e724" have entirely different histories.

10 changed files with 94 additions and 70 deletions

View File

@ -6,7 +6,7 @@ import (
)
type ConsoleLogConfig struct {
Level Level `json:"level"`
Level int `json:"level"`
}
func (c *ConsoleLogConfig) String() string {
@ -16,7 +16,7 @@ func (c *ConsoleLogConfig) String() string {
type FileLogConfig struct {
Filename string `json:"filename"`
Level Level `json:"level"`
Level int `json:"level"`
Maxlines int `json:"maxlines"`
Daily bool `json:"daily"`
Maxdays int `json:"maxdays"`
@ -34,7 +34,7 @@ type ConnLogConfig struct {
Reconnect bool `json:"reconnect"`
Net string `json:"net"`
Addr string `json:"addr"`
Level Level `json:"level"`
Level int `json:"level"`
}
func (c *ConnLogConfig) String() string {
@ -43,11 +43,8 @@ func (c *ConnLogConfig) String() string {
}
type EsConfig struct {
Username string `json:"username"`
Password string `json:"password"`
Dsn string `json:"dsn"`
Level Level `json:"level"`
Index string `json:"index"`
Level int `json:"level"`
}
func (c *EsConfig) String() string {

View File

@ -7,9 +7,9 @@ import (
"path"
)
var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
var adatperMapper = map[string]func(l *Logger, level int) error{
AdapterFile: func(l *Logger, level Level) error {
AdapterFile: func(l *Logger, level int) error {
if wd, err := os.Getwd(); err == nil {
c := FileLogConfig{
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", l.Lable)),
@ -20,33 +20,28 @@ var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
Color: true,
Rotate: true,
}
return l.SetLogger(logs.AdapterFile, c.String())
} else {
return err
}
},
AdapterConsole: func(l *Logger, level Level) error {
AdapterConsole: func(l *Logger, level int) error {
c := ConsoleLogConfig{Level: level}
return l.SetLogger(logs.AdapterConsole, c.String())
},
AdapterElasticSearch: func(l *Logger, level Level) error {
AdapterElasticSearch: func(l *Logger, level int) error {
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
if envkey == "prod" || envkey == "" {
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
dsn = "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
}
c := EsConfig{
Username: "elastic",
Password: "Hellogaore@",
Dsn: dsn,
Level: level,
Index: os.Getenv("GRLOG_APP_NAME"),
}
return l.SetLogger(logs.AdapterEs, c.String())
},
}

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module golib.gaore.com/GaoreGo/grlogs
go 1.13
require (
github.com/OwnLocal/goes v1.0.0
github.com/astaxie/beego v1.12.1
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644
)

46
go.sum Normal file
View File

@ -0,0 +1,46 @@
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OwnLocal/goes v1.0.0 h1:81QQ3z6dvLhgXlkNpLkaYhk8jiKS7saFG01xy039KaU=
github.com/OwnLocal/goes v1.0.0/go.mod h1:8rIFjBGTue3lCU0wplczcUgt9Gxgrkkrw7etMIcn8TM=
github.com/astaxie/beego v1.12.1 h1:dfpuoxpzLVgclveAXe4PyNKqkzgm5zF4tgF2B3kkM2I=
github.com/astaxie/beego v1.12.1/go.mod h1:kPBWpSANNbSdIqOc8SUL9h+1oyBMZhROeYsXQDbidWQ=
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:TWI8EKQMs5u5jLKW/tsb9VwauIrMIxQG1r5fMsswK5U=
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY=
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg=
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec/go.mod h1:QBvMkMya+gXctz3kmljlUCu/yB3GZ6oee+dUozsezQE=
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200117065230-39095c1d176c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

20
log.go
View File

@ -16,7 +16,7 @@ type Logger struct {
*logs.BeeLogger
}
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
func (self *Logger) SetAdapter(level int, adapter string) *Logger {
if call, ok := adatperMapper[adapter]; ok {
if err := call(self, level); err != nil {
fmt.Println(err)
@ -25,17 +25,16 @@ func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
return self
}
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
func New(label string, channelLens ...int64) (l *Logger) {
var channellens int64
var tmp interface{}
s, ok := loggers.LoadOrStore(label, new(Logger))
l = s.(*Logger)
tmp, loaded = loggers.LoadOrStore(label, new(Logger))
l = tmp.(*Logger)
if len(channelLens) > 0 {
channellens = channelLens[0]
}
if !loaded {
if !ok {
l.Lable = label
l.BeeLogger = logs.NewLogger(channellens)
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
@ -44,15 +43,10 @@ func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
}
func Get(label string, channelLens ...int64) (l *Logger) {
var filelevel Level = LevelWarning
var loaded bool
var filelevel int = LevelWarning
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
}
if l, loaded = New(label, channelLens...); !loaded {
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
}
l = New(label, channelLens...).SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
return
}

View File

@ -7,7 +7,7 @@ import (
)
func TestGetLogger(t *testing.T) {
l := Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
l := Get("nds", 128).SetAdapter(LevelNone, AdapterElasticSearch)
l.Debug("我正在调试")
l.Critical("出错了")
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))

View File

@ -6,8 +6,8 @@ import (
"sync"
"time"
"github.com/astaxie/beego/logs"
"github.com/gogo/protobuf/proto"
"golib.gaore.com/GaoreGo/grlogs/logs"
)
const (

View File

@ -5,11 +5,10 @@ import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
"golib.gaore.com/GaoreGo/goes"
"github.com/OwnLocal/goes"
"golib.gaore.com/GaoreGo/grlogs/logs"
)
@ -25,7 +24,6 @@ type esLogger struct {
*goes.Client
DSN string `json:"dsn"`
Level int `json:"level"`
IndexName string `json:"index"`
}
// {"dsn":"http://localhost:9200/","level":1}
@ -43,17 +41,7 @@ func (el *esLogger) Init(jsonconfig string) error {
} else if host, port, err := net.SplitHostPort(u.Host); err != nil {
return err
} else {
tr := &http.Transport{
ResponseHeaderTimeout: 3 * time.Second,
}
cl := &http.Client{
Transport: tr,
}
conn := goes.NewClient(host, port)
conn.WithHTTPClient(cl)
el.Client = conn
}
return nil
@ -68,9 +56,8 @@ func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
vals := make(map[string]interface{})
vals["@timestamp"] = when.Format(time.RFC3339)
vals["@msg"] = msg
vals["@level"] = level
d := goes.Document{
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, when.Year(), when.Month(), when.Day()),
Index: fmt.Sprintf("%04d.%02d.%02d", when.Year(), when.Month(), when.Day()),
Type: "logs",
Fields: vals,
}

View File

@ -15,7 +15,7 @@
// Package logs provide a general log interface
// Usage:
//
// import "golib.gaore.com/GaoreGo/grlogs/logs"
// import "github.com/astaxie/beego/logs"
//
// log := NewLogger(10000)
// log.SetLogger("console", "")

View File

@ -1,24 +1,20 @@
package grlogs
type Level int
type Adapter string
const (
_ Level = iota
LevelNone Level = iota
LevelCritical Level = iota
LevelError Level = iota
LevelWarning Level = iota
LevelWarn Level = iota
LevelInfo Level = iota
LevelDebug Level = iota
LevelAll Level = iota
_ = iota
LevelNone = iota
LevelCritical = iota
LevelError = iota
LevelWarning = iota
LevelWarn = iota
LevelInfo = iota
LevelDebug = iota
LevelAll = iota
)
const (
AdapterFile Adapter = "file"
AdapterConsole Adapter = "console"
AdapterSocket Adapter = "socket"
AdapterElasticSearch Adapter = "es"
AdapterFile = "file"
AdapterConsole = "console"
AdapterSocket = "socket"
AdapterElasticSearch = "es"
)