/
logging.go
52 lines (46 loc) · 1.19 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package logging
import (
"fmt"
"time"
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
"github.com/goreleaser/goreleaser/internal/middleware"
"github.com/goreleaser/goreleaser/pkg/context"
)
var (
bold = lipgloss.NewStyle().Bold(true)
faint = lipgloss.NewStyle().Italic(true).Faint(true)
)
// Log pretty prints the given action and its title.
func Log(title string, next middleware.Action) middleware.Action {
return func(ctx *context.Context) error {
start := time.Now()
defer func() {
logDuration(start)
log.ResetPadding()
}()
log.Infof(bold.Render(title))
log.IncreasePadding()
return next(ctx)
}
}
// PadLog pretty prints the given action and its title with an increased padding.
func PadLog(title string, next middleware.Action) middleware.Action {
return func(ctx *context.Context) error {
start := time.Now()
defer func() {
logDuration(start)
log.ResetPadding()
}()
log.ResetPadding()
log.IncreasePadding()
log.Infof(bold.Render(title))
log.IncreasePadding()
return next(ctx)
}
}
func logDuration(start time.Time) {
if took := time.Since(start).Round(time.Second); took > 0 {
log.Info(faint.Render(fmt.Sprintf("took: %s", took)))
}
}