Skip to content

Instantly share code, notes, and snippets.

@cmdrkeene
Created May 1, 2015 19:12
Show Gist options
  • Save cmdrkeene/d8fcd25bc7a8b2c4b448 to your computer and use it in GitHub Desktop.
Save cmdrkeene/d8fcd25bc7a8b2c4b448 to your computer and use it in GitHub Desktop.
An alternative leveled logger package that follows unix convention for output
// Package log follows linux output convention
// Top level functions like Test() and Verbose() set the package "mode"
package log
import (
"io/ioutil"
"log"
"os"
)
var (
Debug *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
var (
PrefixDebug = "[DEBUG] "
PrefixWarning = "[WARNING] "
PrefixError = "[ERROR] "
PrefixInfo = "[INFO] "
)
const (
FlagsDefault = log.Lshortfile
FlagsTest = log.Lshortfile
FlagsVerbose = log.Ldate | log.Ltime | log.Lshortfile
FlagsSilent = 0
)
func init() {
Default()
}
// Default sets outputs to sensible defaults (none, stderr, stderr, stdout)
func Default() {
Debug = log.New(ioutil.Discard, PrefixDebug, FlagsDefault)
Warning = log.New(os.Stderr, PrefixWarning, FlagsDefault)
Error = log.New(os.Stderr, PrefixError, FlagsDefault)
Info = log.New(os.Stdout, PrefixInfo, FlagsDefault)
}
// Test sets outputs to standard out
func Test() {
Debug = log.New(os.Stdout, PrefixDebug, FlagsTest)
Warning = log.New(os.Stdout, PrefixWarning, FlagsTest)
Error = log.New(os.Stdout, PrefixError, FlagsTest)
Info = log.New(os.Stdout, PrefixInfo, FlagsTest)
}
// Verbose sets outputs to standard out
func Verbose() {
Debug = log.New(os.Stdout, PrefixDebug, FlagsVerbose)
Warning = log.New(os.Stdout, PrefixWarning, FlagsVerbose)
Error = log.New(os.Stdout, PrefixError, FlagsVerbose)
Info = log.New(os.Stdout, PrefixInfo, FlagsVerbose)
}
// Verbose sets outputs to none
func Silent() {
Debug = log.New(ioutil.Discard, PrefixDebug, FlagsSilent)
Warning = log.New(ioutil.Discard, PrefixWarning, FlagsSilent)
Error = log.New(ioutil.Discard, PrefixError, FlagsSilent)
Info = log.New(ioutil.Discard, PrefixInfo, FlagsSilent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment