1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 20:10:55 +00:00

This makes the logger instance a singleton

This commit is contained in:
Faissal Elamraoui 2016-11-16 12:33:01 +01:00
parent 21fc177551
commit 6b27ef186a
2 changed files with 12 additions and 3 deletions

2
bee.go
View File

@ -90,6 +90,8 @@ var commands = []*Command{
cmdFix, cmdFix,
} }
var logger = GetBeeLogger(os.Stdout)
func main() { func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()

View File

@ -39,7 +39,8 @@ const (
var ( var (
sequenceNo uint64 sequenceNo uint64
logger *BeeLogger instance *BeeLogger
once sync.Once
) )
// BeeLogger logs logging records to the specified io.Writer // BeeLogger logs logging records to the specified io.Writer
@ -79,9 +80,15 @@ func init() {
MustCheck(err) MustCheck(err)
debugLogRecordTemplate, err = template.New("dbgLogRecordTemplate").Funcs(funcs).Parse(debugLogFormat) debugLogRecordTemplate, err = template.New("dbgLogRecordTemplate").Funcs(funcs).Parse(debugLogFormat)
MustCheck(err) MustCheck(err)
}
// Initialize the logger instance with a NewColorWriter output // GetBeeLogger initializes the logger instance with a NewColorWriter output
logger = &BeeLogger{output: NewColorWriter(os.Stdout)} // and returns a singleton
func GetBeeLogger(w io.Writer) *BeeLogger {
once.Do(func() {
instance = &BeeLogger{output: NewColorWriter(w)}
})
return instance
} }
// SetOutput sets the logger output destination // SetOutput sets the logger output destination