frp/vendor/github.com/astaxie/beego/logs/log.go

423 lines
9.8 KiB
Go
Raw Normal View History

2016-01-27 14:24:36 +01:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2016-08-11 10:32:05 +02:00
// Package logs provide a general log interface
2016-01-27 14:24:36 +01:00
// Usage:
//
// import "github.com/astaxie/beego/logs"
//
// log := NewLogger(10000)
// log.SetLogger("console", "")
//
// > the first params stand for how many channel
//
// Use it like this:
//
// log.Trace("trace")
// log.Info("info")
// log.Warn("warning")
// log.Debug("debug")
// log.Critical("critical")
//
// more docs http://beego.me/docs/module/logs.md
package logs
import (
"fmt"
2016-08-11 10:32:05 +02:00
"os"
2016-01-27 14:24:36 +01:00
"path"
"runtime"
2016-08-11 10:32:05 +02:00
"strconv"
2016-01-27 14:24:36 +01:00
"sync"
2016-08-11 10:32:05 +02:00
"time"
2016-01-27 14:24:36 +01:00
)
// RFC5424 log message levels.
const (
LevelEmergency = iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
// Legacy loglevel constants to ensure backwards compatibility.
//
// Deprecated: will be removed in 1.5.0.
const (
LevelInfo = LevelInformational
LevelTrace = LevelDebug
LevelWarn = LevelWarning
)
2016-08-11 10:32:05 +02:00
type loggerType func() Logger
2016-01-27 14:24:36 +01:00
2016-08-11 10:32:05 +02:00
// Logger defines the behavior of a log provider.
type Logger interface {
2016-01-27 14:24:36 +01:00
Init(config string) error
2016-08-11 10:32:05 +02:00
WriteMsg(when time.Time, msg string, level int) error
2016-01-27 14:24:36 +01:00
Destroy()
Flush()
}
var adapters = make(map[string]loggerType)
// Register makes a log provide available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, log loggerType) {
if log == nil {
panic("logs: Register provide is nil")
}
if _, dup := adapters[name]; dup {
panic("logs: Register called twice for provider " + name)
}
adapters[name] = log
}
// BeeLogger is default logger in beego application.
// it can contain several providers and log message into all providers.
type BeeLogger struct {
lock sync.Mutex
level int
enableFuncCallDepth bool
loggerFuncCallDepth int
asynchronous bool
2016-08-11 10:32:05 +02:00
msgChan chan *logMsg
signalChan chan string
wg sync.WaitGroup
outputs []*nameLogger
}
type nameLogger struct {
Logger
name string
2016-01-27 14:24:36 +01:00
}
type logMsg struct {
level int
msg string
2016-08-11 10:32:05 +02:00
when time.Time
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
var logMsgPool *sync.Pool
2016-01-27 14:24:36 +01:00
// NewLogger returns a new BeeLogger.
2016-08-11 10:32:05 +02:00
// channelLen means the number of messages in chan(used where asynchronous is true).
2016-01-27 14:24:36 +01:00
// if the buffering chan is full, logger adapters write to file or other way.
2016-08-11 10:32:05 +02:00
func NewLogger(channelLen int64) *BeeLogger {
2016-01-27 14:24:36 +01:00
bl := new(BeeLogger)
bl.level = LevelDebug
bl.loggerFuncCallDepth = 2
2016-08-11 10:32:05 +02:00
bl.msgChan = make(chan *logMsg, channelLen)
bl.signalChan = make(chan string, 1)
2016-01-27 14:24:36 +01:00
return bl
}
2016-08-11 10:32:05 +02:00
// Async set the log to asynchronous and start the goroutine
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Async() *BeeLogger {
bl.asynchronous = true
2016-08-11 10:32:05 +02:00
logMsgPool = &sync.Pool{
New: func() interface{} {
return &logMsg{}
},
}
bl.wg.Add(1)
2016-01-27 14:24:36 +01:00
go bl.startLogger()
return bl
}
// SetLogger provides a given logger adapter into BeeLogger with config string.
// config need to be correct JSON as string: {"interval":360}.
2016-08-11 10:32:05 +02:00
func (bl *BeeLogger) SetLogger(adapterName string, config string) error {
2016-01-27 14:24:36 +01:00
bl.lock.Lock()
defer bl.lock.Unlock()
2016-08-11 10:32:05 +02:00
for _, l := range bl.outputs {
if l.name == adapterName {
return fmt.Errorf("logs: duplicate adaptername %q (you have set this logger before)", adapterName)
2016-01-27 14:24:36 +01:00
}
}
2016-08-11 10:32:05 +02:00
log, ok := adapters[adapterName]
if !ok {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
}
lg := log()
err := lg.Init(config)
if err != nil {
fmt.Fprintln(os.Stderr, "logs.BeeLogger.SetLogger: "+err.Error())
return err
}
bl.outputs = append(bl.outputs, &nameLogger{name: adapterName, Logger: lg})
2016-01-27 14:24:36 +01:00
return nil
}
2016-08-11 10:32:05 +02:00
// DelLogger remove a logger adapter in BeeLogger.
func (bl *BeeLogger) DelLogger(adapterName string) error {
2016-01-27 14:24:36 +01:00
bl.lock.Lock()
defer bl.lock.Unlock()
2016-08-11 10:32:05 +02:00
outputs := []*nameLogger{}
for _, lg := range bl.outputs {
if lg.name == adapterName {
lg.Destroy()
} else {
outputs = append(outputs, lg)
}
}
if len(outputs) == len(bl.outputs) {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
}
bl.outputs = outputs
return nil
}
func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) {
for _, l := range bl.outputs {
err := l.WriteMsg(when, msg, level)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)
}
2016-01-27 14:24:36 +01:00
}
}
2016-08-11 10:32:05 +02:00
func (bl *BeeLogger) writeMsg(logLevel int, msg string) error {
when := time.Now()
2016-01-27 14:24:36 +01:00
if bl.enableFuncCallDepth {
_, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
if !ok {
file = "???"
line = 0
}
_, filename := path.Split(file)
2016-08-11 10:32:05 +02:00
msg = "[" + filename + ":" + strconv.FormatInt(int64(line), 10) + "]" + msg
2016-01-27 14:24:36 +01:00
}
if bl.asynchronous {
2016-08-11 10:32:05 +02:00
lm := logMsgPool.Get().(*logMsg)
lm.level = logLevel
lm.msg = msg
lm.when = when
bl.msgChan <- lm
2016-01-27 14:24:36 +01:00
} else {
2016-08-11 10:32:05 +02:00
bl.writeToLoggers(when, msg, logLevel)
2016-01-27 14:24:36 +01:00
}
return nil
}
2016-08-11 10:32:05 +02:00
// SetLevel Set log message level.
2016-01-27 14:24:36 +01:00
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message.
func (bl *BeeLogger) SetLevel(l int) {
bl.level = l
}
2016-08-11 10:32:05 +02:00
// SetLogFuncCallDepth set log funcCallDepth
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
bl.loggerFuncCallDepth = d
}
2016-08-11 10:32:05 +02:00
// GetLogFuncCallDepth return log funcCallDepth for wrapper
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) GetLogFuncCallDepth() int {
return bl.loggerFuncCallDepth
}
2016-08-11 10:32:05 +02:00
// EnableFuncCallDepth enable log funcCallDepth
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
bl.enableFuncCallDepth = b
}
// start logger chan reading.
// when chan is not empty, write logs.
func (bl *BeeLogger) startLogger() {
2016-08-11 10:32:05 +02:00
gameOver := false
2016-01-27 14:24:36 +01:00
for {
select {
2016-08-11 10:32:05 +02:00
case bm := <-bl.msgChan:
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
case sg := <-bl.signalChan:
// Now should only send "flush" or "close" to bl.signalChan
bl.flush()
if sg == "close" {
for _, l := range bl.outputs {
l.Destroy()
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
bl.outputs = nil
gameOver = true
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
bl.wg.Done()
}
if gameOver {
break
2016-01-27 14:24:36 +01:00
}
}
}
2016-08-11 10:32:05 +02:00
// Emergency Log EMERGENCY level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
if LevelEmergency > bl.level {
return
}
msg := fmt.Sprintf("[M] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelEmergency, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Alert Log ALERT level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
if LevelAlert > bl.level {
return
}
msg := fmt.Sprintf("[A] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelAlert, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Critical Log CRITICAL level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
if LevelCritical > bl.level {
return
}
msg := fmt.Sprintf("[C] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelCritical, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Error Log ERROR level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Error(format string, v ...interface{}) {
if LevelError > bl.level {
return
}
msg := fmt.Sprintf("[E] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelError, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Warning Log WARNING level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
if LevelWarning > bl.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelWarning, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Notice Log NOTICE level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
if LevelNotice > bl.level {
return
}
msg := fmt.Sprintf("[N] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelNotice, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Informational Log INFORMATIONAL level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
if LevelInformational > bl.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelInformational, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Debug Log DEBUG level message.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
if LevelDebug > bl.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelDebug, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Warn Log WARN level message.
2016-01-27 14:24:36 +01:00
// compatibility alias for Warning()
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
if LevelWarning > bl.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelWarning, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Info Log INFO level message.
2016-01-27 14:24:36 +01:00
// compatibility alias for Informational()
func (bl *BeeLogger) Info(format string, v ...interface{}) {
if LevelInformational > bl.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelInformational, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Trace Log TRACE level message.
2016-01-27 14:24:36 +01:00
// compatibility alias for Debug()
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
if LevelDebug > bl.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
2016-08-11 10:32:05 +02:00
bl.writeMsg(LevelDebug, msg)
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Flush flush all chan data.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Flush() {
2016-08-11 10:32:05 +02:00
if bl.asynchronous {
bl.signalChan <- "flush"
bl.wg.Wait()
bl.wg.Add(1)
return
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
bl.flush()
2016-01-27 14:24:36 +01:00
}
2016-08-11 10:32:05 +02:00
// Close close logger, flush all chan data and destroy all adapters in BeeLogger.
2016-01-27 14:24:36 +01:00
func (bl *BeeLogger) Close() {
2016-08-11 10:32:05 +02:00
if bl.asynchronous {
bl.signalChan <- "close"
bl.wg.Wait()
} else {
bl.flush()
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
}
close(bl.msgChan)
close(bl.signalChan)
}
// Reset close all outputs, and set bl.outputs to nil
func (bl *BeeLogger) Reset() {
bl.Flush()
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
}
func (bl *BeeLogger) flush() {
2016-01-27 14:24:36 +01:00
for {
2016-08-11 10:32:05 +02:00
if len(bl.msgChan) > 0 {
bm := <-bl.msgChan
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
2016-01-27 14:24:36 +01:00
continue
}
break
}
for _, l := range bl.outputs {
l.Flush()
}
}