log: fixups, add FatalError

- Support suppressing console output.
- DefaultDebugOptions sets the correct tag now.
- FatalError(error, string) calls log.Fatal(message, err) if err != nil.
This commit is contained in:
Kyle Isom 2023-05-11 19:03:18 -07:00
parent 72fdc255e7
commit 7a4e7977c3
1 changed files with 36 additions and 16 deletions

View File

@ -12,8 +12,9 @@ import (
) )
type logger struct { type logger struct {
l gsyslog.Syslogger l gsyslog.Syslogger
p gsyslog.Priority p gsyslog.Priority
writeConsole bool
} }
func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}) { func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}) {
@ -21,7 +22,7 @@ func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}
format += "\n" format += "\n"
} }
if p <= log.p { if p <= log.p && log.writeConsole {
fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
fmt.Printf(format, args...) fmt.Printf(format, args...)
} }
@ -32,7 +33,7 @@ func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}
} }
func (log *logger) print(p gsyslog.Priority, args ...interface{}) { func (log *logger) print(p gsyslog.Priority, args ...interface{}) {
if p <= log.p { if p <= log.p && log.writeConsole {
fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
fmt.Print(args...) fmt.Print(args...)
} }
@ -43,7 +44,7 @@ func (log *logger) print(p gsyslog.Priority, args ...interface{}) {
} }
func (log *logger) println(p gsyslog.Priority, args ...interface{}) { func (log *logger) println(p gsyslog.Priority, args ...interface{}) {
if p <= log.p { if p <= log.p && log.writeConsole {
fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
fmt.Println(args...) fmt.Println(args...)
} }
@ -98,10 +99,11 @@ func timestamp() string {
} }
type Options struct { type Options struct {
Level string Level string
Tag string Tag string
Facility string Facility string
WriteSyslog bool WriteSyslog bool
WriteConsole bool
} }
// DefaultOptions returns a sane set of defaults for syslog, using the program // DefaultOptions returns a sane set of defaults for syslog, using the program
@ -113,10 +115,11 @@ func DefaultOptions(tag string, withSyslog bool) *Options {
} }
return &Options{ return &Options{
Level: "WARNING", Level: "WARNING",
Tag: tag, Tag: tag,
Facility: "daemon", Facility: "daemon",
WriteSyslog: withSyslog, WriteSyslog: withSyslog,
WriteConsole: true,
} }
} }
@ -129,9 +132,11 @@ func DefaultDebugOptions(tag string, withSyslog bool) *Options {
} }
return &Options{ return &Options{
Level: "DEBUG", Level: "DEBUG",
Facility: "daemon", Tag: tag,
WriteSyslog: withSyslog, Facility: "daemon",
WriteSyslog: withSyslog,
WriteConsole: true,
} }
} }
@ -142,6 +147,10 @@ func Setup(opts *Options) error {
} }
log.p = priority log.p = priority
log.writeConsole = opts.WriteConsole
if opts.WriteConsole {
fmt.Println("will write to console")
}
if opts.WriteSyslog { if opts.WriteSyslog {
var err error var err error
@ -261,6 +270,17 @@ func Fatalf(format string, args ...interface{}) {
os.Exit(1) os.Exit(1)
} }
// FatalError will only execute if err != nil. If it does,
// it will print the message (append the error) and exit
// the program.
func FatalError(err error, message string) {
if err == nil {
return
}
Fatal(fmt.Sprintf("%s: %s", message, err))
}
// Spew will pretty print the args if the logger is set to DEBUG priority. // Spew will pretty print the args if the logger is set to DEBUG priority.
func Spew(args ...interface{}) { func Spew(args ...interface{}) {
log.spew(args...) log.spew(args...)