github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/ulog/log.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package ulog exposes logging via a Go interface. 6 // 7 // ulog has three implementations of the Logger interface: a Go standard 8 // library "log" package Logger, a kernel syslog (dmesg) Logger, and a test 9 // Logger that logs via a test's testing.TB.Logf. 10 package ulog 11 12 import ( 13 "log" 14 "os" 15 "testing" 16 ) 17 18 // Logger is a log receptacle. 19 // 20 // It puts your information somewhere for safekeeping. 21 type Logger interface { 22 Printf(format string, v ...interface{}) 23 Print(v ...interface{}) 24 } 25 26 // Log is a Logger that prints to stderr, like the default log package. 27 var Log = log.New(os.Stderr, "", log.LstdFlags) 28 29 // TestLogger is a Logger implementation that logs to testing.TB.Logf. 30 type TestLogger struct { 31 TB testing.TB 32 } 33 34 // Printf formats according to the format specifier and prints to a unit test's log. 35 func (tl TestLogger) Printf(format string, v ...interface{}) { 36 tl.TB.Logf(format, v...) 37 } 38 39 // Print formats according the default formats for v and prints to a unit test's log. 40 func (tl TestLogger) Print(v ...interface{}) { 41 tl.TB.Log(v...) 42 }