github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/log/syslog.go (about)

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