diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-02-23 22:49:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-23 22:49:05 +0800 |
commit | 357732a8404c9fe4d02f041d20a0d05381a3e6d1 (patch) | |
tree | edaf8a63eb7f7434cd9b9cbd764b3c4c3d76191e /log/syslog.go | |
parent | 29fac7de448c85049a97cbec3dc0819122bd2cb0 (diff) | |
parent | f89dd627760b43bd405cb3db1e5efdb100835db5 (diff) | |
download | dexon-357732a8404c9fe4d02f041d20a0d05381a3e6d1.tar.gz dexon-357732a8404c9fe4d02f041d20a0d05381a3e6d1.tar.zst dexon-357732a8404c9fe4d02f041d20a0d05381a3e6d1.zip |
Merge pull request #3696 from karalabe/contextual-logger
Contextual logger
Diffstat (limited to 'log/syslog.go')
-rw-r--r-- | log/syslog.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/log/syslog.go b/log/syslog.go new file mode 100644 index 000000000..71a17b30b --- /dev/null +++ b/log/syslog.go @@ -0,0 +1,57 @@ +// +build !windows,!plan9 + +package log + +import ( + "log/syslog" + "strings" +) + +// SyslogHandler opens a connection to the system syslog daemon by calling +// syslog.New and writes all records to it. +func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error) { + wr, err := syslog.New(priority, tag) + return sharedSyslog(fmtr, wr, err) +} + +// SyslogNetHandler opens a connection to a log daemon over the network and writes +// all log records to it. +func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error) { + wr, err := syslog.Dial(net, addr, priority, tag) + return sharedSyslog(fmtr, wr, err) +} + +func sharedSyslog(fmtr Format, sysWr *syslog.Writer, err error) (Handler, error) { + if err != nil { + return nil, err + } + h := FuncHandler(func(r *Record) error { + var syslogFn = sysWr.Info + switch r.Lvl { + case LvlCrit: + syslogFn = sysWr.Crit + case LvlError: + syslogFn = sysWr.Err + case LvlWarn: + syslogFn = sysWr.Warning + case LvlInfo: + syslogFn = sysWr.Info + case LvlDebug: + syslogFn = sysWr.Debug + case LvlTrace: + syslogFn = func(m string) error { return nil } // There's no syslog level for trace + } + + s := strings.TrimSpace(string(fmtr.Format(r))) + return syslogFn(s) + }) + return LazyHandler(&closingHandler{sysWr, h}), nil +} + +func (m muster) SyslogHandler(priority syslog.Priority, tag string, fmtr Format) Handler { + return must(SyslogHandler(priority, tag, fmtr)) +} + +func (m muster) SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) Handler { + return must(SyslogNetHandler(net, addr, priority, tag, fmtr)) +} |