diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-01-12 11:29:21 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2016-01-12 11:29:21 -0500 |
commit | 691380a60a0f038e22af4b518df91baefbf25e85 (patch) | |
tree | 302bda63c5c37a7cb6401bb57211ba936c3cd4fb /Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go | |
parent | cf0d275ae44ae0e554c039b34a9b68ddac7a081f (diff) | |
parent | a41c807c700979c4dfb0cdda179e889f022dd80b (diff) | |
download | chat-691380a60a0f038e22af4b518df91baefbf25e85.tar.gz chat-691380a60a0f038e22af4b518df91baefbf25e85.tar.bz2 chat-691380a60a0f038e22af4b518df91baefbf25e85.zip |
Merge pull request #1852 from mattermost/PLT-1608
PLT-1608 Upgrading logging package
Diffstat (limited to 'Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go b/Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go new file mode 100644 index 000000000..1d224a99d --- /dev/null +++ b/Godeps/_workspace/src/github.com/alecthomas/log4go/socklog.go @@ -0,0 +1,57 @@ +// Copyright (C) 2010, Kyle Lemons <kyle@kylelemons.net>. All rights reserved. + +package log4go + +import ( + "encoding/json" + "fmt" + "net" + "os" +) + +// This log writer sends output to a socket +type SocketLogWriter chan *LogRecord + +// This is the SocketLogWriter's output method +func (w SocketLogWriter) LogWrite(rec *LogRecord) { + w <- rec +} + +func (w SocketLogWriter) Close() { + close(w) +} + +func NewSocketLogWriter(proto, hostport string) SocketLogWriter { + sock, err := net.Dial(proto, hostport) + if err != nil { + fmt.Fprintf(os.Stderr, "NewSocketLogWriter(%q): %s\n", hostport, err) + return nil + } + + w := SocketLogWriter(make(chan *LogRecord, LogBufferLength)) + + go func() { + defer func() { + if sock != nil && proto == "tcp" { + sock.Close() + } + }() + + for rec := range w { + // Marshall into JSON + js, err := json.Marshal(rec) + if err != nil { + fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err) + return + } + + _, err = sock.Write(js) + if err != nil { + fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err) + return + } + } + }() + + return w +} |