优化
This commit is contained in:
parent
3acbffe5f4
commit
e662190f7d
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ConsoleLogConfig struct {
|
type ConsoleLogConfig struct {
|
||||||
Level int `json:"level"`
|
Level Level `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConsoleLogConfig) String() string {
|
func (c *ConsoleLogConfig) String() string {
|
||||||
@ -16,7 +16,7 @@ func (c *ConsoleLogConfig) String() string {
|
|||||||
|
|
||||||
type FileLogConfig struct {
|
type FileLogConfig struct {
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Level int `json:"level"`
|
Level Level `json:"level"`
|
||||||
Maxlines int `json:"maxlines"`
|
Maxlines int `json:"maxlines"`
|
||||||
Daily bool `json:"daily"`
|
Daily bool `json:"daily"`
|
||||||
Maxdays int `json:"maxdays"`
|
Maxdays int `json:"maxdays"`
|
||||||
@ -34,7 +34,7 @@ type ConnLogConfig struct {
|
|||||||
Reconnect bool `json:"reconnect"`
|
Reconnect bool `json:"reconnect"`
|
||||||
Net string `json:"net"`
|
Net string `json:"net"`
|
||||||
Addr string `json:"addr"`
|
Addr string `json:"addr"`
|
||||||
Level int `json:"level"`
|
Level Level `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnLogConfig) String() string {
|
func (c *ConnLogConfig) String() string {
|
||||||
@ -44,7 +44,7 @@ func (c *ConnLogConfig) String() string {
|
|||||||
|
|
||||||
type EsConfig struct {
|
type EsConfig struct {
|
||||||
Dsn string `json:"dsn"`
|
Dsn string `json:"dsn"`
|
||||||
Level int `json:"level"`
|
Level Level `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EsConfig) String() string {
|
func (c *EsConfig) String() string {
|
||||||
|
@ -7,9 +7,9 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
var adatperMapper = map[string]func(l *Logger, level int) error{
|
var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
|
||||||
|
|
||||||
AdapterFile: func(l *Logger, level int) error {
|
AdapterFile: func(l *Logger, level Level) error {
|
||||||
if wd, err := os.Getwd(); err == nil {
|
if wd, err := os.Getwd(); err == nil {
|
||||||
c := FileLogConfig{
|
c := FileLogConfig{
|
||||||
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", l.Lable)),
|
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", l.Lable)),
|
||||||
@ -20,18 +20,19 @@ var adatperMapper = map[string]func(l *Logger, level int) error{
|
|||||||
Color: true,
|
Color: true,
|
||||||
Rotate: true,
|
Rotate: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
return l.SetLogger(logs.AdapterFile, c.String())
|
return l.SetLogger(logs.AdapterFile, c.String())
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
AdapterConsole: func(l *Logger, level int) error {
|
AdapterConsole: func(l *Logger, level Level) error {
|
||||||
c := ConsoleLogConfig{Level: level}
|
c := ConsoleLogConfig{Level: level}
|
||||||
return l.SetLogger(logs.AdapterConsole, c.String())
|
return l.SetLogger(logs.AdapterConsole, c.String())
|
||||||
},
|
},
|
||||||
|
|
||||||
AdapterElasticSearch: func(l *Logger, level int) error {
|
AdapterElasticSearch: func(l *Logger, level Level) error {
|
||||||
|
|
||||||
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
|
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
|
||||||
if envkey == "prod" || envkey == "" {
|
if envkey == "prod" || envkey == "" {
|
||||||
|
9
go.mod
9
go.mod
@ -1,9 +0,0 @@
|
|||||||
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
46
go.sum
@ -1,46 +0,0 @@
|
|||||||
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
20
log.go
@ -16,7 +16,7 @@ type Logger struct {
|
|||||||
*logs.BeeLogger
|
*logs.BeeLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Logger) SetAdapter(level int, adapter string) *Logger {
|
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
||||||
if call, ok := adatperMapper[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,16 +25,17 @@ func (self *Logger) SetAdapter(level int, adapter string) *Logger {
|
|||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(label string, channelLens ...int64) (l *Logger) {
|
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
|
||||||
var channellens int64
|
var channellens int64
|
||||||
s, ok := loggers.LoadOrStore(label, new(Logger))
|
var tmp interface{}
|
||||||
l = s.(*Logger)
|
|
||||||
|
|
||||||
|
tmp, loaded = loggers.LoadOrStore(label, new(Logger))
|
||||||
|
l = tmp.(*Logger)
|
||||||
if len(channelLens) > 0 {
|
if len(channelLens) > 0 {
|
||||||
channellens = channelLens[0]
|
channellens = channelLens[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !loaded {
|
||||||
l.Lable = label
|
l.Lable = label
|
||||||
l.BeeLogger = logs.NewLogger(channellens)
|
l.BeeLogger = logs.NewLogger(channellens)
|
||||||
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
||||||
@ -43,10 +44,15 @@ func New(label string, channelLens ...int64) (l *Logger) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Get(label string, channelLens ...int64) (l *Logger) {
|
func Get(label string, channelLens ...int64) (l *Logger) {
|
||||||
var filelevel int = LevelWarning
|
var filelevel Level = LevelWarning
|
||||||
|
var loaded bool
|
||||||
if os.Getenv(envkey) == "dev" {
|
if os.Getenv(envkey) == "dev" {
|
||||||
filelevel = LevelAll
|
filelevel = LevelAll
|
||||||
}
|
}
|
||||||
l = New(label, channelLens...).SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
|
|
||||||
|
if l, loaded = New(label, channelLens...); !loaded {
|
||||||
|
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package grlogs
|
package grlogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetLogger(t *testing.T) {
|
func TestGetLogger(t *testing.T) {
|
||||||
l := Get("nds", 128).SetAdapter(LevelNone, AdapterElasticSearch)
|
l := Get("nds", 128)
|
||||||
l.Debug("我正在调试")
|
l.Debug("我正在调试")
|
||||||
l.Critical("出错了")
|
l.Critical("出错了")
|
||||||
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
|
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
|
||||||
|
30
options.go
30
options.go
@ -1,20 +1,24 @@
|
|||||||
package grlogs
|
package grlogs
|
||||||
|
|
||||||
|
type Level int
|
||||||
|
|
||||||
|
type Adapter string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_ = iota
|
_ Level = iota
|
||||||
LevelNone = iota
|
LevelNone Level = iota
|
||||||
LevelCritical = iota
|
LevelCritical Level = iota
|
||||||
LevelError = iota
|
LevelError Level = iota
|
||||||
LevelWarning = iota
|
LevelWarning Level = iota
|
||||||
LevelWarn = iota
|
LevelWarn Level = iota
|
||||||
LevelInfo = iota
|
LevelInfo Level = iota
|
||||||
LevelDebug = iota
|
LevelDebug Level = iota
|
||||||
LevelAll = iota
|
LevelAll Level = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AdapterFile = "file"
|
AdapterFile Adapter = "file"
|
||||||
AdapterConsole = "console"
|
AdapterConsole Adapter = "console"
|
||||||
AdapterSocket = "socket"
|
AdapterSocket Adapter = "socket"
|
||||||
AdapterElasticSearch = "es"
|
AdapterElasticSearch Adapter = "es"
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user