Skip to content

Commit 94747fb

Browse files
authored
Merge pull request #319 from lets-cli/codex/update-logging-prefix-and-color
Centralize lets log prefix and colorize debug output
2 parents f906bbf + 1940aa4 commit 94747fb

9 files changed

Lines changed: 193 additions & 34 deletions

File tree

docs/docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ title: Changelog
1616
* `[Refactoring]` Move CLI startup flow from `cmd/lets/main.go` into `internal/cli/cli.go`, keeping `main.go` as a thin launcher.
1717
* `[Added]` Add `lets self doc` command to open the online documentation in a browser.
1818
* `[Added]` Show background update notifications for interactive sessions, with Homebrew-aware guidance and `LETS_CHECK_UPDATE` opt-out.
19+
* `[Changed]` Centralize the `lets:` log prefix in the formatter and render debug messages in blue.
1920

2021
## [0.0.59](https://github.com/lets-cli/lets/releases/tag/v0.0.59)
2122

internal/cli/cli.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ func Main(version string, buildDate string) int {
4747

4848
command, args, err := rootCmd.Traverse(os.Args[1:])
4949
if err != nil {
50-
log.Errorf("lets: traverse commands error: %s", err)
50+
log.Errorf("traverse commands error: %s", err)
5151
return getExitCode(err, 1)
5252
}
5353

5454
rootFlags, err := parseRootFlags(args)
5555
if err != nil {
56-
log.Errorf("lets: parse flags error: %s", err)
56+
log.Errorf("parse flags error: %s", err)
5757
return 1
5858
}
5959

6060
if rootFlags.version {
6161
if err := cmd.PrintVersionMessage(rootCmd); err != nil {
62-
log.Errorf("lets: print version error: %s", err)
62+
log.Errorf("print version error: %s", err)
6363
return 1
6464
}
6565

@@ -79,7 +79,7 @@ func Main(version string, buildDate string) int {
7979
cfg, err := config.Load(rootFlags.config, configDir, version)
8080
if err != nil {
8181
if failOnConfigError(rootCmd, command, rootFlags) {
82-
log.Errorf("lets: config error: %s", err)
82+
log.Errorf("config error: %s", err)
8383
return 1
8484
}
8585
}
@@ -96,7 +96,7 @@ func Main(version string, buildDate string) int {
9696
}
9797

9898
if err != nil {
99-
log.Errorf("lets: can not create lets.yaml: %s", err)
99+
log.Errorf("can not create lets.yaml: %s", err)
100100
return 1
101101
}
102102

@@ -110,7 +110,7 @@ func Main(version string, buildDate string) int {
110110
}
111111

112112
if err != nil {
113-
log.Errorf("lets: can not self-upgrade binary: %s", err)
113+
log.Errorf("can not self-upgrade binary: %s", err)
114114
return 1
115115
}
116116

@@ -121,7 +121,7 @@ func Main(version string, buildDate string) int {
121121

122122
if showUsage {
123123
if err := cmd.PrintRootHelpMessage(rootCmd); err != nil {
124-
log.Errorf("lets: print help error: %s", err)
124+
log.Errorf("print help error: %s", err)
125125
return 1
126126
}
127127

@@ -137,7 +137,7 @@ func Main(version string, buildDate string) int {
137137
executor.PrintDependencyTree(depErr, os.Stderr)
138138
}
139139

140-
log.Errorf("lets: %s", err.Error())
140+
log.Errorf("%s", err.Error())
141141

142142
return getExitCode(err, 1)
143143
}
@@ -161,7 +161,7 @@ func getContext() context.Context {
161161

162162
go func() {
163163
sig := <-ch
164-
log.Printf("lets: signal received: %s", sig)
164+
log.Printf("signal received: %s", sig)
165165
cancel()
166166
}()
167167

@@ -211,7 +211,7 @@ func maybeStartUpdateCheck(
211211
return nil, func() {}
212212
}
213213

214-
log.Debugf("lets: start update check")
214+
log.Debugf("start update check")
215215

216216
notifier, err := upgrade.NewUpdateNotifier(registry.NewGithubRegistry())
217217
if err != nil {
@@ -227,7 +227,7 @@ func maybeStartUpdateCheck(
227227
upgrade.LogUpdateCheckError(err)
228228
}
229229

230-
log.Debugf("lets: update check done")
230+
log.Debugf("update check done")
231231

232232
ch <- updateCheckResult{
233233
notifier: notifier,

internal/config/config/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (c *Command) GetEnv(cfg Config, builtinEnv map[string]string) (map[string]s
162162

163163
envFileEnv, err := envFiles.Load(cfg, filenameEnv)
164164
if err != nil {
165-
return nil, fmt.Errorf("lets: failed to resolve env_file for command '%s': %w", c.Name, err)
165+
return nil, fmt.Errorf("failed to resolve env_file for command '%s': %w", c.Name, err)
166166
}
167167

168168
resolvedEnv := envs.Dump()

internal/config/find.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"path/filepath"
66

7-
"github.com/fatih/color"
87
"github.com/lets-cli/lets/internal/config/path"
98
"github.com/lets-cli/lets/internal/util"
109
"github.com/lets-cli/lets/internal/workdir"
@@ -39,7 +38,7 @@ func FindConfig(configName string, configDir string) (PathInfo, error) {
3938
return PathInfo{}, err
4039
}
4140

42-
log.Debugf("%s", color.BlueString("lets: found %s config file in %s directory", configName, workDir))
41+
log.Debugf("found %s config file in %s directory", configName, workDir)
4342

4443
configAbsPath := ""
4544

internal/logging/formatter.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/fatih/color"
89
log "github.com/sirupsen/logrus"
910
)
1011

@@ -20,13 +21,36 @@ type Formatter struct{}
2021
// Format implements the log.Formatter interface.
2122
func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
2223
buff := &bytes.Buffer{}
23-
buff.WriteString(writeData(entry.Data))
24-
buff.WriteString(entry.Message)
24+
parts := []string{formatPrefix(entry)}
25+
26+
if data := writeData(entry.Data); data != "" {
27+
parts = append(parts, data)
28+
}
29+
30+
parts = append(parts, formatMessage(entry))
31+
32+
buff.WriteString(strings.Join(parts, " "))
2533
buff.WriteString("\n")
2634

2735
return buff.Bytes(), nil
2836
}
2937

38+
func formatPrefix(entry *log.Entry) string {
39+
if entry.Level == log.DebugLevel {
40+
return color.BlueString("lets:")
41+
}
42+
43+
return "lets:"
44+
}
45+
46+
func formatMessage(entry *log.Entry) string {
47+
if entry.Level == log.DebugLevel {
48+
return color.BlueString(entry.Message)
49+
}
50+
51+
return entry.Message
52+
}
53+
3054
func writeData(fields log.Fields) string {
3155
var buff []string
3256

@@ -39,9 +63,5 @@ func writeData(fields log.Fields) string {
3963
}
4064
}
4165

42-
if len(buff) > 0 {
43-
buff = append(buff, "")
44-
}
45-
4666
return strings.Join(buff, " ")
4767
}

internal/logging/log.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,20 @@ func InitLogging(
4141

4242
// ExecLogger is used in Executor.
4343
// If adds command chain in message like this:
44-
// lets: [foo=>bar] message.
44+
// [foo=>bar] message.
4545
type ExecLogger struct {
4646
log *log.Logger
4747
// command name
4848
name string
49-
// lets: [a=>b]
49+
// [a=>b]
5050
prefix string
5151
cache map[string]*ExecLogger
5252
}
5353

5454
func NewExecLogger() *ExecLogger {
5555
return &ExecLogger{
56-
log: log.StandardLogger(),
57-
prefix: color.BlueString("lets:"),
58-
cache: make(map[string]*ExecLogger),
56+
log: log.StandardLogger(),
57+
cache: make(map[string]*ExecLogger),
5958
}
6059
}
6160

@@ -71,19 +70,25 @@ func (l *ExecLogger) Child(name string) *ExecLogger {
7170
l.cache[name] = &ExecLogger{
7271
log: l.log,
7372
name: name,
74-
prefix: color.BlueString("lets: %s", color.GreenString("[%s]", name)),
73+
prefix: color.GreenString("[%s]", name),
7574
cache: make(map[string]*ExecLogger),
7675
}
7776

7877
return l.cache[name]
7978
}
8079

8180
func (l *ExecLogger) Info(format string, a ...any) {
82-
format = fmt.Sprintf("%s %s", l.prefix, color.BlueString(format))
81+
if l.prefix != "" {
82+
format = fmt.Sprintf("%s %s", l.prefix, format)
83+
}
84+
8385
l.log.Logf(log.InfoLevel, format, a...)
8486
}
8587

8688
func (l *ExecLogger) Debug(format string, a ...any) {
87-
format = fmt.Sprintf("%s %s", l.prefix, color.BlueString(format))
89+
if l.prefix != "" {
90+
format = fmt.Sprintf("%s %s", l.prefix, format)
91+
}
92+
8893
l.log.Logf(log.DebugLevel, format, a...)
8994
}

0 commit comments

Comments
 (0)