gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/log/syslog.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 // +build !windows,!plan9,!nacl 18 19 package log 20 21 import ( 22 "log/syslog" 23 "strings" 24 ) 25 26 // SyslogHandler opens a connection to the system syslog daemon by calling 27 // syslog.New and writes all records to it. 28 func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error) { 29 wr, err := syslog.New(priority, tag) 30 return sharedSyslog(fmtr, wr, err) 31 } 32 33 // SyslogNetHandler opens a connection to a log daemon over the network and writes 34 // all log records to it. 35 func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error) { 36 wr, err := syslog.Dial(net, addr, priority, tag) 37 return sharedSyslog(fmtr, wr, err) 38 } 39 40 func sharedSyslog(fmtr Format, sysWr *syslog.Writer, err error) (Handler, error) { 41 if err != nil { 42 return nil, err 43 } 44 h := FuncHandler(func(r *Record) error { 45 var syslogFn = sysWr.Info 46 switch r.Lvl { 47 case LvlCrit: 48 syslogFn = sysWr.Crit 49 case LvlError: 50 syslogFn = sysWr.Err 51 case LvlWarn: 52 syslogFn = sysWr.Warning 53 case LvlInfo: 54 syslogFn = sysWr.Info 55 case LvlDebug: 56 syslogFn = sysWr.Debug 57 case LvlTrace: 58 syslogFn = func(m string) error { return nil } // There's no syslog level for trace 59 } 60 61 s := strings.TrimSpace(string(fmtr.Format(r))) 62 return syslogFn(s) 63 }) 64 return LazyHandler(&closingHandler{sysWr, h}), nil 65 } 66 67 func (m muster) SyslogHandler(priority syslog.Priority, tag string, fmtr Format) Handler { 68 return must(SyslogHandler(priority, tag, fmtr)) 69 } 70 71 func (m muster) SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) Handler { 72 return must(SyslogNetHandler(net, addr, priority, tag, fmtr)) 73 }