Skip to content

Commit 79c3d34

Browse files
committed
feat: Improve logging and proper use in components
1 parent c61a83c commit 79c3d34

6 files changed

Lines changed: 158 additions & 29 deletions

File tree

internal/command/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
gear "github.com/azuyamat/gear/command"
77
"github.com/azuyamat/pace/internal/config"
8+
"github.com/azuyamat/pace/internal/logger"
89
"github.com/azuyamat/pace/internal/runner"
910
)
1011

@@ -27,6 +28,7 @@ func runHandler(ctx *gear.Context, args gear.ValidatedArgs) error {
2728

2829
task, exists := config.GetTaskOrDefault(taskName)
2930
if !exists {
31+
logger.Error("Task '%s' not found", taskName)
3032
return fmt.Errorf("task '%s' not found", taskName)
3133
}
3234

internal/command/update.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
gear "github.com/azuyamat/gear/command"
12+
"github.com/azuyamat/pace/internal/logger"
1213
)
1314

1415
const (
@@ -24,13 +25,13 @@ func init() {
2425
}
2526

2627
func updateHandler(ctx *gear.Context, args gear.ValidatedArgs) error {
27-
fmt.Println("Checking for updates...")
28+
logger.Info("Checking for updates...")
2829

2930
if cmd := getManagedUpdateCommand(); cmd != nil {
3031
return runManagedUpdate(cmd)
3132
}
3233

33-
fmt.Println("Running installation script to update...")
34+
logger.Info("Running installation script to update...")
3435
return runInstallScript()
3536
}
3637

@@ -70,7 +71,7 @@ func getManagedUpdateCommand() *exec.Cmd {
7071
}
7172

7273
func runManagedUpdate(cmd *exec.Cmd) error {
73-
fmt.Printf("Running: %s\n", strings.Join(cmd.Args, " "))
74+
logger.Task("Running: %s", strings.Join(cmd.Args, " "))
7475

7576
cmd.Stdout = os.Stdout
7677
cmd.Stderr = os.Stderr
@@ -80,7 +81,7 @@ func runManagedUpdate(cmd *exec.Cmd) error {
8081
return fmt.Errorf("update command failed: %w", err)
8182
}
8283

83-
fmt.Println("\nUpdate completed successfully!")
84+
logger.Success("Update completed successfully!")
8485
return nil
8586
}
8687

@@ -106,7 +107,7 @@ func runInstallScript() error {
106107
return fmt.Errorf("installation script failed: %w", err)
107108
}
108109

109-
fmt.Println("\nUpdate completed successfully!")
110+
logger.Success("Update completed successfully!")
110111
return nil
111112
}
112113

internal/command/version.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package command
22

33
import (
4-
"fmt"
5-
64
gear "github.com/azuyamat/gear/command"
5+
"github.com/azuyamat/pace/internal/logger"
76
"github.com/azuyamat/pace/internal/version"
87
)
98

@@ -15,8 +14,8 @@ func init() {
1514
}
1615

1716
func versionHandler(ctx *gear.Context, args gear.ValidatedArgs) error {
18-
fmt.Printf("pace version %s\n", version.Version)
19-
fmt.Printf("commit: %s\n", version.Commit)
20-
fmt.Printf("built at: %s\n", version.Date)
17+
logger.Printf("pace version %s\n", version.Version)
18+
logger.Printf("commit: %s\n", version.Commit)
19+
logger.Printf("built at: %s\n", version.Date)
2120
return nil
2221
}

internal/command/watch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
gear "github.com/azuyamat/gear/command"
77
"github.com/azuyamat/pace/internal/config"
8+
"github.com/azuyamat/pace/internal/logger"
89
"github.com/azuyamat/pace/internal/runner"
910
)
1011

