File tree Expand file tree Collapse file tree 4 files changed +30
-2
lines changed Expand file tree Collapse file tree 4 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,21 @@ stdLogger.Printf("[DEBUG] %+v", stdLogger)
128
128
... [DEBUG] my-app: &{mu:{state:0 sema:0} prefix: flag:0 out:0xc42000a0a0 buf:[]}
129
129
```
130
130
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
+
131
146
Notice that if ` appLogger ` is initialized with the ` INFO ` log level _ and_ you
132
147
specify ` InferLevels: true ` , you will not see any output here. You must change
133
148
` appLogger ` to ` DEBUG ` to see output. See the docs for more information.
Original file line number Diff line number Diff line change 6
6
"encoding"
7
7
"encoding/json"
8
8
"fmt"
9
+ "io"
9
10
"log"
10
11
"os"
11
12
"reflect"
@@ -503,5 +504,9 @@ func (z *intLogger) StandardLogger(opts *StandardLoggerOptions) *log.Logger {
503
504
opts = & StandardLoggerOptions {}
504
505
}
505
506
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 }
507
512
}
Original file line number Diff line number Diff line change @@ -127,6 +127,9 @@ type Logger interface {
127
127
128
128
// Return a value that conforms to the stdlib log.Logger interface
129
129
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
130
133
}
131
134
132
135
type StandardLoggerOptions struct {
Original file line number Diff line number Diff line change 1
1
package hclog
2
2
3
3
import (
4
+ "io"
4
5
"io/ioutil"
5
6
"log"
6
7
)
@@ -43,5 +44,9 @@ func (l *nullLogger) ResetNamed(name string) Logger { return l }
43
44
func (l * nullLogger ) SetLevel (level Level ) {}
44
45
45
46
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
47
52
}
You can’t perform that action at this time.
0 commit comments