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