Skip to content

Commit 90f2fbc

Browse files
authored
Merge pull request #33 from hashicorp/f-expose-writer
Expose Writer to allow for configuring standard logger
2 parents 4783cae + b6eaa06 commit 90f2fbc

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ stdLogger.Printf("[DEBUG] %+v", stdLogger)
128128
... [DEBUG] my-app: &{mu:{state:0 sema:0} prefix: flag:0 out:0xc42000a0a0 buf:[]}
129129
```
130130

131+
Alternatively, you may configure the system-wide logger:
132+
133+
```go
134+
// log the standard logger from 'import "log"'
135+
log.SetOutput(appLogger.Writer(&hclog.StandardLoggerOptions{InferLevels: true}))
136+
log.SetPrefix("")
137+
log.SetFlags(0)
138+
139+
log.Printf("[DEBUG] %d", 42)
140+
```
141+
142+
```text
143+
... [DEBUG] my-app: 42
144+
```
145+
131146
Notice that if `appLogger` is initialized with the `INFO` log level _and_ you
132147
specify `InferLevels: true`, you will not see any output here. You must change
133148
`appLogger` to `DEBUG` to see output. See the docs for more information.

int.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding"
77
"encoding/json"
88
"fmt"
9+
"io"
910
"log"
1011
"os"
1112
"reflect"
@@ -503,5 +504,9 @@ func (z *intLogger) StandardLogger(opts *StandardLoggerOptions) *log.Logger {
503504
opts = &StandardLoggerOptions{}
504505
}
505506

506-
return log.New(&stdlogAdapter{z, opts.InferLevels}, "", 0)
507+
return log.New(z.StandardWriter(opts), "", 0)
508+
}
509+
510+
func (z *intLogger) StandardWriter(opts *StandardLoggerOptions) io.Writer {
511+
return &stdlogAdapter{z, opts.InferLevels}
507512
}

log.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ type Logger interface {
127127

128128
// Return a value that conforms to the stdlib log.Logger interface
129129
StandardLogger(opts *StandardLoggerOptions) *log.Logger
130+
131+
// Return a value that conforms to io.Writer, which can be passed into log.SetOutput()
132+
StandardWriter(opts *StandardLoggerOptions) io.Writer
130133
}
131134

132135
type StandardLoggerOptions struct {

nulllogger.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hclog
22

33
import (
4+
"io"
45
"io/ioutil"
56
"log"
67
)
@@ -43,5 +44,9 @@ func (l *nullLogger) ResetNamed(name string) Logger { return l }
4344
func (l *nullLogger) SetLevel(level Level) {}
4445

4546
func (l *nullLogger) StandardLogger(opts *StandardLoggerOptions) *log.Logger {
46-
return log.New(ioutil.Discard, "", log.LstdFlags)
47+
return log.New(l.StandardWriter(opts), "", log.LstdFlags)
48+
}
49+
50+
func (l *nullLogger) StandardWriter(opts *StandardLoggerOptions) io.Writer {
51+
return ioutil.Discard
4752
}

0 commit comments

Comments
 (0)