-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from hashicorp/svh/f-level-writer
Add a LevelWriter interface to use as leveled writer
- Loading branch information
Showing
4 changed files
with
160 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package hclog | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
type writer struct { | ||
b bytes.Buffer | ||
w io.Writer | ||
} | ||
|
||
func newWriter(w io.Writer) *writer { | ||
return &writer{w: w} | ||
} | ||
|
||
func (w *writer) Flush(level Level) (err error) { | ||
if lw, ok := w.w.(LevelWriter); ok { | ||
_, err = lw.LevelWrite(level, w.b.Bytes()) | ||
} else { | ||
_, err = w.w.Write(w.b.Bytes()) | ||
} | ||
w.b.Reset() | ||
return err | ||
} | ||
|
||
func (w *writer) Write(p []byte) (int, error) { | ||
return w.b.Write(p) | ||
} | ||
|
||
func (w *writer) WriteByte(c byte) error { | ||
return w.b.WriteByte(c) | ||
} | ||
|
||
func (w *writer) WriteString(s string) (int, error) { | ||
return w.b.WriteString(s) | ||
} | ||
|
||
// LevelWriter is the interface that wraps the LevelWrite method. | ||
type LevelWriter interface { | ||
LevelWrite(level Level, p []byte) (n int, err error) | ||
} | ||
|
||
// LeveledWriter writes all log messages to the standard writer, | ||
// except for log levels that are defined in the overrides map. | ||
type LeveledWriter struct { | ||
standard io.Writer | ||
overrides map[Level]io.Writer | ||
} | ||
|
||
// NewLevelWriter returns an initialized LeveledWriter. | ||
// | ||
// standard will be used as the default writer for all log levels, | ||
// except for log levels that are defined in the overrides map. | ||
func NewLevelWriter(standard io.Writer, overrides map[Level]io.Writer) *LeveledWriter { | ||
return &LeveledWriter{ | ||
standard: standard, | ||
overrides: overrides, | ||
} | ||
} | ||
|
||
// Write implements io.Writer. | ||
func (lw *LeveledWriter) Write(p []byte) (int, error) { | ||
return lw.standard.Write(p) | ||
} | ||
|
||
// LevelWrite implements LevelWriter. | ||
func (lw *LeveledWriter) LevelWrite(level Level, p []byte) (int, error) { | ||
w, ok := lw.overrides[level] | ||
if !ok { | ||
w = lw.standard | ||
} | ||
return w.Write(p) | ||
} |