Skip to content

Commit 3bc5a6d

Browse files
chore: cleanup output to make logs less noisy
1 parent 46809ca commit 3bc5a6d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

pkg/cli/gptscript.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type GPTScript struct {
4545
Color *bool `usage:"Use color in output (default true)" default:"true"`
4646
Confirm bool `usage:"Prompt before running potentially dangerous commands"`
4747
Debug bool `usage:"Enable debug logging"`
48+
NoTrunc bool `usage:"Do not truncate long log messages"`
4849
Quiet *bool `usage:"No output logging (set --quiet=false to force on even when there is no TTY)" short:"q"`
4950
Output string `usage:"Save output to a file, or - for stdout" short:"o"`
5051
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 {
237238
r.Color = new(bool)
238239
}
239240
} else {
240-
mvl.SetSimpleFormat()
241+
mvl.SetSimpleFormat(!r.NoTrunc)
241242
if *r.Quiet {
242243
mvl.SetError()
243244
}

pkg/mvl/log.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mvl
22

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

20-
func SetSimpleFormat() {
21-
logrus.SetFormatter(&formatter{})
21+
func SetSimpleFormat(trunc bool) {
22+
logrus.SetFormatter(&formatter{
23+
trunc: trunc,
24+
})
2225
}
2326

2427
type formatter struct {
28+
trunc bool
2529
}
2630

2731
func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
@@ -30,6 +34,20 @@ func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
3034
msg += fmt.Sprintf(" [input=%s]", i)
3135
}
3236
if i, ok := entry.Data["output"].(string); ok && i != "" {
37+
if f.trunc {
38+
i = strings.TrimSpace(i)
39+
addDot := false
40+
if len(i) > 100 {
41+
addDot = true
42+
i = i[:100]
43+
}
44+
d, _ := json.Marshal(i)
45+
i = string(d)
46+
i = strings.TrimSpace(i[1 : len(i)-2])
47+
if addDot {
48+
i += "..."
49+
}
50+
}
3351
msg += fmt.Sprintf(" [output=%s]", i)
3452
}
3553
if i, ok := entry.Data["request"]; ok && i != "" {

0 commit comments

Comments
 (0)