github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/log/syslog.go (about)

     1  //go:build !windows && !plan9
     2  // +build !windows,!plan9
     3  
     4  package log
     5  
     6  import (
     7  	"log/syslog"
     8  	"strings"
     9  )
    10  
    11  func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
    12  	wr, err := syslog.New(priority, tag)
    13  	return sharedSyslog(fmtr, wr, err)
    14  }
    15  
    16  func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
    17  	wr, err := syslog.Dial(net, addr, priority, tag)
    18  	return sharedSyslog(fmtr, wr, err)
    19  }
    20  
    21  func sharedSyslog(fmtr Format, sysWr *syslog.Writer, err error) (Handler, error) {
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	h := FuncHandler(func(r *Record) error {
    26  		var syslogFn = sysWr.Info
    27  		switch r.Lvl {
    28  		case LvlCrit:
    29  			syslogFn = sysWr.Crit
    30  		case LvlError:
    31  			syslogFn = sysWr.Err
    32  		case LvlWarn:
    33  			syslogFn = sysWr.Warning
    34  		case LvlInfo:
    35  			syslogFn = sysWr.Info
    36  		case LvlDebug:
    37  			syslogFn = sysWr.Debug
    38  		case LvlTrace:
    39  			syslogFn = func(m string) error { return nil }
    40  		}
    41  
    42  		s := strings.TrimSpace(string(fmtr.Format(r)))
    43  		return syslogFn(s)
    44  	})
    45  	return LazyHandler(&closingHandler{sysWr, h}), nil
    46  }
    47  
    48  func (m muster) SyslogHandler(priority syslog.Priority, tag string, fmtr Format) Handler {
    49  	return must(SyslogHandler(priority, tag, fmtr))
    50  }
    51  
    52  func (m muster) SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) Handler {
    53  	return must(SyslogNetHandler(net, addr, priority, tag, fmtr))
    54  }