Skip to content

Commit

Permalink
build(mage): ✨ improve logging
Browse files Browse the repository at this point in the history
- use tint for colourful logging
- add more attributes for information
  • Loading branch information
joshuar committed Oct 3, 2024
1 parent beb02e1 commit a6256c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion build/magefiles/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log/slog"
"os"
"path/filepath"
"runtime"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -80,9 +81,10 @@ func buildProject() error {
// Set an appropriate output file based on the arch to build for.
outputFile := filepath.Join(distPath, "/go-hass-agent-"+envMap["PLATFORMPAIR"])

//nolint:sloglint
slog.Info("Running go build...",
slog.String("output", outputFile),
slog.String("ldflags", ldflags))
slog.String("build.host", runtime.GOARCH))

// Run the build.
if err := sh.RunWithV(envMap, "go", "build", "-ldflags="+ldflags, "-o", outputFile); err != nil {
Expand Down
5 changes: 1 addition & 4 deletions build/magefiles/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ func generateEnv() (map[string]string, error) {
_, arch, ver := parseBuildPlatform()

if arch != "" && arch != runtime.GOARCH {
slog.Info("Cross compilation requested.",
slog.String("host", runtime.GOARCH),
slog.String("target", arch))

slog.Info("Setting up cross-compilation.")
// Update NFPM_ARCH to the target arch.
envMap["NFPM_ARCH"] = arch + ver

Expand Down
28 changes: 27 additions & 1 deletion build/magefiles/mage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"errors"
"log/slog"
"os"
"time"

"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
)

const (
Expand All @@ -22,6 +26,28 @@ var (
)

func init() {
version, _ := getVersion() //nolint:errcheck
hash, _ := getGitHash() //nolint:errcheck

platform, arch, ver := parseBuildPlatform()

// set global logger with custom options
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil)))
logger := slog.New(tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.Kitchen,
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
})).
With(
slog.Group("git",
slog.String("version", version),
slog.String("hash", hash),
),
slog.Group("build",
slog.String("os", platform),
slog.String("arch", arch),
slog.String("revision", ver),
),
)

slog.SetDefault(logger)
}
2 changes: 1 addition & 1 deletion build/magefiles/preps.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (Preps) Generate() error {

slog.Info("Running go generate...")

if err := sh.RunWithV(envMap, "go", "generate", "-v", "./..."); err != nil {
if err := sh.RunWithV(envMap, "go", "generate", "./..."); err != nil {
return fmt.Errorf("failed to run go generate: %w", err)
}

Expand Down

0 comments on commit a6256c1

Please sign in to comment.