diff --git a/pkg/cli/gptscript.go b/pkg/cli/gptscript.go index c9c11396..6fbc0bc2 100644 --- a/pkg/cli/gptscript.go +++ b/pkg/cli/gptscript.go @@ -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"` @@ -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() } diff --git a/pkg/mvl/log.go b/pkg/mvl/log.go index a558ab34..f35e023e 100644 --- a/pkg/mvl/log.go +++ b/pkg/mvl/log.go @@ -1,6 +1,7 @@ package mvl import ( + "encoding/json" "fmt" "io" "os" @@ -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) { @@ -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 != "" {