Last active
August 29, 2015 14:26
-
-
Save yoppi/117ff0dcf93a59dc3945 to your computer and use it in GitHub Desktop.
こう書いても良さそう。けど、logパッケージと被る。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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