Browse Source

v1.2.0 整个beegolog 打进去, 增加syncmap日志通道

tags/v1.2.2
liangzy 4 years ago
parent
commit
3acbffe5f4
9 changed files with 210 additions and 86 deletions
  1. +53
    -0
      config.go
  2. +0
    -1
      function.go
  3. +47
    -0
      funtion.go
  4. +9
    -0
      go.mod
  5. +46
    -0
      go.sum
  6. +29
    -82
      log.go
  7. +5
    -2
      log_test.go
  8. +1
    -1
      logs/es/es.go
  9. +20
    -0
      options.go

+ 53
- 0
config.go View File

@@ -0,0 +1,53 @@
package grlogs

import (
"bytes"
"encoding/json"
)

type ConsoleLogConfig struct {
Level int `json:"level"`
}

func (c *ConsoleLogConfig) String() string {
b, _ := json.Marshal(c)
return bytes.NewBuffer(b).String()
}

type FileLogConfig struct {
Filename string `json:"filename"`
Level int `json:"level"`
Maxlines int `json:"maxlines"`
Daily bool `json:"daily"`
Maxdays int `json:"maxdays"`
Color bool `json:"color"`
Rotate bool `json:"rotate"`
}

func (c *FileLogConfig) String() string {
b, _ := json.Marshal(c)
return bytes.NewBuffer(b).String()
}

type ConnLogConfig struct {
ReconnectOnMsg bool `json:"reconnect_on_msg"`
Reconnect bool `json:"reconnect"`
Net string `json:"net"`
Addr string `json:"addr"`
Level int `json:"level"`
}

func (c *ConnLogConfig) String() string {
b, _ := json.Marshal(c)
return bytes.NewBuffer(b).String()
}

type EsConfig struct {
Dsn string `json:"dsn"`
Level int `json:"level"`
}

func (c *EsConfig) String() string {
b, _ := json.Marshal(c)
return bytes.NewBuffer(b).String()
}

+ 0
- 1
function.go View File

@@ -1 +0,0 @@
package grlogs

+ 47
- 0
funtion.go View File

@@ -0,0 +1,47 @@
package grlogs

import (
"fmt"
"github.com/astaxie/beego/logs"
"os"
"path"
)

var adatperMapper = map[string]func(l *Logger, level int) 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)),
Level: level,
Maxlines: 0,
Daily: true,
Maxdays: 7,
Color: true,
Rotate: true,
}
return l.SetLogger(logs.AdapterFile, c.String())
} else {
return err
}
},

AdapterConsole: func(l *Logger, level int) error {
c := ConsoleLogConfig{Level: level}
return l.SetLogger(logs.AdapterConsole, c.String())
},

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.public.elasticsearch.aliyuncs.com:9200/"
}

c := EsConfig{
Dsn: dsn,
Level: level,
}
return l.SetLogger(logs.AdapterEs, c.String())
},
}

+ 9
- 0
go.mod 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
- 0
go.sum 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=

+ 29
- 82
log.go View File

@@ -1,105 +1,52 @@
package grlogs

import (
"bytes"
"encoding/json"
"fmt"
"golib.gaore.com/GaoreGo/grlogs/logs"
"os"
"path"
"sync"
)

const (
_ = iota
LEVEL_NONE = iota
LEVEL_CRITICAL = iota
LEVEL_ERROR = iota
LEVEL_WARNING = iota
LEVEL_WARN = iota
LEVEL_INFO = iota
LEVEL_DEBUG = iota
LEVEL_ALL = iota
)

type ConsoleLogConfig struct {
Level int `json:"level"`
}

type FileLogConfig struct {
Filename string `json:"filename"`
Level int `json:"level"`
Maxlines int `json:"maxlines"`
Daily bool `json:"daily"`
Maxdays int `json:"maxdays"`
Color bool `json:"color"`
Rotate bool `json:"rotate"`
}

type ConnLogConfig struct {
ReconnectOnMsg bool `json:"reconnect_on_msg"`
Reconnect bool `json:"reconnect"`
Net string `json:"net"`
Addr string `json:"addr"`
Level int `json:"level"`
}

var loggers = sync.Map{}

var envkey = "CENTER_RUNMODE"

type Logger struct {
Lable string
*logs.BeeLogger
}

func (l *Logger) Write(p []byte) (n int, err error) {
l.Debug(bytes.NewBuffer(p).String())
return
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)
}
}
return self
}

func (l *Logger) SetFormat() {
func New(label string, channelLens ...int64) (l *Logger) {
var channellens int64
s, ok := loggers.LoadOrStore(label, new(Logger))
l = s.(*Logger)

}
if len(channelLens) > 0 {
channellens = channelLens[0]
}

func (l *Logger) Stop() {
l.BeeLogger.Close()
if !ok {
l.Lable = label
l.BeeLogger = logs.NewLogger(channellens)
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
}
return
}

func GetLogger(name string) *Logger {

if l, ok := loggers.Load(name); ok {
return l.(*Logger)
} else {
var level int = LEVEL_WARN

if s := os.Getenv("CENTER_RUNMODE"); s == "dev" {
level = LEVEL_ALL
}

wd, _ := os.Getwd()

conf1 := FileLogConfig{
Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", name)),
Level: LEVEL_ALL,
Maxlines: 0,
Daily: true,
Maxdays: 7,
Color: true,
Rotate: true,
}

conf2 := ConsoleLogConfig{
Level: level,
}

confString, _ := json.Marshal(&conf1)
confString2, _ := json.Marshal(&conf2)
l := new(Logger)
l.BeeLogger = logs.NewLogger(1000)
l.SetLogger(logs.AdapterFile, bytes.NewBuffer(confString).String())
l.SetLogger(logs.AdapterConsole, bytes.NewBuffer(confString2).String())
l.BeeLogger.SetPrefix("_" + name)
l.BeeLogger.EnableFuncCallDepth(true)
l.BeeLogger.SetLogFuncCallDepth(2)
loggers.Store(name, l)
return l
func Get(label string, channelLens ...int64) (l *Logger) {
var filelevel int = LevelWarning
if os.Getenv(envkey) == "dev" {
filelevel = LevelAll
}
l = New(label, channelLens...).SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
return
}

+ 5
- 2
log_test.go View File

@@ -1,12 +1,15 @@
package grlogs

import (
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
"testing"
"time"
)

func TestGetLogger(t *testing.T) {
l := GetLogger("nds")
l := Get("nds", 128).SetAdapter(LevelNone, AdapterElasticSearch)
l.Debug("我正在调试")
l.Critical("出错了")
GetLogger("wifi").Critical("hello wifi")
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
l.Flush()
}

+ 1
- 1
logs/es/es.go View File

@@ -9,7 +9,7 @@ import (
"time"

"github.com/OwnLocal/goes"
"github.com/astaxie/beego/logs"
"golib.gaore.com/GaoreGo/grlogs/logs"
)

// NewES return a LoggerInterface


+ 20
- 0
options.go View File

@@ -0,0 +1,20 @@
package grlogs

const (
_ = iota
LevelNone = iota
LevelCritical = iota
LevelError = iota
LevelWarning = iota
LevelWarn = iota
LevelInfo = iota
LevelDebug = iota
LevelAll = iota
)

const (
AdapterFile = "file"
AdapterConsole = "console"
AdapterSocket = "socket"
AdapterElasticSearch = "es"
)

Loading…
Cancel
Save