diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-02-20 23:39:36 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-02-23 18:00:02 +0800 |
commit | ec7f81f4bc11c6f8203ec1d3055c9a09a244ff43 (patch) | |
tree | 5ec75ddab7749a9d0c47a97d6f638b52e6ebc6bb /log/syslog.go | |
parent | 29fac7de448c85049a97cbec3dc0819122bd2cb0 (diff) | |
download | dexon-ec7f81f4bc11c6f8203ec1d3055c9a09a244ff43.tar.gz dexon-ec7f81f4bc11c6f8203ec1d3055c9a09a244ff43.tar.zst dexon-ec7f81f4bc11c6f8203ec1d3055c9a09a244ff43.zip |
log, vendor: vendor in log15 inline into our codebase
Diffstat (limited to 'log/syslog.go')
-rw-r--r-- | log/syslog.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/log/syslog.go b/log/syslog.go new file mode 100644 index 000000000..4f1097ff9 --- /dev/null +++ b/log/syslog.go @@ -0,0 +1,55 @@ +// +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 + } + + 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)) +} |