AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### logger ####### Copyright (c) 2021-2022 losyme ############################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package logger
import (
"log/syslog"
"forge.chapril.org/losyme/kvfmt"
)
type outputSyslog struct {
*syslog.Writer
}
func NewSyslogOutput(app, facility string) (Output, error) {
var p syslog.Priority
switch facility {
case "local1":
p = syslog.LOG_LOCAL1
case "local2":
p = syslog.LOG_LOCAL2
case "local3":
p = syslog.LOG_LOCAL3
case "local4":
p = syslog.LOG_LOCAL4
case "local5":
p = syslog.LOG_LOCAL5
case "local6":
p = syslog.LOG_LOCAL6
case "local7":
p = syslog.LOG_LOCAL7
default:
p = syslog.LOG_LOCAL0
}
sw, err := syslog.New(p, app)
if err != nil {
return nil, err
}
return &outputSyslog{sw}, nil
}
func (o *outputSyslog) Log(l *Log) error {
buf := BufPool.Get()
buf.AppendString(l.Logger.label)
buf.AppendByte(' ')
buf.AppendString(l.Msg)
if len(l.KV) > 0 {
buf.AppendString("> ")
kvfmt.Format(buf, l.KV...)
}
var err error
switch l.Level {
case LevelTrace:
err = o.Debug(buf.String())
case LevelDebug:
err = o.Debug(buf.String())
case LevelInfo:
err = o.Info(buf.String())
case LevelNotice:
err = o.Notice(buf.String())
case LevelWarning:
err = o.Warning(buf.String())
case LevelError:
err = o.Err(buf.String())
case LevelFatal:
err = o.Crit(buf.String())
}
BufPool.Put(buf)
return err
}
/*
######################################################################################################## @(°_°)@ #######
*/