logs:default support fileline
Showing
5 changed files
with
9 additions
and
38 deletions
| ... | @@ -38,11 +38,6 @@ func SetLevel(l int) { | ... | @@ -38,11 +38,6 @@ func SetLevel(l int) { |
| 38 | BeeLogger.SetLevel(l) | 38 | BeeLogger.SetLevel(l) |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | func SetLogFuncCall(b bool) { | ||
| 42 | BeeLogger.EnableFuncCallDepth(b) | ||
| 43 | BeeLogger.SetLogFuncCallDepth(3) | ||
| 44 | } | ||
| 45 | |||
| 46 | // logger references the used application logger. | 41 | // logger references the used application logger. |
| 47 | var BeeLogger *logs.BeeLogger | 42 | var BeeLogger *logs.BeeLogger |
| 48 | 43 | ... | ... |
| ... | @@ -99,7 +99,7 @@ func (c *ConnWriter) connect() error { | ... | @@ -99,7 +99,7 @@ func (c *ConnWriter) connect() error { |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | c.innerWriter = conn | 101 | c.innerWriter = conn |
| 102 | c.lg = log.New(conn, "", log.Ldate|log.Ltime) | 102 | c.lg = log.New(conn, "", log.Ldate|log.Ltime|log.Lshortfile) |
| 103 | return nil | 103 | return nil |
| 104 | } | 104 | } |
| 105 | 105 | ... | ... |
| ... | @@ -51,7 +51,7 @@ type ConsoleWriter struct { | ... | @@ -51,7 +51,7 @@ type ConsoleWriter struct { |
| 51 | // create ConsoleWriter returning as LoggerInterface. | 51 | // create ConsoleWriter returning as LoggerInterface. |
| 52 | func NewConsole() LoggerInterface { | 52 | func NewConsole() LoggerInterface { |
| 53 | cw := new(ConsoleWriter) | 53 | cw := new(ConsoleWriter) |
| 54 | cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime) | 54 | cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) |
| 55 | cw.Level = LevelDebug | 55 | cw.Level = LevelDebug |
| 56 | return cw | 56 | return cw |
| 57 | } | 57 | } | ... | ... |
| ... | @@ -89,7 +89,7 @@ func NewFileWriter() LoggerInterface { | ... | @@ -89,7 +89,7 @@ func NewFileWriter() LoggerInterface { |
| 89 | // use MuxWriter instead direct use os.File for lock write when rotate | 89 | // use MuxWriter instead direct use os.File for lock write when rotate |
| 90 | w.mw = new(MuxWriter) | 90 | w.mw = new(MuxWriter) |
| 91 | // set MuxWriter as Logger's io.Writer | 91 | // set MuxWriter as Logger's io.Writer |
| 92 | w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime) | 92 | w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime|log.Lshortfile) |
| 93 | return w | 93 | return w |
| 94 | } | 94 | } |
| 95 | 95 | ... | ... |
| ... | @@ -34,8 +34,7 @@ package logs | ... | @@ -34,8 +34,7 @@ package logs |
| 34 | 34 | ||
| 35 | import ( | 35 | import ( |
| 36 | "fmt" | 36 | "fmt" |
| 37 | "path" | 37 | |
| 38 | "runtime" | ||
| 39 | "sync" | 38 | "sync" |
| 40 | ) | 39 | ) |
| 41 | 40 | ||
| ... | @@ -88,12 +87,10 @@ func Register(name string, log loggerType) { | ... | @@ -88,12 +87,10 @@ func Register(name string, log loggerType) { |
| 88 | // BeeLogger is default logger in beego application. | 87 | // BeeLogger is default logger in beego application. |
| 89 | // it can contain several providers and log message into all providers. | 88 | // it can contain several providers and log message into all providers. |
| 90 | type BeeLogger struct { | 89 | type BeeLogger struct { |
| 91 | lock sync.Mutex | 90 | lock sync.Mutex |
| 92 | level int | 91 | level int |
| 93 | enableFuncCallDepth bool | 92 | msg chan *logMsg |
| 94 | loggerFuncCallDepth int | 93 | outputs map[string]LoggerInterface |
| 95 | msg chan *logMsg | ||
| 96 | outputs map[string]LoggerInterface | ||
| 97 | } | 94 | } |
| 98 | 95 | ||
| 99 | type logMsg struct { | 96 | type logMsg struct { |
| ... | @@ -107,7 +104,6 @@ type logMsg struct { | ... | @@ -107,7 +104,6 @@ type logMsg struct { |
| 107 | func NewLogger(channellen int64) *BeeLogger { | 104 | func NewLogger(channellen int64) *BeeLogger { |
| 108 | bl := new(BeeLogger) | 105 | bl := new(BeeLogger) |
| 109 | bl.level = LevelDebug | 106 | bl.level = LevelDebug |
| 110 | bl.loggerFuncCallDepth = 2 | ||
| 111 | bl.msg = make(chan *logMsg, channellen) | 107 | bl.msg = make(chan *logMsg, channellen) |
| 112 | bl.outputs = make(map[string]LoggerInterface) | 108 | bl.outputs = make(map[string]LoggerInterface) |
| 113 | //bl.SetLogger("console", "") // default output to console | 109 | //bl.SetLogger("console", "") // default output to console |
| ... | @@ -153,17 +149,7 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { | ... | @@ -153,17 +149,7 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { |
| 153 | } | 149 | } |
| 154 | lm := new(logMsg) | 150 | lm := new(logMsg) |
| 155 | lm.level = loglevel | 151 | lm.level = loglevel |
| 156 | if bl.enableFuncCallDepth { | 152 | lm.msg = msg |
| 157 | _, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth) | ||
| 158 | if ok { | ||
| 159 | _, filename := path.Split(file) | ||
| 160 | lm.msg = fmt.Sprintf("[%s:%d] %s", filename, line, msg) | ||
| 161 | } else { | ||
| 162 | lm.msg = msg | ||
| 163 | } | ||
| 164 | } else { | ||
| 165 | lm.msg = msg | ||
| 166 | } | ||
| 167 | bl.msg <- lm | 153 | bl.msg <- lm |
| 168 | return nil | 154 | return nil |
| 169 | } | 155 | } |
| ... | @@ -176,16 +162,6 @@ func (bl *BeeLogger) SetLevel(l int) { | ... | @@ -176,16 +162,6 @@ func (bl *BeeLogger) SetLevel(l int) { |
| 176 | bl.level = l | 162 | bl.level = l |
| 177 | } | 163 | } |
| 178 | 164 | ||
| 179 | // set log funcCallDepth | ||
| 180 | func (bl *BeeLogger) SetLogFuncCallDepth(d int) { | ||
| 181 | bl.loggerFuncCallDepth = d | ||
| 182 | } | ||
| 183 | |||
| 184 | // enable log funcCallDepth | ||
| 185 | func (bl *BeeLogger) EnableFuncCallDepth(b bool) { | ||
| 186 | bl.enableFuncCallDepth = b | ||
| 187 | } | ||
| 188 | |||
| 189 | // start logger chan reading. | 165 | // start logger chan reading. |
| 190 | // when chan is not empty, write logs. | 166 | // when chan is not empty, write logs. |
| 191 | func (bl *BeeLogger) startLogger() { | 167 | func (bl *BeeLogger) startLogger() { | ... | ... |
-
Please register or sign in to post a comment