2023-05-05 06:21:07 +00:00
|
|
|
// Package syslog is a syslog-type facility for logging.
|
2023-05-04 23:20:39 +00:00
|
|
|
package syslog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-05-05 06:40:20 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2023-05-04 23:20:39 +00:00
|
|
|
gsyslog "github.com/hashicorp/go-syslog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logger struct {
|
|
|
|
l gsyslog.Syslogger
|
|
|
|
p gsyslog.Priority
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}) {
|
|
|
|
if !strings.HasSuffix(format, "\n") {
|
|
|
|
format += "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
if p <= log.p {
|
|
|
|
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
|
|
|
fmt.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if log.l != nil {
|
|
|
|
log.l.WriteLevel(p, []byte(fmt.Sprintf(format, args...)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *logger) print(p gsyslog.Priority, args ...interface{}) {
|
|
|
|
if p <= log.p {
|
|
|
|
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
|
|
|
fmt.Print(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if log.l != nil {
|
|
|
|
log.l.WriteLevel(p, []byte(fmt.Sprint(args...)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *logger) println(p gsyslog.Priority, args ...interface{}) {
|
|
|
|
if p <= log.p {
|
|
|
|
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
|
|
|
fmt.Println(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if log.l != nil {
|
|
|
|
log.l.WriteLevel(p, []byte(fmt.Sprintln(args...)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:40:20 +00:00
|
|
|
func (log *logger) spew(args ...interface{}) {
|
|
|
|
if log.p == gsyslog.LOG_DEBUG {
|
|
|
|
spew.Dump(args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:20:39 +00:00
|
|
|
func (log *logger) adjustPriority(level string) error {
|
|
|
|
priority, ok := priorities[level]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("log: unknown priority %s", level)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.p = priority
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var log = &logger{p: gsyslog.LOG_WARNING}
|
|
|
|
|
|
|
|
var priorities = map[string]gsyslog.Priority{
|
|
|
|
"EMERG": gsyslog.LOG_EMERG,
|
|
|
|
"ALERT": gsyslog.LOG_ALERT,
|
|
|
|
"CRIT": gsyslog.LOG_CRIT,
|
|
|
|
"ERR": gsyslog.LOG_ERR,
|
|
|
|
"WARNING": gsyslog.LOG_WARNING,
|
|
|
|
"NOTICE": gsyslog.LOG_NOTICE,
|
|
|
|
"INFO": gsyslog.LOG_INFO,
|
|
|
|
"DEBUG": gsyslog.LOG_DEBUG,
|
|
|
|
}
|
|
|
|
|
|
|
|
var prioritiev = map[gsyslog.Priority]string{
|
|
|
|
gsyslog.LOG_EMERG: "EMERG",
|
|
|
|
gsyslog.LOG_ALERT: "ALERT",
|
|
|
|
gsyslog.LOG_CRIT: "CRIT",
|
|
|
|
gsyslog.LOG_ERR: "ERR",
|
|
|
|
gsyslog.LOG_WARNING: "WARNING",
|
|
|
|
gsyslog.LOG_NOTICE: "NOTICE",
|
|
|
|
gsyslog.LOG_INFO: "INFO",
|
|
|
|
gsyslog.LOG_DEBUG: "DEBUG",
|
|
|
|
}
|
|
|
|
|
|
|
|
func timestamp() string {
|
|
|
|
return time.Now().Format("2006-01-02 15:04:05 MST")
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Level string
|
|
|
|
Tag string
|
|
|
|
Facility string
|
|
|
|
WriteSyslog bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns a sane set of defaults for syslog, using the program
|
|
|
|
// name as the tag name. withSyslog controls whether logs should be sent to
|
|
|
|
// syslog, too.
|
2023-05-05 06:21:07 +00:00
|
|
|
func DefaultOptions(tag string, withSyslog bool) *Options {
|
2023-05-04 23:20:39 +00:00
|
|
|
if tag == "" {
|
|
|
|
tag = os.Args[0]
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:21:07 +00:00
|
|
|
return &Options{
|
2023-05-04 23:20:39 +00:00
|
|
|
Level: "WARNING",
|
|
|
|
Tag: tag,
|
|
|
|
Facility: "daemon",
|
|
|
|
WriteSyslog: withSyslog,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:21:07 +00:00
|
|
|
// DefaultDebugOptions returns a sane set of debug defaults for syslog,
|
2023-05-04 23:20:39 +00:00
|
|
|
// using the program name as the tag name. withSyslog controls whether logs
|
|
|
|
// should be sent to syslog, too.
|
2023-05-05 06:21:07 +00:00
|
|
|
func DefaultDebugOptions(tag string, withSyslog bool) *Options {
|
2023-05-04 23:20:39 +00:00
|
|
|
if tag == "" {
|
|
|
|
tag = os.Args[0]
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:21:07 +00:00
|
|
|
return &Options{
|
2023-05-04 23:20:39 +00:00
|
|
|
Level: "DEBUG",
|
|
|
|
Facility: "daemon",
|
|
|
|
WriteSyslog: withSyslog,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Setup(opts *Options) error {
|
|
|
|
priority, ok := priorities[opts.Level]
|
|
|
|
if !ok {
|
2023-05-05 06:21:07 +00:00
|
|
|
return fmt.Errorf("log: unknown priority %s", opts.Level)
|
2023-05-04 23:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.p = priority
|
|
|
|
|
2023-05-06 07:57:42 +00:00
|
|
|
if opts.WriteSyslog {
|
|
|
|
var err error
|
|
|
|
log.l, err = gsyslog.NewLogger(priority, opts.Facility, opts.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-04 23:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_DEBUG, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_INFO, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Notice(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_NOTICE, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warning(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_WARNING, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Err(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_ERR, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Crit(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_CRIT, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Alert(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_ALERT, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Emerg(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_EMERG, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debugln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_DEBUG, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Infoln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_INFO, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Noticeln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_NOTICE, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warningln(args ...interface{}) {
|
|
|
|
log.print(gsyslog.LOG_WARNING, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_ERR, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Critln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_CRIT, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Alertln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_ALERT, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Emergln(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_EMERG, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debugf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_DEBUG, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_INFO, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Noticef(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_NOTICE, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warningf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_WARNING, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_ERR, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Critf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_CRIT, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Alertf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_ALERT, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Emergf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_EMERG, format, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
log.println(gsyslog.LOG_ERR, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...interface{}) {
|
|
|
|
log.printf(gsyslog.LOG_ERR, format, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:40:20 +00:00
|
|
|
// Spew will pretty print the args if the logger is set to DEBUG priority.
|
|
|
|
func Spew(args ...interface{}) {
|
|
|
|
log.spew(args...)
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:20:39 +00:00
|
|
|
func ChangePriority(level string) error {
|
|
|
|
return log.adjustPriority(level)
|
|
|
|
}
|