Skip to content

Instantly share code, notes, and snippets.

@yoppi
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoppi/117ff0dcf93a59dc3945 to your computer and use it in GitHub Desktop.
Save yoppi/117ff0dcf93a59dc3945 to your computer and use it in GitHub Desktop.
こう書いても良さそう。けど、logパッケージと被る。
type AdLogger struct {
mu sync.Mutex
Path string
Out *os.File
buf []byte
}
func (l *AdLogger) Write(log logs.Log) {
l.mu.Lock()
defer l.mu.Unlock()
ret, err := json.Marshal(log)
if err != nil {
return
}
l.buf = l.buf[:0]
l.buf = append(l.buf, ret...)
if len(ret) > 0 && ret[len(ret)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
_, err := l.Out.Write(l.buf)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment