Skip to content

Commit

Permalink
fix: split logs to stdout and stderr (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baruch Odem (Rothkoff) authored Aug 10, 2023
1 parent 6be9ba5 commit ce8b843
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions lib/logger.go
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()
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"os/signal"

"github.com/checkmarx/2ms/cmd"
"github.com/checkmarx/2ms/lib"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
// send all logs to stdout
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})
log.Logger = lib.CreateLogger(zerolog.InfoLevel)

// this block sets up a go routine to listen for an interrupt signal
// which will immediately exit gitleaks
Expand Down

0 comments on commit ce8b843

Please sign in to comment.