github.com/turingchain2020/turingchain@v1.1.21/common/log/log15/syslog.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !windows,!plan9
     6  
     7  package log15
     8  
     9  import (
    10  	"io"
    11  	"log/syslog"
    12  	"strings"
    13  )
    14  
    15  // SyslogHandler opens a connection to the system syslog daemon by calling
    16  // syslog.New and writes all records to it.
    17  func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
    18  	wr, err := syslog.New(priority, tag)
    19  	return sharedSyslog(fmtr, wr, err)
    20  }
    21  
    22  // SyslogNetHandler opens a connection to a log daemon over the network and writes
    23  // all log records to it.
    24  func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
    25  	wr, err := syslog.Dial(net, addr, priority, tag)
    26  	return sharedSyslog(fmtr, wr, err)
    27  }
    28  
    29  func sharedSyslog(fmtr Format, sysWr io.WriteCloser, err error) (Handler, error) {
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	wr := sysWr.(*syslog.Writer)
    34  	h := FuncHandler(int(LvlDebug), func(r *Record) error {
    35  		var syslogFn = wr.Info
    36  		switch r.Lvl {
    37  		case LvlCrit:
    38  			syslogFn = wr.Crit
    39  		case LvlError:
    40  			syslogFn = wr.Err
    41  		case LvlWarn:
    42  			syslogFn = wr.Warning
    43  		case LvlInfo:
    44  			syslogFn = wr.Info
    45  		case LvlDebug:
    46  			syslogFn = wr.Debug
    47  		}
    48  		s := strings.TrimSpace(string(fmtr.Format(r)))
    49  		return syslogFn(s)
    50  	})
    51  	return LazyHandler(&closingHandler{sysWr, h}), nil
    52  }
    53  
    54  func (m muster) SyslogHandler(priority syslog.Priority, tag string, fmtr Format) Handler {
    55  	return must(SyslogHandler(priority, tag, fmtr))
    56  }
    57  
    58  func (m muster) SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) Handler {
    59  	return must(SyslogNetHandler(net, addr, priority, tag, fmtr))
    60  }