40 Star 146 Fork 3

Gitee 极速下载/grafana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grafana/grafana
克隆/下载
syslog.go 2.04 KB
一键复制 编辑 原始数据 按行查看 历史
//+build !windows,!nacl,!plan9
package log
import (
"errors"
"log/syslog"
"os"
"github.com/inconshreveable/log15"
"gopkg.in/ini.v1"
)
type SysLogHandler struct {
syslog *syslog.Writer
Network string
Address string
Facility string
Tag string
Format log15.Format
}
func NewSyslog(sec *ini.Section, format log15.Format) *SysLogHandler {
handler := &SysLogHandler{
Format: log15.LogfmtFormat(),
}
handler.Format = format
handler.Network = sec.Key("network").MustString("")
handler.Address = sec.Key("address").MustString("")
handler.Facility = sec.Key("facility").MustString("local7")
handler.Tag = sec.Key("tag").MustString("")
if err := handler.Init(); err != nil {
Root.Error("Failed to init syslog log handler", "error", err)
os.Exit(1)
}
return handler
}
func (sw *SysLogHandler) Init() error {
prio, err := parseFacility(sw.Facility)
if err != nil {
return err
}
w, err := syslog.Dial(sw.Network, sw.Address, prio, sw.Tag)
if err != nil {
return err
}
sw.syslog = w
return nil
}
func (sw *SysLogHandler) Log(r *log15.Record) error {
var err error
msg := string(sw.Format.Format(r))
switch r.Lvl {
case log15.LvlDebug:
err = sw.syslog.Debug(msg)
case log15.LvlInfo:
err = sw.syslog.Info(msg)
case log15.LvlWarn:
err = sw.syslog.Warning(msg)
case log15.LvlError:
err = sw.syslog.Err(msg)
case log15.LvlCrit:
err = sw.syslog.Crit(msg)
default:
err = errors.New("invalid syslog level")
}
return err
}
func (sw *SysLogHandler) Close() {
sw.syslog.Close()
}
var facilities = map[string]syslog.Priority{
"user": syslog.LOG_USER,
"daemon": syslog.LOG_DAEMON,
"local0": syslog.LOG_LOCAL0,
"local1": syslog.LOG_LOCAL1,
"local2": syslog.LOG_LOCAL2,
"local3": syslog.LOG_LOCAL3,
"local4": syslog.LOG_LOCAL4,
"local5": syslog.LOG_LOCAL5,
"local6": syslog.LOG_LOCAL6,
"local7": syslog.LOG_LOCAL7,
}
func parseFacility(facility string) (syslog.Priority, error) {
prio, ok := facilities[facility]
if !ok {
return syslog.LOG_LOCAL0, errors.New("invalid syslog facility")
}
return prio, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/grafana.git
git@gitee.com:mirrors/grafana.git
mirrors
grafana
grafana
v6.1.0-beta1

搜索帮助

0d507c66 1850385 C8b1a773 1850385