3f5fee2d by asta.xie

Logs support file & filenum

1 parent c7f16b5d
...@@ -60,6 +60,7 @@ var ( ...@@ -60,6 +60,7 @@ var (
60 AdminHttpPort int 60 AdminHttpPort int
61 FlashName string // name of the flash variable found in response header and cookie 61 FlashName string // name of the flash variable found in response header and cookie
62 FlashSeperator string // used to seperate flash key:value 62 FlashSeperator string // used to seperate flash key:value
63 EnableLogFuncCallDepth bool // enable the funcCallDeppth
63 ) 64 )
64 65
65 func init() { 66 func init() {
...@@ -133,6 +134,10 @@ func init() { ...@@ -133,6 +134,10 @@ func init() {
133 // init BeeLogger 134 // init BeeLogger
134 BeeLogger = logs.NewLogger(10000) 135 BeeLogger = logs.NewLogger(10000)
135 BeeLogger.SetLogger("console", "") 136 BeeLogger.SetLogger("console", "")
137 if EnableLogFuncCallDepth {
138 BeeLogger.EnableFuncCallDepth(true)
139 BeeLogger.SetLogFuncCallDepth(3)
140 }
136 141
137 err := ParseConfig() 142 err := ParseConfig()
138 if err != nil && !os.IsNotExist(err) { 143 if err != nil && !os.IsNotExist(err) {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
6 6
7 func TestConsole(t *testing.T) { 7 func TestConsole(t *testing.T) {
8 log := NewLogger(10000) 8 log := NewLogger(10000)
9 log.EnableFuncCallDepth(true)
9 log.SetLogger("console", "") 10 log.SetLogger("console", "")
10 log.Trace("trace") 11 log.Trace("trace")
11 log.Info("info") 12 log.Info("info")
...@@ -23,6 +24,7 @@ func TestConsole(t *testing.T) { ...@@ -23,6 +24,7 @@ func TestConsole(t *testing.T) {
23 24
24 func BenchmarkConsole(b *testing.B) { 25 func BenchmarkConsole(b *testing.B) {
25 log := NewLogger(10000) 26 log := NewLogger(10000)
27 log.EnableFuncCallDepth(true)
26 log.SetLogger("console", "") 28 log.SetLogger("console", "")
27 for i := 0; i < b.N; i++ { 29 for i := 0; i < b.N; i++ {
28 log.Trace("trace") 30 log.Trace("trace")
......
...@@ -2,6 +2,8 @@ package logs ...@@ -2,6 +2,8 @@ package logs
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "path"
6 "runtime"
5 "sync" 7 "sync"
6 ) 8 )
7 9
...@@ -43,10 +45,12 @@ func Register(name string, log loggerType) { ...@@ -43,10 +45,12 @@ func Register(name string, log loggerType) {
43 // BeeLogger is default logger in beego application. 45 // BeeLogger is default logger in beego application.
44 // it can contain several providers and log message into all providers. 46 // it can contain several providers and log message into all providers.
45 type BeeLogger struct { 47 type BeeLogger struct {
46 lock sync.Mutex 48 lock sync.Mutex
47 level int 49 level int
48 msg chan *logMsg 50 enableFuncCallDepth bool
49 outputs map[string]LoggerInterface 51 loggerFuncCallDepth int
52 msg chan *logMsg
53 outputs map[string]LoggerInterface
50 } 54 }
51 55
52 type logMsg struct { 56 type logMsg struct {
...@@ -59,6 +63,7 @@ type logMsg struct { ...@@ -59,6 +63,7 @@ type logMsg struct {
59 // if the buffering chan is full, logger adapters write to file or other way. 63 // if the buffering chan is full, logger adapters write to file or other way.
60 func NewLogger(channellen int64) *BeeLogger { 64 func NewLogger(channellen int64) *BeeLogger {
61 bl := new(BeeLogger) 65 bl := new(BeeLogger)
66 bl.loggerFuncCallDepth = 2
62 bl.msg = make(chan *logMsg, channellen) 67 bl.msg = make(chan *logMsg, channellen)
63 bl.outputs = make(map[string]LoggerInterface) 68 bl.outputs = make(map[string]LoggerInterface)
64 //bl.SetLogger("console", "") // default output to console 69 //bl.SetLogger("console", "") // default output to console
...@@ -100,7 +105,17 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { ...@@ -100,7 +105,17 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
100 } 105 }
101 lm := new(logMsg) 106 lm := new(logMsg)
102 lm.level = loglevel 107 lm.level = loglevel
103 lm.msg = msg 108 if bl.enableFuncCallDepth {
109 _, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
110 if ok {
111 _, filename := path.Split(file)
112 lm.msg = fmt.Sprintf("[%s:%d] %s", filename, line, msg)
113 } else {
114 lm.msg = msg
115 }
116 } else {
117 lm.msg = msg
118 }
104 bl.msg <- lm 119 bl.msg <- lm
105 return nil 120 return nil
106 } 121 }
...@@ -111,6 +126,16 @@ func (bl *BeeLogger) SetLevel(l int) { ...@@ -111,6 +126,16 @@ func (bl *BeeLogger) SetLevel(l int) {
111 bl.level = l 126 bl.level = l
112 } 127 }
113 128
129 // set log funcCallDepth
130 func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
131 bl.loggerFuncCallDepth = d
132 }
133
134 // enable log funcCallDepth
135 func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
136 bl.enableFuncCallDepth = b
137 }
138
114 // start logger chan reading. 139 // start logger chan reading.
115 // when chan is full, write logs. 140 // when chan is full, write logs.
116 func (bl *BeeLogger) StartLogger() { 141 func (bl *BeeLogger) StartLogger() {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!