Last active
August 29, 2015 14:20
-
-
Save cmdrkeene/22f45517c8bd681d46f2 to your computer and use it in GitHub Desktop.
A simple leveled logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo | |
import "os" | |
import "log" | |
// Level indicates Logger verbosity | |
type Level int | |
// LogLevel specifies the minimum level logged | |
var LogLevel = Debug | |
// Logger is the common logger | |
var Logger = log.New(os.Stderr, "[FOO]", log.LstdFlags) | |
const ( | |
// Debug is used for diagnostic, developer information | |
Debug Level = iota | |
// Warn is used for automatic error fixing or potential problems | |
Warn | |
// Error is used for failures for the current operation, not to the system | |
Error | |
// Info is for domain-level notifications | |
Info | |
) | |
// String returns a prefix | |
func (l Level) String() string { | |
switch l { | |
case Debug: | |
return "[DEBUG]" | |
case Warn: | |
return "[WARN]" | |
case Error: | |
return "[ERROR]" | |
case Info: | |
return "[INFO]" | |
default: | |
panic("unknown level") | |
} | |
} | |
// Println appends Level prefix and prints to Logger | |
func (l Level) Println(v ...interface{}) { | |
if l >= LogLevel { | |
Logger.Println(l.withPrefix(v)...) | |
} | |
} | |
// Printf appends Level prefix and prints with format to Logger | |
func (l Level) Printf(format string, v ...interface{}) { | |
if l >= LogLevel { | |
Logger.Printf(format, l.withPrefix(v)...) | |
} | |
} | |
func (l Level) withPrefix(v ...interface{}) []interface{} { | |
return append([]interface{}{l.String()}, v...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment