Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nint8835 committed Aug 24, 2023
1 parent c24a160 commit a9cfa5d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"fmt"
"log/slog"

"github.com/spf13/cobra"
"github.com/yuin/goldmark"
Expand All @@ -27,6 +28,8 @@ var exampleCmd = &cobra.Command{

fmt.Println(buf.String())

slog.Info("This is a log message")

return nil
},
}
Expand Down
80 changes: 80 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,96 @@ package cmd

import (
"fmt"
"log/slog"
"os"
"strings"
"time"

"github.com/lmittmann/tint"
"github.com/spf13/cobra"
"golang.org/x/term"
)

var logLevelString string
var enableRichLogs bool
var disableRichLogs bool

var rootCmd = &cobra.Command{
Use: "almanac",
// TODO: Add description
}

func init() {
rootCmd.PersistentFlags().StringVar(&logLevelString, "log-level", "info", "Log level (debug, info, warn, error)")
_ = rootCmd.RegisterFlagCompletionFunc(
"log-level",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var matching []string

for _, level := range []string{"debug", "info", "warn", "error"} {
if strings.HasPrefix(level, toComplete) {
matching = append(matching, level)
}
}

return matching, cobra.ShellCompDirectiveNoFileComp
},
)

rootCmd.PersistentFlags().BoolVar(
&enableRichLogs,
"enable-rich-logs",
false,
"Enable rich logs, even in non-terminal environments",
)
rootCmd.PersistentFlags().BoolVar(
&disableRichLogs,
"disable-rich-logs",
false,
"Disable rich logs, even in terminal environments",
)

cobra.OnInitialize(setupLogging)
}

func setupLogging() {
level, validLevel := map[string]slog.Level{
"debug": slog.LevelDebug,
"info": slog.LevelInfo,
"warn": slog.LevelWarn,
"error": slog.LevelError,
}[strings.ToLower(logLevelString)]

if !validLevel {
slog.Error(
"Invalid log level",
"log-level", logLevelString,
)
os.Exit(1)
}

if (term.IsTerminal(int(os.Stderr.Fd())) && !disableRichLogs) || enableRichLogs {
slog.SetDefault(slog.New(
tint.NewHandler(
os.Stderr,
&tint.Options{
TimeFormat: time.Kitchen,
Level: level,
},
),
))
} else {
slog.SetDefault(slog.New(
slog.NewJSONHandler(
os.Stderr,
&slog.HandlerOptions{
Level: level,
},
),
))
}
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module github.com/fogo-sh/almanac
go 1.21.0

require (
github.com/lmittmann/tint v1.0.0
github.com/spf13/cobra v1.7.0
github.com/yuin/goldmark v1.5.6
golang.org/x/term v0.11.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.11.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lmittmann/tint v1.0.0 h1:fzEj70K1L58uyoePQxKe+ezDZJ5pybiWGdA0JeFvvyw=
github.com/lmittmann/tint v1.0.0/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.5.6 h1:COmQAWTCcGetChm3Ig7G/t8AFAN00t+o8Mt4cf7JpwA=
github.com/yuin/goldmark v1.5.6/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit a9cfa5d

Please sign in to comment.