-
Notifications
You must be signed in to change notification settings - Fork 12
Style pre-execute errors via logrus formatter #366
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,15 @@ package logging | |
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "unicode" | ||
|
|
||
| "charm.land/lipgloss/v2" | ||
| "github.com/charmbracelet/x/term" | ||
| "github.com/fatih/color" | ||
| "github.com/lets-cli/fang" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
|
|
@@ -15,11 +21,63 @@ type LogRepresenter interface { | |
| Repr() string | ||
| } | ||
|
|
||
| type errorStyles struct { | ||
| header lipgloss.Style | ||
| text lipgloss.Style | ||
| } | ||
|
|
||
| // Formatter formats a log entry in a human readable way. | ||
| type Formatter struct{} | ||
| type Formatter struct { | ||
| errorStyles *errorStyles // nil when output is not a TTY or no scheme given | ||
| } | ||
|
|
||
| // newFormatter builds a Formatter, enabling lipgloss error styling when | ||
| // errWriter is a terminal and cs is non-nil. | ||
| func newFormatter(errWriter io.Writer, cs fang.ColorSchemeFunc) *Formatter { | ||
| f := &Formatter{} | ||
| if cs == nil { | ||
| return f | ||
| } | ||
|
|
||
| file, ok := errWriter.(term.File) | ||
| if !ok || !term.IsTerminal(file.Fd()) { | ||
| return f | ||
| } | ||
|
|
||
| isDark := lipgloss.HasDarkBackground(os.Stdin, file) | ||
| scheme := cs(lipgloss.LightDark(isDark)) | ||
|
|
||
| w, _, err := term.GetSize(file.Fd()) | ||
| if err != nil || w == 0 { | ||
| w = 160 | ||
| } | ||
| if w > 160 { | ||
| w = 160 | ||
| } | ||
|
|
||
| f.errorStyles = &errorStyles{ | ||
| header: lipgloss.NewStyle(). | ||
| Foreground(scheme.ErrorHeader[0]). | ||
| Background(scheme.ErrorHeader[1]). | ||
| Bold(true). | ||
| Padding(0, 1). | ||
| Margin(1). | ||
| MarginLeft(2). | ||
| SetString("ERROR"), | ||
| text: lipgloss.NewStyle(). | ||
| MarginLeft(2). | ||
| Width(w - 2), | ||
| } | ||
|
|
||
| return f | ||
| } | ||
|
|
||
| // Format implements the log.Formatter interface. | ||
| func (f *Formatter) Format(entry *log.Entry) ([]byte, error) { | ||
| if entry.Level == log.ErrorLevel && f.errorStyles != nil { | ||
| return f.formatStyledError(entry), nil | ||
| } | ||
|
|
||
| buff := &bytes.Buffer{} | ||
| parts := []string{formatPrefix(entry)} | ||
|
|
||
|
|
@@ -35,6 +93,15 @@ func (f *Formatter) Format(entry *log.Entry) ([]byte, error) { | |
| return buff.Bytes(), nil | ||
| } | ||
|
|
||
| func (f *Formatter) formatStyledError(entry *log.Entry) []byte { | ||
| var buf bytes.Buffer | ||
| buf.WriteString(f.errorStyles.header.String()) | ||
| buf.WriteString("\n") | ||
| buf.WriteString(f.errorStyles.text.Render(capitalizeFirst(entry.Message) + ".")) | ||
| buf.WriteString("\n\n") | ||
|
Comment on lines
+96
to
+101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Styled error formatting drops all structured fields attached to the entry. The non-styled formatter includes |
||
| return buf.Bytes() | ||
| } | ||
|
|
||
| func formatPrefix(entry *log.Entry) string { | ||
| if entry.Level == log.DebugLevel { | ||
| return color.BlueString("lets:") | ||
|
|
@@ -65,3 +132,12 @@ func writeData(fields log.Fields) string { | |
|
|
||
| return strings.Join(buff, " ") | ||
| } | ||
|
|
||
| func capitalizeFirst(s string) string { | ||
| if s == "" { | ||
| return s | ||
| } | ||
| runes := []rune(s) | ||
| runes[0] = unicode.ToUpper(runes[0]) | ||
| return string(runes) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Handle very narrow terminal widths to avoid negative or zero lipgloss width.
For very small widths (e.g.
w == 1),Width(w - 2)may be 0 or negative, which can cause broken or undefined lipgloss rendering. Clampwto a sane minimum (e.g.w = max(20, min(w, 160))) or otherwise ensure the value passed toWidthis always >= 1.