mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 20:50:54 +00:00
use array not map
This commit is contained in:
parent
baa2e9d64a
commit
482b7a62bd
46
logs/log.go
46
logs/log.go
@ -35,6 +35,7 @@ package logs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
@ -95,7 +96,12 @@ type BeeLogger struct {
|
|||||||
loggerFuncCallDepth int
|
loggerFuncCallDepth int
|
||||||
asynchronous bool
|
asynchronous bool
|
||||||
msg chan *logMsg
|
msg chan *logMsg
|
||||||
outputs map[string]Logger
|
outputs []*nameLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
type nameLogger struct {
|
||||||
|
Logger
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type logMsg struct {
|
type logMsg struct {
|
||||||
@ -104,14 +110,13 @@ type logMsg struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewLogger returns a new BeeLogger.
|
// NewLogger returns a new BeeLogger.
|
||||||
// channellen means the number of messages in chan.
|
// channelLen means the number of messages in chan(used where asynchronous is true).
|
||||||
// if the buffering chan is full, logger adapters write to file or other way.
|
// if the buffering chan is full, logger adapters write to file or other way.
|
||||||
func NewLogger(channellen int64) *BeeLogger {
|
func NewLogger(channelLen int64) *BeeLogger {
|
||||||
bl := new(BeeLogger)
|
bl := new(BeeLogger)
|
||||||
bl.level = LevelDebug
|
bl.level = LevelDebug
|
||||||
bl.loggerFuncCallDepth = 2
|
bl.loggerFuncCallDepth = 2
|
||||||
bl.msg = make(chan *logMsg, channellen)
|
bl.msg = make(chan *logMsg, channelLen)
|
||||||
bl.outputs = make(map[string]Logger)
|
|
||||||
return bl
|
return bl
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,33 +129,40 @@ func (bl *BeeLogger) Async() *BeeLogger {
|
|||||||
|
|
||||||
// SetLogger provides a given logger adapter into BeeLogger with config string.
|
// SetLogger provides a given logger adapter into BeeLogger with config string.
|
||||||
// config need to be correct JSON as string: {"interval":360}.
|
// config need to be correct JSON as string: {"interval":360}.
|
||||||
func (bl *BeeLogger) SetLogger(adaptername string, config string) error {
|
func (bl *BeeLogger) SetLogger(adapterName string, config string) error {
|
||||||
bl.lock.Lock()
|
bl.lock.Lock()
|
||||||
defer bl.lock.Unlock()
|
defer bl.lock.Unlock()
|
||||||
if log, ok := adapters[adaptername]; ok {
|
if log, ok := adapters[adapterName]; ok {
|
||||||
lg := log()
|
lg := log()
|
||||||
err := lg.Init(config)
|
err := lg.Init(config)
|
||||||
bl.outputs[adaptername] = lg
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("logs.BeeLogger.SetLogger: " + err.Error())
|
fmt.Fprintln(os.Stderr, "logs.BeeLogger.SetLogger: "+err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
bl.outputs = append(bl.outputs, &nameLogger{name: adapterName, Logger: lg})
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername)
|
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelLogger remove a logger adapter in BeeLogger.
|
// DelLogger remove a logger adapter in BeeLogger.
|
||||||
func (bl *BeeLogger) DelLogger(adaptername string) error {
|
func (bl *BeeLogger) DelLogger(adapterName string) error {
|
||||||
bl.lock.Lock()
|
bl.lock.Lock()
|
||||||
defer bl.lock.Unlock()
|
defer bl.lock.Unlock()
|
||||||
if lg, ok := bl.outputs[adaptername]; ok {
|
outputs := []*nameLogger{}
|
||||||
|
for _, lg := range bl.outputs {
|
||||||
|
if lg.name == adapterName {
|
||||||
lg.Destroy()
|
lg.Destroy()
|
||||||
delete(bl.outputs, adaptername)
|
} else {
|
||||||
return nil
|
outputs = append(outputs, lg)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername)
|
}
|
||||||
|
if len(outputs) == len(bl.outputs) {
|
||||||
|
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
|
||||||
|
}
|
||||||
|
bl.outputs = outputs
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
|
func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
|
||||||
@ -170,10 +182,10 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
|
|||||||
if bl.asynchronous {
|
if bl.asynchronous {
|
||||||
bl.msg <- lm
|
bl.msg <- lm
|
||||||
} else {
|
} else {
|
||||||
for name, l := range bl.outputs {
|
for _, l := range bl.outputs {
|
||||||
err := l.WriteMsg(lm.msg, lm.level)
|
err := l.WriteMsg(lm.msg, lm.level)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("unable to WriteMsg to adapter:", name, err)
|
fmt.Println("unable to WriteMsg to adapter:", l.name, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user