Skip to content

Commit

Permalink
Split stdout and stderr in logrus (#108)
Browse files Browse the repository at this point in the history
* split stdout and stderr

* delete extra line
  • Loading branch information
karenychen authored Jun 15, 2022
1 parent ea12678 commit b7fa007
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"os"

"github.com/Azure/draft/pkg/logger"
cc "github.com/ivanpirog/coloredcobra"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/viper"
"github.com/Azure/draft/pkg/logger"
)

var cfgFile string
Expand All @@ -33,9 +33,10 @@ For more information, please visit the Draft Github page: https://github.com/Azu
if verbose {
logrus.SetLevel(logrus.DebugLevel)
} else if silent {
logrus.SetLevel(logrus.WarnLevel)
logrus.SetLevel(logrus.ErrorLevel)

}
logrus.SetOutput(&logger.OutputSplitter{})
logrus.SetFormatter(new(logger.CustomFormatter))
},
}
Expand Down
18 changes: 15 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package logger

import (
log "github.com/sirupsen/logrus"
"github.com/fatih/color"
"fmt"
"bytes"
"fmt"
"os"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
)

type CustomFormatter struct {}
Expand All @@ -12,3 +15,12 @@ func (f *CustomFormatter) Format(entry *log.Entry) ([]byte, error) {
cyan := color.New(color.Bold, color.FgCyan).SprintFunc()
return []byte(fmt.Sprintf("%s %s\n",cyan("[Draft]"), entry.Message)), nil
}

type OutputSplitter struct{}

func (splitter *OutputSplitter) Write(p []byte) (n int, err error) {
if bytes.Contains(p, []byte("level=error")) || bytes.Contains(p, []byte("level=fatal")) || bytes.Contains(p, []byte("level=panic")) {
return os.Stderr.Write(p)
}
return os.Stdout.Write(p)
}

0 comments on commit b7fa007

Please sign in to comment.