1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 07:01:28 +00:00

Revert "logs:default support fileline"

This reverts commit 1f26852610.
This commit is contained in:
astaxie 2014-10-30 16:57:48 +08:00
parent ecd0a5487e
commit 68c3bdfdd4
5 changed files with 38 additions and 9 deletions

5
log.go
View File

@ -38,6 +38,11 @@ func SetLevel(l int) {
BeeLogger.SetLevel(l) BeeLogger.SetLevel(l)
} }
func SetLogFuncCall(b bool) {
BeeLogger.EnableFuncCallDepth(b)
BeeLogger.SetLogFuncCallDepth(3)
}
// logger references the used application logger. // logger references the used application logger.
var BeeLogger *logs.BeeLogger var BeeLogger *logs.BeeLogger

View File

@ -99,7 +99,7 @@ func (c *ConnWriter) connect() error {
} }
c.innerWriter = conn c.innerWriter = conn
c.lg = log.New(conn, "", log.Ldate|log.Ltime|log.Lshortfile) c.lg = log.New(conn, "", log.Ldate|log.Ltime)
return nil return nil
} }

View File

@ -51,7 +51,7 @@ type ConsoleWriter struct {
// create ConsoleWriter returning as LoggerInterface. // create ConsoleWriter returning as LoggerInterface.
func NewConsole() LoggerInterface { func NewConsole() LoggerInterface {
cw := new(ConsoleWriter) cw := new(ConsoleWriter)
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
cw.Level = LevelDebug cw.Level = LevelDebug
return cw return cw
} }

View File

@ -89,7 +89,7 @@ func NewFileWriter() LoggerInterface {
// use MuxWriter instead direct use os.File for lock write when rotate // use MuxWriter instead direct use os.File for lock write when rotate
w.mw = new(MuxWriter) w.mw = new(MuxWriter)
// set MuxWriter as Logger's io.Writer // set MuxWriter as Logger's io.Writer
w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime|log.Lshortfile) w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime)
return w return w
} }

View File

@ -34,7 +34,8 @@ package logs
import ( import (
"fmt" "fmt"
"path"
"runtime"
"sync" "sync"
) )
@ -89,6 +90,8 @@ func Register(name string, log loggerType) {
type BeeLogger struct { type BeeLogger struct {
lock sync.Mutex lock sync.Mutex
level int level int
enableFuncCallDepth bool
loggerFuncCallDepth int
msg chan *logMsg msg chan *logMsg
outputs map[string]LoggerInterface outputs map[string]LoggerInterface
} }
@ -104,6 +107,7 @@ type logMsg struct {
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.msg = make(chan *logMsg, channellen) bl.msg = make(chan *logMsg, channellen)
bl.outputs = make(map[string]LoggerInterface) bl.outputs = make(map[string]LoggerInterface)
//bl.SetLogger("console", "") // default output to console //bl.SetLogger("console", "") // default output to console
@ -149,7 +153,17 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
} }
lm := new(logMsg) lm := new(logMsg)
lm.level = loglevel lm.level = loglevel
if bl.enableFuncCallDepth {
_, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
if ok {
_, filename := path.Split(file)
lm.msg = fmt.Sprintf("[%s:%d] %s", filename, line, msg)
} else {
lm.msg = msg lm.msg = msg
}
} else {
lm.msg = msg
}
bl.msg <- lm bl.msg <- lm
return nil return nil
} }
@ -162,6 +176,16 @@ func (bl *BeeLogger) SetLevel(l int) {
bl.level = l bl.level = l
} }
// set log funcCallDepth
func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
bl.loggerFuncCallDepth = d
}
// enable log funcCallDepth
func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
bl.enableFuncCallDepth = b
}
// start logger chan reading. // start logger chan reading.
// when chan is not empty, write logs. // when chan is not empty, write logs.
func (bl *BeeLogger) startLogger() { func (bl *BeeLogger) startLogger() {