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:
parent
72fdc255e7
commit
7a4e7977c3
|
@ -14,6 +14,7 @@ import (
|
|||
type logger struct {
|
||||
l gsyslog.Syslogger
|
||||
p gsyslog.Priority
|
||||
writeConsole bool
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
if p <= log.p {
|
||||
if p <= log.p && log.writeConsole {
|
||||
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
||||
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{}) {
|
||||
if p <= log.p {
|
||||
if p <= log.p && log.writeConsole {
|
||||
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
||||
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{}) {
|
||||
if p <= log.p {
|
||||
if p <= log.p && log.writeConsole {
|
||||
fmt.Printf("%s [%s] ", prioritiev[p], timestamp())
|
||||
fmt.Println(args...)
|
||||
}
|
||||
|
@ -102,6 +103,7 @@ type Options struct {
|
|||
Tag string
|
||||
Facility string
|
||||
WriteSyslog bool
|
||||
WriteConsole bool
|
||||
}
|
||||
|
||||
// DefaultOptions returns a sane set of defaults for syslog, using the program
|
||||
|
@ -117,6 +119,7 @@ func DefaultOptions(tag string, withSyslog bool) *Options {
|
|||
Tag: tag,
|
||||
Facility: "daemon",
|
||||
WriteSyslog: withSyslog,
|
||||
WriteConsole: true,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,8 +133,10 @@ func DefaultDebugOptions(tag string, withSyslog bool) *Options {
|
|||
|
||||
return &Options{
|
||||
Level: "DEBUG",
|
||||
Tag: tag,
|
||||
Facility: "daemon",
|
||||
WriteSyslog: withSyslog,
|
||||
WriteConsole: true,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,6 +147,10 @@ func Setup(opts *Options) error {
|
|||
}
|
||||
|
||||
log.p = priority
|
||||
log.writeConsole = opts.WriteConsole
|
||||
if opts.WriteConsole {
|
||||
fmt.Println("will write to console")
|
||||
}
|
||||
|
||||
if opts.WriteSyslog {
|
||||
var err error
|
||||
|
@ -261,6 +270,17 @@ func Fatalf(format string, args ...interface{}) {
|
|||
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.
|
||||
func Spew(args ...interface{}) {
|
||||
log.spew(args...)
|
||||
|
|
Loading…
Reference in New Issue