Skip to content

Commit

Permalink
fix: Set console log level to the application one when writing to file
Browse files Browse the repository at this point in the history
This already works when it isn't redirected to a file as it goes through
the application wide logger.
  • Loading branch information
mstoykov committed Oct 11, 2023
1 parent 6e2c8e8 commit 15b9e77
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion js/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func newConsole(logger logrus.FieldLogger) *console {
}

// Creates a console logger with its output set to the file at the provided `filepath`.
func newFileConsole(filepath string, formatter logrus.Formatter) (*console, error) {
func newFileConsole(filepath string, formatter logrus.Formatter, level logrus.Level) (*console, error) {
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644) //nolint:gosec
if err != nil {
return nil, err
}

l := logrus.New()
l.SetLevel(level)
l.SetOutput(f)
l.SetFormatter(formatter)

Expand Down
4 changes: 3 additions & 1 deletion js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,12 @@ func (r *Runner) SetOptions(opts lib.Options) error {
// TODO: fix logger hack, see https://github.com/grafana/k6/issues/2958
// and https://github.com/grafana/k6/issues/2968
var formatter logrus.Formatter = &logrus.JSONFormatter{}
level := logrus.InfoLevel
if l, ok := r.preInitState.Logger.(*logrus.Logger); ok { //nolint: forbidigo
formatter = l.Formatter
level = l.Level
}
c, err := newFileConsole(opts.ConsoleOutput.String, formatter)
c, err := newFileConsole(opts.ConsoleOutput.String, formatter, level)
if err != nil {
return err
}
Expand Down

0 comments on commit 15b9e77

Please sign in to comment.