Skip to content

Commit 675a83a

Browse files
committed
Add a smarter formatter to simulate the exact logger behaviour
This adds a more clever formatter that uses the logger call depth to return accurate data about the logged message in question.
1 parent 6a31b18 commit 675a83a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

capnslog/formatters.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ func (c *PrettyFormatter) Format(pkg string, l LogLevel, depth int, entries ...i
127127
func (c *PrettyFormatter) Flush() {
128128
c.w.Flush()
129129
}
130+
131+
type LogFormatter struct {
132+
w *bufio.Writer
133+
logger *log.Logger
134+
prefix string
135+
}
136+
137+
func NewLogFormatter(w io.Writer, prefix string, flag int) *LogFormatter {
138+
return &LogFormatter{
139+
w: bufio.NewWriter(w),
140+
logger: log.New(w, "", flag), // don't use prefix here
141+
prefix: prefix, // save it instead
142+
}
143+
}
144+
145+
func (s *LogFormatter) Format(pkg string, l LogLevel, i int, entries ...interface{}) {
146+
str := fmt.Sprint(entries...)
147+
prefix := s.prefix
148+
if pkg != "" {
149+
prefix = fmt.Sprintf("%s%s: ", prefix, pkg)
150+
}
151+
s.logger.Output(5, fmt.Sprintf("%s%v", prefix, str)) // call depth is 5
152+
s.Flush()
153+
}
154+
155+
func (s *LogFormatter) Flush() {
156+
s.w.Flush()
157+
}

0 commit comments

Comments
 (0)