Skip to content

chore: cleanup output to make logs less noisy #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type GPTScript struct {
Color *bool `usage:"Use color in output (default true)" default:"true"`
Confirm bool `usage:"Prompt before running potentially dangerous commands"`
Debug bool `usage:"Enable debug logging"`
NoTrunc bool `usage:"Do not truncate long log messages"`
Quiet *bool `usage:"No output logging (set --quiet=false to force on even when there is no TTY)" short:"q"`
Output string `usage:"Save output to a file, or - for stdout" short:"o"`
EventsStreamTo string `usage:"Stream events to this location, could be a file descriptor/handle (e.g. fd://2), filename, or named pipe (e.g. \\\\.\\pipe\\my-pipe)" name:"events-stream-to"`
Expand Down Expand Up @@ -237,7 +238,7 @@ func (r *GPTScript) PersistentPre(*cobra.Command, []string) error {
r.Color = new(bool)
}
} else {
mvl.SetSimpleFormat()
mvl.SetSimpleFormat(!r.NoTrunc)
if *r.Quiet {
mvl.SetError()
}
Expand Down
22 changes: 20 additions & 2 deletions pkg/mvl/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mvl

import (
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -17,11 +18,14 @@ import (
// So this is simple place to make a better decision later about logging frameworks. I only care about
// the interface, not the implementation. Smarter people do that well.

func SetSimpleFormat() {
logrus.SetFormatter(&formatter{})
func SetSimpleFormat(trunc bool) {
logrus.SetFormatter(&formatter{
trunc: trunc,
})
}

type formatter struct {
trunc bool
}

func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
Expand All @@ -30,6 +34,20 @@ func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
msg += fmt.Sprintf(" [input=%s]", i)
}
if i, ok := entry.Data["output"].(string); ok && i != "" {
if f.trunc {
i = strings.TrimSpace(i)
addDot := false
if len(i) > 100 {
addDot = true
i = i[:100]
}
d, _ := json.Marshal(i)
i = string(d)
i = strings.TrimSpace(i[1 : len(i)-2])
if addDot {
i += "..."
}
}
msg += fmt.Sprintf(" [output=%s]", i)
}
if i, ok := entry.Data["request"]; ok && i != "" {
Expand Down