This isolates logging from the standard library

This commit adds a custom encapsulated Logger, which then has its
methods exposed in a controlled fashion. This prevents external
libraries from modifying things such as the flags passed to the logger,
or where it should output to.

Change-Id: Ide4321246b1277fcee51608aa95532b8696b9777
This commit is contained in:
Ian Howell 2019-10-04 10:31:41 -05:00
parent dc9c78b210
commit 062f59119f
1 changed files with 12 additions and 8 deletions

View File

@ -3,46 +3,50 @@ package log
import ( import (
"io" "io"
"log" "log"
"os"
) )
var debug = false var (
debug = false
airshipLog = log.New(os.Stderr, "", log.LstdFlags)
)
// Init initializes settings related to logging // Init initializes settings related to logging
func Init(debugFlag bool, out io.Writer) { func Init(debugFlag bool, out io.Writer) {
debug = debugFlag debug = debugFlag
log.SetOutput(out) airshipLog.SetOutput(out)
} }
// Debug is a wrapper for log.Debug // Debug is a wrapper for log.Debug
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
if debug { if debug {
log.Print(v...) airshipLog.Print(v...)
} }
} }
// Debugf is a wrapper for log.Debugf // Debugf is a wrapper for log.Debugf
func Debugf(format string, v ...interface{}) { func Debugf(format string, v ...interface{}) {
if debug { if debug {
log.Printf(format, v...) airshipLog.Printf(format, v...)
} }
} }
// Print is a wrapper for log.Print // Print is a wrapper for log.Print
func Print(v ...interface{}) { func Print(v ...interface{}) {
log.Print(v...) airshipLog.Print(v...)
} }
// Printf is a wrapper for log.Printf // Printf is a wrapper for log.Printf
func Printf(format string, v ...interface{}) { func Printf(format string, v ...interface{}) {
log.Printf(format, v...) airshipLog.Printf(format, v...)
} }
// Fatal is a wrapper for log.Fatal // Fatal is a wrapper for log.Fatal
func Fatal(v ...interface{}) { func Fatal(v ...interface{}) {
log.Fatal(v...) airshipLog.Fatal(v...)
} }
// Fatalf is a wrapper for log.Fatalf // Fatalf is a wrapper for log.Fatalf
func Fatalf(format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...) airshipLog.Fatalf(format, v...)
} }