log default use synchro, and support async
Showing
1 changed file
with
17 additions
and
2 deletions
| ... | @@ -92,6 +92,7 @@ type BeeLogger struct { | ... | @@ -92,6 +92,7 @@ type BeeLogger struct { |
| 92 | level int | 92 | level int |
| 93 | enableFuncCallDepth bool | 93 | enableFuncCallDepth bool |
| 94 | loggerFuncCallDepth int | 94 | loggerFuncCallDepth int |
| 95 | asynchronous bool | ||
| 95 | msg chan *logMsg | 96 | msg chan *logMsg |
| 96 | outputs map[string]LoggerInterface | 97 | outputs map[string]LoggerInterface |
| 97 | } | 98 | } |
| ... | @@ -110,7 +111,11 @@ func NewLogger(channellen int64) *BeeLogger { | ... | @@ -110,7 +111,11 @@ func NewLogger(channellen int64) *BeeLogger { |
| 110 | bl.loggerFuncCallDepth = 2 | 111 | bl.loggerFuncCallDepth = 2 |
| 111 | bl.msg = make(chan *logMsg, channellen) | 112 | bl.msg = make(chan *logMsg, channellen) |
| 112 | bl.outputs = make(map[string]LoggerInterface) | 113 | bl.outputs = make(map[string]LoggerInterface) |
| 113 | //bl.SetLogger("console", "") // default output to console | 114 | return bl |
| 115 | } | ||
| 116 | |||
| 117 | func (bl *BeeLogger) Async() *BeeLogger { | ||
| 118 | bl.asynchronous = true | ||
| 114 | go bl.startLogger() | 119 | go bl.startLogger() |
| 115 | return bl | 120 | return bl |
| 116 | } | 121 | } |
| ... | @@ -164,7 +169,17 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { | ... | @@ -164,7 +169,17 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { |
| 164 | } else { | 169 | } else { |
| 165 | lm.msg = msg | 170 | lm.msg = msg |
| 166 | } | 171 | } |
| 167 | bl.msg <- lm | 172 | if bl.asynchronous { |
| 173 | bl.msg <- lm | ||
| 174 | } else { | ||
| 175 | for name, l := range bl.outputs { | ||
| 176 | err := l.WriteMsg(lm.msg, lm.level) | ||
| 177 | if err != nil { | ||
| 178 | fmt.Println("unable to WriteMsg to adapter:", name, err) | ||
| 179 | return err | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 168 | return nil | 183 | return nil |
| 169 | } | 184 | } |
| 170 | 185 | ... | ... |
-
Please register or sign in to post a comment