@@ -28,6 +29,7 @@ func watchHandler(ctx *gear.Context, args gear.ValidatedArgs) error {
2829

2930
func Watch(cfg *config.Config, args []string) error {
3031
if len(args) < 1 {
32+
logger.Error("No task name provided for watch command")
3133
return fmt.Errorf("no task name provided for watch command")
3234
}
3335

@@ -39,10 +41,12 @@ func Watch(cfg *config.Config, args []string) error {
3941

4042
task, exists := cfg.Tasks[taskName]
4143
if !exists {
44+
logger.Error("Task %q not found in configuration", taskName)
4245
return fmt.Errorf("task %q not found in configuration", taskName)
4346
}
4447

4548
if len(task.Inputs) == 0 {
49+
logger.Warning("Task %q has no inputs defined for watching", taskName)
4650
return fmt.Errorf("task %q has no inputs defined for watching", taskName)
4751
}
4852

internal/logger/color.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package logger
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
type Color string
9+
type BackgroundColor string
10+
11+
const colorPrefix = "\033["
12+
const colorSuffix = "m"
13+
14+
const (
15+
ColorReset Color = colorPrefix + "0" + colorSuffix
16+
ColorRed Color = colorPrefix + "31" + colorSuffix
17+
ColorGreen Color = colorPrefix + "32" + colorSuffix
18+
ColorYellow Color = colorPrefix + "33" + colorSuffix
19+
ColorBlue Color = colorPrefix + "34" + colorSuffix
20+
ColorPurple Color = colorPrefix + "35" + colorSuffix
21+
ColorCyan Color = colorPrefix + "36" + colorSuffix
22+
ColorWhite Color = colorPrefix + "37" + colorSuffix
23+
ColorGray Color = colorPrefix + "90" + colorSuffix
24+
)
25+
26+
func (c Color) Wrap(text string) string {
27+
return string(ColorReset) + string(c) + text + string(ColorReset)
28+
}
29+
30+
func (c Color) Background() BackgroundColor {
31+
return BackgroundColor(c.transform(func(code int) int { return code + 10 }))
32+
}
33+
34+
func (c Color) Bright() Color {
35+
return c.transform(func(code int) int {
36+
if code >= 30 && code <= 37 {
37+
return code + 60
38+
}
39+
return code
40+
})
41+
}
42+
43+
func (c Color) Dark() Color {
44+
return c.transform(func(code int) int {
45+
if code >= 90 && code <= 97 {
46+
return code - 60
47+
}
48+
return code
49+
})
50+
}
51+
52+
func (c Color) Dim() Color {
53+
if c == ColorReset {
54+
return ColorReset
55+
}
56+
s := string(c)
57+
s = strings.TrimPrefix(s, colorPrefix)
58+
s = strings.TrimSuffix(s, colorSuffix)
59+
return Color(colorPrefix + s + ";2" + colorSuffix)
60+
}
61+
62+
func (c Color) transform(fn func(int) int) Color {
63+
if c == ColorReset {
64+
return ColorReset
65+
}
66+
67+
s := string(c)
68+
s = strings.TrimPrefix(s, colorPrefix)
69+
s = strings.TrimSuffix(s, colorSuffix)
70+
71+
code, err := strconv.Atoi(s)
72+
if err != nil {
73+
return ColorReset
74+
}
75+
76+
return Color(colorPrefix + strconv.Itoa(fn(code)) + colorSuffix)
77+
}
78+
79+
func (b BackgroundColor) Wrap(text string) string {
80+
return string(ColorReset) + string(b) + text + string(ColorReset)
81+
}
82+
83+
func (b BackgroundColor) Bright() BackgroundColor {
84+
return BackgroundColor(transformBackgroundCode(string(b), func(code int) int {
85+
if code >= 40 && code <= 47 {
86+
return code + 60
87+
}
88+
return code
89+
}))
90+
}
91+
92+
func (b BackgroundColor) Dark() BackgroundColor {
93+
return BackgroundColor(transformBackgroundCode(string(b), func(code int) int {
94+
if code >= 100 && code <= 107 {
95+
return code - 60
96+
}
97+
return code
98+
}))
99+
}
100+
101+
func transformBackgroundCode(color string, fn func(int) int) string {
102+
s := strings.TrimPrefix(color, colorPrefix)
103+
s = strings.TrimSuffix(s, colorSuffix)
104+
105+
code, err := strconv.Atoi(s)
106+
if err != nil {
107+
return string(ColorReset)
108+
}
109+
110+
return colorPrefix + strconv.Itoa(fn(code)) + colorSuffix
111+
}

internal/logger/logger.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import (
66
"time"
77
)
88

9-
const (
10-
colorReset = "\033[0m"
11-
colorRed = "\033[31m"
12-
colorGreen = "\033[32m"
13-
colorYellow = "\033[33m"
14-
colorBlue = "\033[34m"
15-
colorPurple = "\033[35m"
16-
colorCyan = "\033[36m"
17-
colorGray = "\033[90m"
18-
colorWhite = "\033[97m"
19-
)
20-
219
type LogLevel int
2210

2311
const (
@@ -44,55 +32,79 @@ func (l *Logger) SetEnabled(enabled bool) {
4432
}
4533

4634
func (l *Logger) timestamp() string {
47-
return colorGray + time.Now().Format("15:04:05") + colorReset
35+
timeStr := time.Now().Format("15:04:05")
36+
return ColorGray.Wrap("[" + timeStr + "]")
37+
}
38+
39+
func (l *Logger) badge(text string, color Color) string {
40+
bg := color.Dark().Background()
41+
return bg.Wrap(" " + text + " ")
4842
}
4943

5044
func (l *Logger) Info(format string, args ...interface{}) {
5145
if !l.enabled || l.level > LevelInfo {
5246
return
5347
}
5448
msg := fmt.Sprintf(format, args...)
55-
fmt.Printf("%s %s%s%s\n", l.timestamp(), colorCyan, msg, colorReset)
49+
badge := l.badge("INFO", ColorBlue)
50+
icon := ColorCyan.Wrap("◆")
51+
coloredMsg := ColorWhite.Wrap(msg)
52+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
5653
}
5754

5855
func (l *Logger) Success(format string, args ...interface{}) {
5956
if !l.enabled || l.level > LevelInfo {
6057
return
6158
}
6259
msg := fmt.Sprintf(format, args...)
63-
fmt.Printf("%s %s✓%s %s\n", l.timestamp(), colorGreen, colorReset, msg)
60+
badge := l.badge("DONE", ColorGreen)
61+
icon := ColorGreen.Bright().Wrap("✓")
62+
coloredMsg := ColorWhite.Wrap(msg)
63+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
6464
}
6565

6666
func (l *Logger) Error(format string, args ...interface{}) {
6767
if !l.enabled || l.level > LevelError {
6868
return
6969
}
7070
msg := fmt.Sprintf(format, args...)
71-
fmt.Printf("%s %s✗%s %s\n", l.timestamp(), colorRed, colorReset, msg)
71+
badge := l.badge("ERROR", ColorRed)
72+
icon := ColorRed.Bright().Wrap("✗")
73+
coloredMsg := ColorWhite.Wrap(msg)
74+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
7275
}
7376

7477
func (l *Logger) Warning(format string, args ...interface{}) {
7578
if !l.enabled || l.level > LevelWarning {
7679
return
7780
}
7881
msg := fmt.Sprintf(format, args...)
79-
fmt.Printf("%s %s⚠%s %s\n", l.timestamp(), colorYellow, colorReset, msg)
82+
badge := l.badge("WARN", ColorYellow)
83+
icon := ColorYellow.Bright().Wrap("⚠")
84+
coloredMsg := ColorWhite.Wrap(msg)
85+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
8086
}
8187

8288
func (l *Logger) Task(format string, args ...interface{}) {
8389
if !l.enabled || l.level > LevelInfo {
8490
return
8591
}
8692
msg := fmt.Sprintf(format, args...)
87-
fmt.Printf("%s %s▶%s %s\n", l.timestamp(), colorBlue, colorReset, msg)
93+
badge := l.badge("TASK", ColorPurple)
94+
icon := ColorPurple.Bright().Wrap("▶")
95+
coloredMsg := ColorWhite.Wrap(msg)
96+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
8897
}
8998

9099
func (l *Logger) Debug(format string, args ...interface{}) {
91100
if !l.enabled || l.level != LevelDebug {
92101
return
93102
}
94103
msg := fmt.Sprintf(format, args...)
95-
fmt.Printf("%s %s[DEBUG]%s %s\n", l.timestamp(), colorGray, colorReset, msg)
104+
badge := l.badge("DEBUG", ColorGray)
105+
icon := ColorGray.Wrap("●")
106+
coloredMsg := ColorGray.Wrap(msg)
107+
fmt.Printf("%s %s %s %s\n", l.timestamp(), badge, icon, coloredMsg)
96108
}
97109

98110
func (l *Logger) Print(format string, args ...interface{}) {

0 commit comments

Comments
 (0)