1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 01:41:29 +00:00
Beego/logs/log.go

647 lines
15 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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.
2015-09-11 15:08:24 +00:00
// Package logs provide a general log interface
2014-08-18 08:41:43 +00: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")
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// more docs http://beego.me/docs/module/logs.md
2013-08-27 15:48:58 +00:00
package logs
import (
"fmt"
2016-03-24 09:37:56 +00:00
"log"
2016-01-12 13:05:06 +00:00
"os"
"path"
"runtime"
2016-01-12 14:54:39 +00:00
"strconv"
2016-03-24 09:37:56 +00:00
"strings"
2013-08-27 15:48:58 +00:00
"sync"
2016-01-23 08:24:58 +00:00
"time"
2013-08-27 15:48:58 +00:00
)
// RFC5424 log message levels.
2013-08-27 15:48:58 +00:00
const (
LevelEmergency = iota
LevelAlert
2013-08-27 15:48:58 +00:00
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
2016-03-24 11:15:14 +00:00
// levelLogLogger is defined to implement log.Logger
// the real log level will be LevelEmergency
const levelLoggerImpl = -1
2016-03-24 10:21:52 +00:00
// Name for adapter with beego official support
2016-03-24 09:37:56 +00:00
const (
AdapterConsole = "console"
AdapterFile = "file"
AdapterMultiFile = "multifile"
2016-09-07 05:33:11 +00:00
AdapterMail = "smtp"
2016-03-24 09:37:56 +00:00
AdapterConn = "conn"
2016-03-24 10:21:52 +00:00
AdapterEs = "es"
2016-09-08 09:21:11 +00:00
AdapterJianLiao = "jianliao"
AdapterSlack = "slack"
2016-12-27 01:58:42 +00:00
AdapterAliLS = "alils"
2016-03-24 09:37:56 +00:00
)
2016-03-24 11:15:14 +00:00
// Legacy log level constants to ensure backwards compatibility.
const (
LevelInfo = LevelInformational
LevelTrace = LevelDebug
LevelWarn = LevelWarning
2013-08-27 15:48:58 +00:00
)
2016-03-24 10:21:52 +00:00
type newLoggerFunc func() Logger
2013-08-27 15:48:58 +00:00
2015-09-11 15:08:24 +00:00
// Logger defines the behavior of a log provider.
type Logger interface {
2013-08-27 15:48:58 +00:00
Init(config string) error
2016-01-23 08:24:58 +00:00
WriteMsg(when time.Time, msg string, level int) error
2013-08-27 15:48:58 +00:00
Destroy()
2013-11-27 09:50:10 +00:00
Flush()
2013-08-27 15:48:58 +00:00
}
2016-03-24 10:21:52 +00:00
var adapters = make(map[string]newLoggerFunc)
var levelPrefix = [LevelDebug + 1]string{"[M] ", "[A] ", "[C] ", "[E] ", "[W] ", "[N] ", "[I] ", "[D] "}
2013-08-27 15:48:58 +00:00
// 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.
2016-03-24 10:21:52 +00:00
func Register(name string, log newLoggerFunc) {
2013-08-27 15:48:58 +00:00
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
}
2013-12-30 15:32:57 +00:00
// BeeLogger is default logger in beego application.
// it can contain several providers and log message into all providers.
2013-08-27 15:48:58 +00:00
type BeeLogger struct {
lock sync.Mutex
level int
2016-03-24 11:15:14 +00:00
init bool
enableFuncCallDepth bool
loggerFuncCallDepth int
asynchronous bool
2016-03-24 11:15:14 +00:00
msgChanLen int64
2016-01-12 14:39:40 +00:00
msgChan chan *logMsg
2016-02-02 09:12:47 +00:00
signalChan chan string
wg sync.WaitGroup
2016-01-12 13:05:06 +00:00
outputs []*nameLogger
}
2016-03-25 07:04:52 +00:00
const defaultAsyncMsgLen = 1e3
2016-01-12 13:05:06 +00:00
type nameLogger struct {
Logger
name string
2013-08-27 15:48:58 +00:00
}
type logMsg struct {
level int
msg string
2016-02-03 08:32:59 +00:00
when time.Time
2013-08-27 15:48:58 +00:00
}
2016-01-12 14:39:40 +00:00
var logMsgPool *sync.Pool
2013-12-30 15:32:57 +00:00
// NewLogger returns a new BeeLogger.
2016-01-12 13:05:06 +00:00
// channelLen means the number of messages in chan(used where asynchronous is true).
2013-12-30 15:32:57 +00:00
// if the buffering chan is full, logger adapters write to file or other way.
2016-03-24 09:37:56 +00:00
func NewLogger(channelLens ...int64) *BeeLogger {
2013-08-27 15:48:58 +00:00
bl := new(BeeLogger)
bl.level = LevelDebug
bl.loggerFuncCallDepth = 2
2016-03-24 09:37:56 +00:00
bl.msgChanLen = append(channelLens, 0)[0]
2016-03-25 07:13:28 +00:00
if bl.msgChanLen <= 0 {
bl.msgChanLen = defaultAsyncMsgLen
2016-03-24 09:37:56 +00:00
}
bl.signalChan = make(chan string, 1)
2016-03-24 11:15:14 +00:00
bl.setLogger(AdapterConsole)
return bl
}
2015-09-11 15:08:24 +00:00
// Async set the log to asynchronous and start the goroutine
2016-03-28 07:18:51 +00:00
func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
2016-03-24 11:15:14 +00:00
bl.lock.Lock()
defer bl.lock.Unlock()
if bl.asynchronous {
return bl
}
bl.asynchronous = true
2016-03-28 07:18:51 +00:00
if len(msgLen) > 0 && msgLen[0] > 0 {
bl.msgChanLen = msgLen[0]
}
2016-03-24 09:37:56 +00:00
bl.msgChan = make(chan *logMsg, bl.msgChanLen)
2016-01-12 14:39:40 +00:00
logMsgPool = &sync.Pool{
New: func() interface{} {
return &logMsg{}
},
}
2016-02-02 09:12:47 +00:00
bl.wg.Add(1)
2014-04-02 15:43:37 +00:00
go bl.startLogger()
2013-08-27 15:48:58 +00:00
return bl
}
2013-12-30 15:32:57 +00:00
// SetLogger provides a given logger adapter into BeeLogger with config string.
// config need to be correct JSON as string: {"interval":360}.
2016-03-24 11:15:14 +00:00
func (bl *BeeLogger) setLogger(adapterName string, configs ...string) error {
2016-03-24 09:37:56 +00:00
config := append(configs, "{}")[0]
2016-03-11 02:12:17 +00:00
for _, l := range bl.outputs {
if l.name == adapterName {
return fmt.Errorf("logs: duplicate adaptername %q (you have set this logger before)", adapterName)
2014-04-02 15:43:37 +00:00
}
2016-03-11 02:12:17 +00:00
}
2016-03-11 05:00:58 +00:00
2016-03-11 02:12:17 +00:00
log, ok := adapters[adapterName]
if !ok {
2016-01-12 13:05:06 +00:00
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
2013-08-27 15:48:58 +00:00
}
2016-03-11 02:12:17 +00:00
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})
return nil
2013-08-27 15:48:58 +00:00
}
2016-03-24 11:15:14 +00:00
// SetLogger provides a given logger adapter into BeeLogger with config string.
// config need to be correct JSON as string: {"interval":360}.
func (bl *BeeLogger) SetLogger(adapterName string, configs ...string) error {
bl.lock.Lock()
defer bl.lock.Unlock()
if !bl.init {
bl.outputs = []*nameLogger{}
bl.init = true
}
return bl.setLogger(adapterName, configs...)
}
2015-09-11 15:08:24 +00:00
// DelLogger remove a logger adapter in BeeLogger.
2016-01-12 13:05:06 +00:00
func (bl *BeeLogger) DelLogger(adapterName string) error {
2013-08-27 15:48:58 +00:00
bl.lock.Lock()
defer bl.lock.Unlock()
2016-01-12 13:05:06 +00:00
outputs := []*nameLogger{}
for _, lg := range bl.outputs {
if lg.name == adapterName {
lg.Destroy()
} else {
outputs = append(outputs, lg)
}
2013-08-27 15:48:58 +00:00
}
2016-01-12 13:05:06 +00:00
if len(outputs) == len(bl.outputs) {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
}
bl.outputs = outputs
return nil
2013-08-27 15:48:58 +00:00
}
2016-01-23 08:24:58 +00:00
func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) {
for _, l := range bl.outputs {
2016-01-23 08:24:58 +00:00
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)
}
}
}
func (bl *BeeLogger) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
// writeMsg will always add a '\n' character
if p[len(p)-1] == '\n' {
p = p[0 : len(p)-1]
}
2016-03-24 11:15:14 +00:00
// set levelLoggerImpl to ensure all log message will be write out
err = bl.writeMsg(levelLoggerImpl, string(p))
if err == nil {
return len(p), err
}
return 0, err
}
2016-03-24 10:21:52 +00:00
func (bl *BeeLogger) writeMsg(logLevel int, msg string, v ...interface{}) error {
2016-03-30 06:31:16 +00:00
if !bl.init {
2016-03-25 07:04:52 +00:00
bl.lock.Lock()
bl.setLogger(AdapterConsole)
bl.lock.Unlock()
}
2016-03-24 12:22:42 +00:00
if len(v) > 0 {
msg = fmt.Sprintf(msg, v...)
}
2016-01-25 12:20:53 +00:00
when := time.Now()
if bl.enableFuncCallDepth {
2014-11-03 08:40:08 +00:00
_, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
2015-06-15 12:53:49 +00:00
if !ok {
file = "???"
line = 0
}
2015-06-15 12:53:49 +00:00
_, filename := path.Split(file)
2017-03-17 17:24:45 +00:00
msg = "[" + filename + ":" + strconv.Itoa(line) + "] " + msg
}
//set level info in front of filename info
if logLevel == levelLoggerImpl {
// set to emergency to ensure all log will be print out correctly
logLevel = LevelEmergency
} else {
msg = levelPrefix[logLevel] + msg
}
if bl.asynchronous {
2016-01-12 14:39:40 +00:00
lm := logMsgPool.Get().(*logMsg)
2016-01-12 14:32:20 +00:00
lm.level = logLevel
lm.msg = msg
2016-01-23 08:24:58 +00:00
lm.when = when
2016-01-12 14:39:40 +00:00
bl.msgChan <- lm
} else {
2016-01-23 08:24:58 +00:00
bl.writeToLoggers(when, msg, logLevel)
}
2013-08-27 15:48:58 +00:00
return nil
}
2015-09-11 15:08:24 +00:00
// SetLevel Set log message level.
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message.
2013-08-27 15:48:58 +00:00
func (bl *BeeLogger) SetLevel(l int) {
bl.level = l
}
2015-09-11 15:08:24 +00:00
// SetLogFuncCallDepth set log funcCallDepth
func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
bl.loggerFuncCallDepth = d
}
2015-09-11 15:08:24 +00:00
// GetLogFuncCallDepth return log funcCallDepth for wrapper
func (bl *BeeLogger) GetLogFuncCallDepth() int {
return bl.loggerFuncCallDepth
}
2015-09-11 15:08:24 +00:00
// EnableFuncCallDepth enable log funcCallDepth
func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
bl.enableFuncCallDepth = b
}
2013-12-30 15:32:57 +00:00
// start logger chan reading.
// when chan is not empty, write logs.
2014-04-02 15:43:37 +00:00
func (bl *BeeLogger) startLogger() {
2016-02-02 09:12:47 +00:00
gameOver := false
2013-08-27 15:48:58 +00:00
for {
select {
2016-01-12 14:39:40 +00:00
case bm := <-bl.msgChan:
2016-01-23 08:24:58 +00:00
bl.writeToLoggers(bm.when, bm.msg, bm.level)
2016-01-12 14:39:40 +00:00
logMsgPool.Put(bm)
case sg := <-bl.signalChan:
// Now should only send "flush" or "close" to bl.signalChan
2016-02-02 09:12:47 +00:00
bl.flush()
if sg == "close" {
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
2016-02-02 09:12:47 +00:00
gameOver = true
}
bl.wg.Done()
2013-08-27 15:48:58 +00:00
}
2016-02-02 09:12:47 +00:00
if gameOver {
break
2013-08-27 15:48:58 +00:00
}
}
}
2015-09-11 15:08:24 +00:00
// Emergency Log EMERGENCY level message.
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
if LevelEmergency > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelEmergency, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Alert Log ALERT level message.
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
if LevelAlert > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelAlert, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Critical Log CRITICAL level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
if LevelCritical > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelCritical, format, v...)
}
2015-09-11 15:08:24 +00:00
// Error Log ERROR level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
if LevelError > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelError, format, v...)
}
2015-09-11 15:08:24 +00:00
// Warning Log WARNING level message.
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
2016-10-09 07:19:21 +00:00
if LevelWarn > bl.level {
return
}
bl.writeMsg(LevelWarn, format, v...)
}
2015-09-11 15:08:24 +00:00
// Notice Log NOTICE level message.
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
if LevelNotice > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelNotice, format, v...)
}
2015-09-11 15:08:24 +00:00
// Informational Log INFORMATIONAL level message.
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
2016-10-09 07:19:21 +00:00
if LevelInfo > bl.level {
return
}
bl.writeMsg(LevelInfo, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Debug Log DEBUG level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
if LevelDebug > bl.level {
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelDebug, format, v...)
}
2015-09-11 15:08:24 +00:00
// Warn Log WARN level message.
2015-06-15 12:22:05 +00:00
// compatibility alias for Warning()
2013-08-27 15:48:58 +00:00
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
2016-03-24 11:15:14 +00:00
if LevelWarn > bl.level {
2015-06-15 12:20:37 +00:00
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelWarn, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Info Log INFO level message.
2015-06-15 12:22:05 +00:00
// compatibility alias for Informational()
func (bl *BeeLogger) Info(format string, v ...interface{}) {
2016-03-24 11:15:14 +00:00
if LevelInfo > bl.level {
2015-06-15 12:20:37 +00:00
return
}
2016-03-24 11:15:14 +00:00
bl.writeMsg(LevelInfo, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Trace Log TRACE level message.
2015-06-15 12:22:05 +00:00
// compatibility alias for Debug()
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
2016-10-09 07:19:21 +00:00
if LevelDebug > bl.level {
return
}
bl.writeMsg(LevelDebug, format, v...)
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Flush flush all chan data.
2013-11-27 09:50:10 +00:00
func (bl *BeeLogger) Flush() {
2016-02-02 09:12:47 +00:00
if bl.asynchronous {
bl.signalChan <- "flush"
bl.wg.Wait()
bl.wg.Add(1)
return
2013-11-27 09:50:10 +00:00
}
2016-02-02 09:12:47 +00:00
bl.flush()
2013-11-27 09:50:10 +00:00
}
2015-09-11 15:08:24 +00:00
// Close close logger, flush all chan data and destroy all adapters in BeeLogger.
2013-08-27 15:48:58 +00:00
func (bl *BeeLogger) Close() {
2016-02-02 09:12:47 +00:00
if bl.asynchronous {
bl.signalChan <- "close"
bl.wg.Wait()
2016-03-24 09:37:56 +00:00
close(bl.msgChan)
2016-02-02 09:12:47 +00:00
} else {
bl.flush()
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
}
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-03-24 09:37:56 +00:00
if bl.asynchronous {
for {
if len(bl.msgChan) > 0 {
bm := <-bl.msgChan
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
continue
}
break
2013-11-27 09:50:10 +00:00
}
}
2013-08-27 15:48:58 +00:00
for _, l := range bl.outputs {
2013-11-27 09:50:10 +00:00
l.Flush()
2013-08-27 15:48:58 +00:00
}
}
2016-03-24 09:37:56 +00:00
2016-03-25 07:04:52 +00:00
// beeLogger references the used application logger.
2017-04-30 14:41:23 +00:00
var beeLogger = NewLogger()
2016-03-24 09:37:56 +00:00
2017-04-30 14:41:23 +00:00
// GetBeeLogger returns the default BeeLogger
2016-03-24 09:37:56 +00:00
func GetBeeLogger() *BeeLogger {
return beeLogger
}
var beeLoggerMap = struct {
sync.RWMutex
logs map[string]*log.Logger
}{
logs: map[string]*log.Logger{},
}
// GetLogger returns the default BeeLogger
func GetLogger(prefixes ...string) *log.Logger {
prefix := append(prefixes, "")[0]
if prefix != "" {
2016-03-24 09:49:39 +00:00
prefix = fmt.Sprintf(`[%s] `, strings.ToUpper(prefix))
2016-03-24 09:37:56 +00:00
}
beeLoggerMap.RLock()
l, ok := beeLoggerMap.logs[prefix]
if ok {
beeLoggerMap.RUnlock()
return l
}
beeLoggerMap.RUnlock()
beeLoggerMap.Lock()
defer beeLoggerMap.Unlock()
l, ok = beeLoggerMap.logs[prefix]
if !ok {
l = log.New(beeLogger, prefix, 0)
beeLoggerMap.logs[prefix] = l
}
return l
}
// Reset will remove all the adapter
func Reset() {
beeLogger.Reset()
}
2017-04-30 14:41:23 +00:00
// Async set the beelogger with Async mode and hold msglen messages
2016-03-28 07:18:51 +00:00
func Async(msgLen ...int64) *BeeLogger {
return beeLogger.Async(msgLen...)
2016-03-24 11:15:14 +00:00
}
2016-03-24 09:37:56 +00:00
// SetLevel sets the global log level used by the simple logger.
func SetLevel(l int) {
beeLogger.SetLevel(l)
}
2016-03-28 07:18:51 +00:00
// EnableFuncCallDepth enable log funcCallDepth
func EnableFuncCallDepth(b bool) {
beeLogger.enableFuncCallDepth = b
}
2016-08-23 15:48:47 +00:00
// SetLogFuncCall set the CallDepth, default is 4
2016-03-24 09:37:56 +00:00
func SetLogFuncCall(b bool) {
beeLogger.EnableFuncCallDepth(b)
2016-08-23 15:48:47 +00:00
beeLogger.SetLogFuncCallDepth(4)
2016-03-24 09:37:56 +00:00
}
2016-03-28 07:18:51 +00:00
// SetLogFuncCallDepth set log funcCallDepth
func SetLogFuncCallDepth(d int) {
beeLogger.loggerFuncCallDepth = d
}
2016-03-24 09:37:56 +00:00
// SetLogger sets a new logger.
2016-03-24 09:46:52 +00:00
func SetLogger(adapter string, config ...string) error {
2017-03-17 17:24:45 +00:00
return beeLogger.SetLogger(adapter, config...)
2016-03-24 09:37:56 +00:00
}
// Emergency logs a message at emergency level.
func Emergency(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Emergency(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Alert logs a message at alert level.
func Alert(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Alert(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Critical logs a message at critical level.
func Critical(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Critical(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Error logs a message at error level.
func Error(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Error(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Warning logs a message at warning level.
func Warning(f interface{}, v ...interface{}) {
2016-03-24 11:15:14 +00:00
beeLogger.Warn(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Warn compatibility alias for Warning()
func Warn(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Warn(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Notice logs a message at notice level.
func Notice(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Notice(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Informational logs a message at info level.
func Informational(f interface{}, v ...interface{}) {
2016-03-24 11:15:14 +00:00
beeLogger.Info(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Info compatibility alias for Warning()
func Info(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Info(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Debug logs a message at debug level.
func Debug(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Debug(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
// Trace logs a message at trace level.
// compatibility alias for Warning()
func Trace(f interface{}, v ...interface{}) {
2016-03-24 09:43:16 +00:00
beeLogger.Trace(formatLog(f, v...))
2016-03-24 09:37:56 +00:00
}
2016-03-24 09:43:16 +00:00
func formatLog(f interface{}, v ...interface{}) string {
2016-03-24 09:37:56 +00:00
var msg string
switch f.(type) {
case string:
msg = f.(string)
2016-03-24 12:27:00 +00:00
if len(v) == 0 {
return msg
}
2016-03-24 09:37:56 +00:00
if strings.Contains(msg, "%") && !strings.Contains(msg, "%%") {
//format string
} else {
//do not contain format char
msg += strings.Repeat(" %v", len(v))
}
default:
2016-03-24 12:27:00 +00:00
msg = fmt.Sprint(f)
if len(v) == 0 {
return msg
}
msg += strings.Repeat(" %v", len(v))
2016-03-24 09:37:56 +00:00
}
return fmt.Sprintf(msg, v...)
}