-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: split logs to stdout and stderr (#166)
Fixes #156 References: https://stackoverflow.com/questions/76858037/how-to-use-zerolog-to-filter-info-logs-to-stdout-and-error-logs-to-stderr rs/zerolog#150 (comment)
- Loading branch information
Baruch Odem (Rothkoff)
authored
Aug 10, 2023
1 parent
6be9ba5
commit ce8b843
Showing
2 changed files
with
43 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package lib | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type SpecificLevelWriter struct { | ||
io.Writer | ||
Levels []zerolog.Level | ||
} | ||
|
||
func (w SpecificLevelWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) { | ||
for _, l := range w.Levels { | ||
if l == level { | ||
return w.Write(p) | ||
} | ||
} | ||
return len(p), nil | ||
} | ||
|
||
func CreateLogger(minimumLevel zerolog.Level) zerolog.Logger { | ||
writer := zerolog.MultiLevelWriter( | ||
SpecificLevelWriter{ | ||
Writer: zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05"}, | ||
Levels: []zerolog.Level{ | ||
zerolog.DebugLevel, zerolog.InfoLevel, zerolog.WarnLevel, | ||
}, | ||
}, | ||
SpecificLevelWriter{ | ||
Writer: zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"}, | ||
Levels: []zerolog.Level{ | ||
zerolog.ErrorLevel, zerolog.FatalLevel, zerolog.PanicLevel, | ||
}, | ||
}, | ||
) | ||
|
||
return zerolog.New(writer).Level(minimumLevel).With().Timestamp().Logger() | ||
} |
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