Skip to content

Commit 332ed19

Browse files
committed
Add the ability to wrap a log.Logger. Closes #45
1 parent 5f6a9d4 commit 332ed19

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

intlogger.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func newLogger(opts *LoggerOptions) *intLogger {
113113

114114
l.setColorization(opts)
115115

116-
if opts.TimeFormat != "" {
116+
if opts.DisableTime {
117+
l.timeFormat = ""
118+
} else if opts.TimeFormat != "" {
117119
l.timeFormat = opts.TimeFormat
118120
}
119121

@@ -180,8 +182,10 @@ var logImplFile = regexp.MustCompile(`github.com/hashicorp/go-hclog/.+logger.go$
180182

181183
// Non-JSON logging format function
182184
func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string, args ...interface{}) {
183-
l.writer.WriteString(t.Format(l.timeFormat))
184-
l.writer.WriteByte(' ')
185+
if len(l.timeFormat) > 0 {
186+
l.writer.WriteString(t.Format(l.timeFormat))
187+
l.writer.WriteByte(' ')
188+
}
185189

186190
s, ok := _levelToBracket[level]
187191
if ok {

logger.go

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ type LoggerOptions struct {
230230
// The time format to use instead of the default
231231
TimeFormat string
232232

233+
// Control whether or not to display the time at all. This is required
234+
// because setting TimeFormat to empty assumes the default format.
235+
DisableTime bool
236+
233237
// Color the output. On Windows, colored logs are only avaiable for io.Writers that
234238
// are concretely instances of *os.File.
235239
Color ColorOption

stdlog.go

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hclog
22

33
import (
44
"bytes"
5+
"log"
56
"strings"
67
)
78

@@ -72,3 +73,23 @@ func (s *stdlogAdapter) pickLevel(str string) (Level, string) {
7273
return Info, str
7374
}
7475
}
76+
77+
type logWriter struct {
78+
l *log.Logger
79+
}
80+
81+
func (l *logWriter) Write(b []byte) (int, error) {
82+
l.l.Println(string(bytes.TrimRight(b, " \n\t")))
83+
return len(b), nil
84+
}
85+
86+
// Takes a standard library logger and returns a Logger that will write to it
87+
func FromStandardLogger(l *log.Logger, opts *LoggerOptions) Logger {
88+
var dl LoggerOptions = *opts
89+
90+
// Use the time format that log.Logger uses
91+
dl.DisableTime = true
92+
dl.Output = &logWriter{l}
93+
94+
return New(&dl)
95+
}

stdlog_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package hclog
22

33
import (
44
"bytes"
5+
"log"
6+
"regexp"
57
"strings"
68
"testing"
79

@@ -152,3 +154,19 @@ func TestStdlogAdapter_ForceLevel(t *testing.T) {
152154
})
153155
}
154156
}
157+
158+
func TestFromLogger(t *testing.T) {
159+
var buf bytes.Buffer
160+
161+
sl := log.New(&buf, "test-stdlib-log ", log.Ltime)
162+
163+
hl := FromStandardLogger(sl, &LoggerOptions{
164+
Name: "hclog-inner",
165+
})
166+
167+
hl.Info("this is a test", "name", "tester", "count", 1)
168+
169+
re := regexp.MustCompile(`test-stdlib-log [\d:]+ \[INFO\] hclog-inner: this is a test: name=tester count=1`)
170+
171+
assert.True(t, re.Match(buf.Bytes()))
172+
}

0 commit comments

Comments
 (0)