Skip to content

Instantly share code, notes, and snippets.

@ascarter
Last active September 1, 2017 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ascarter/b3990d3a9213df2d58df4be9ab538386 to your computer and use it in GitHub Desktop.
Save ascarter/b3990d3a9213df2d58df4be9ab538386 to your computer and use it in GitHub Desktop.
Log wrapper for Go stdlib logger
package log
import (
"io"
"io/ioutil"
stdlog "log"
"os"
)
var (
// DebugLogger is a Logger dedicated for debug messages
DebugLogger = stdlog.New(ioutil.Discard, "DEBUG ", stdlog.LstdFlags)
// InfoLogger is a Logger dedicated for info messages
InfoLogger = stdlog.New(os.Stderr, "INFO ", stdlog.LstdFlags)
)
func init() {
if _, ok := os.LookupEnv("VERBOSE"); ok {
DebugLogger.SetOutput(os.Stderr)
}
}
// EnableDebug turns on debug logging
func EnableDebug() {
DebugLogger.SetOutput(os.Stderr)
}
// DisableDebug turns off debug logging
func DisableDebug() {
DebugLogger.SetOutput(ioutil.Discard)
}
// Debug wrappers
// Debug calls Output to print to the debug logger.
// Arguments are handled in the manner of fmt.Print.
func Debug(v ...interface{}) {
DebugLogger.Print(v...)
}
// Debugf calls Output to print to the debug logger.
// Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, v ...interface{}) {
DebugLogger.Printf(format, v...)
}
// Debugln calls Output to print to the debug logger.
// Arguments are handled in the manner of fmt.Println.
func Debugln(v ...interface{}) {
DebugLogger.Println(v...)
}
// Info wrappers
// Info calls Output to print to the Info logger.
// Arguments are handled in the manner of fmt.Print.
func Info(v ...interface{}) {
InfoLogger.Print(v...)
}
// Infof calls Output to print to the Info logger.
// Arguments are handled in the manner of fmt.Printf.
func Infof(format string, v ...interface{}) {
InfoLogger.Printf(format, v...)
}
// Infoln calls Output to print to the Info logger.
// Arguments are handled in the manner of fmt.Println.
func Infoln(v ...interface{}) {
InfoLogger.Println(v...)
}
// These functions write to the standard logger that is wrapped here.
// SetOutput sets the output destination for the standard logger.
func SetOutput(w io.Writer) {
stdlog.SetOutput(w)
}
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
stdlog.Print(v...)
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
stdlog.Printf(format, v...)
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
stdlog.Println(v...)
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
stdlog.Fatal(v...)
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
stdlog.Fatalf(format, v...)
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func Fatalln(v ...interface{}) {
stdlog.Fatalln(v...)
}
// Panic is equivalent to Print() followed by a call to panic().
func Panic(v ...interface{}) {
stdlog.Panic(v...)
}
// Panicf is equivalent to Printf() followed by a call to panic().
func Panicf(format string, v ...interface{}) {
stdlog.Panicf(format, v...)
}
// Panicln is equivalent to Println() followed by a call to panic().
func Panicln(v ...interface{}) {
Panicln(v...)
}
// Output writes the output for a logging event. The string s contains
// the text to print after the prefix specified by the flags of the
// Logger. A newline is appended if the last character of s is not
// already a newline. Calldepth is the count of the number of
// frames to skip when computing the file name and line number
// if Llongfile or Lshortfile is set; a value of 1 will print the details
// for the caller of Output.
func Output(calldepth int, s string) error {
return stdlog.Output(calldepth+1, s) // +1 for this frame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment