Skip to content

Commit

Permalink
elite logging (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-rash committed Apr 16, 2024
1 parent 2e16b86 commit 26f279d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jordan-rash/slog-handler v0.0.0-20240416152541-5a6f90904449 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/lightstep/tracecontext.go v0.0.0-20181129014701-1757c391b1ac // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
Expand Down Expand Up @@ -134,7 +135,7 @@ require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
golang.org/x/tools v0.16.1 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVf
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jordan-rash/firecracker-go-sdk v0.0.0-20240124162534-a5295226c294 h1:gu6Xab0e7EXz3yYRNM8FsU0QmYeWkxjotPcHT+c+t3w=
github.com/jordan-rash/firecracker-go-sdk v0.0.0-20240124162534-a5295226c294/go.mod h1:pcsIXRGgbFGr9QtUdlQCP/z6tuB7EMw6zXgFkcu7Q0c=
github.com/jordan-rash/slog-handler v0.0.0-20240329211950-0b604598c8db h1:PjeQk8zGRwWFOIqIK0kKABjdsre1xs/pMtP1hsfG7WM=
github.com/jordan-rash/slog-handler v0.0.0-20240329211950-0b604598c8db/go.mod h1:qAvCEsGWcbpvC1TrobMyjw7CKr5P5flNnA3wnZ20yuw=
github.com/jordan-rash/slog-handler v0.0.0-20240416151542-3c7bb1abb435 h1:54U713/CzIBH1lwDY079nfR2lhWwXJLASxoTZb7u03I=
github.com/jordan-rash/slog-handler v0.0.0-20240416151542-3c7bb1abb435/go.mod h1:teeFXg8aOsombwXerVrB8Id0bkaPpZ+QuK6V6NYZW0c=
github.com/jordan-rash/slog-handler v0.0.0-20240416152541-5a6f90904449 h1:vve37iyU8masdeQVKKcQZLcclL9BHDiLOtf3W26h/LM=
github.com/jordan-rash/slog-handler v0.0.0-20240416152541-5a6f90904449/go.mod h1:teeFXg8aOsombwXerVrB8Id0bkaPpZ+QuK6V6NYZW0c=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down Expand Up @@ -299,6 +305,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand Down Expand Up @@ -618,6 +626,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
4 changes: 4 additions & 0 deletions internal/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type Options struct {
// LogLevel is the log level to use
LogLevel string
// LogJSON enables JSON logging
LogTimeFormat string
// Timeformat for logs. Must satisfy time.Time format criteria
LogsColorized bool
// Outputs logs with color
LogJSON bool
// Name or path to a configuration context
ConfigurationContext string
Expand Down
57 changes: 29 additions & 28 deletions nex/nex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"log/slog"
"os"
"strconv"
"time"

"github.com/choria-io/fisk"
"github.com/fatih/color"
shandler "github.com/jordan-rash/slog-handler"
"github.com/synadia-io/nex/internal/models"
nextui "github.com/synadia-io/nex/nex/tui"
)
Expand All @@ -18,11 +20,6 @@ var (
COMMIT = ""
BUILDDATE = ""

LevelTrace = slog.Level(-8)
LevelNames = map[slog.Leveler]string{
LevelTrace: "TRACE",
}

blue = color.New(color.FgBlue).SprintFunc()

ncli = fisk.New("nex", fmt.Sprintf("%s\nNATS Execution Engine CLI Version %s\n", blue(Banner), VERSION))
Expand Down Expand Up @@ -74,8 +71,10 @@ func init() {
ncli.Flag("tlsfirst", "Perform TLS handshake before expecting the server greeting").BoolVar(&Opts.TlsFirst)
ncli.Flag("timeout", "Time to wait on responses from NATS").Default("2s").Envar("NATS_TIMEOUT").PlaceHolder("DURATION").DurationVar(&Opts.Timeout)
ncli.Flag("namespace", "Scoping namespace for applicable operations").Default("default").Envar("NEX_NAMESPACE").StringVar(&Opts.Namespace)
ncli.Flag("loglevel", "Log level").Default("info").Envar("NEX_LOGLEVEL").EnumVar(&Opts.LogLevel, "trace", "debug", "info", "warn", "error")
ncli.Flag("loglevel", "Log level").Default("info").Envar("NEX_LOGLEVEL").EnumVar(&Opts.LogLevel, "debug", "info", "warn", "error")
ncli.Flag("logjson", "Log JSON").Default("false").Envar("NEX_LOGJSON").UnNegatableBoolVar(&Opts.LogJSON)
ncli.Flag("logcolor", "Prints text logs with color").Envar("NEX_LOG_COLORIZED").Default("false").UnNegatableBoolVar(&Opts.LogsColorized)
ncli.Flag("timeformat", "How time is formatted in logger").Envar("NEX_LOG_TIMEFORMAT").Default("DateTime").EnumVar(&Opts.LogTimeFormat, "DateOnly", "DateTime", "Stamp", "RFC822", "RFC3339")
ncli.Flag("context", "Configuration context").Envar("NATS_CONTEXT").PlaceHolder("NAME").StringVar(&Opts.ConfigurationContext)
ncli.Flag("no-context", "Disable NATS context discovery").UnNegatableBoolVar(&Opts.SkipContexts)
ncli.Flag("conn-name", "Name of NATS connection").Default(func() string {
Expand Down Expand Up @@ -125,39 +124,41 @@ func main() {
cmd := fisk.MustParse(ncli.Parse(os.Args[1:]))

ctx := context.Background()
opts := slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
levelLabel, exists := LevelNames[level]
if !exists {
levelLabel = level.String()
}
a.Value = slog.StringValue(levelLabel)
}
return a
},
}

var handlerOpts []shandler.HandlerOption
switch Opts.LogLevel {
case "debug":
opts.Level = slog.LevelDebug
handlerOpts = append(handlerOpts, shandler.WithLogLevel(slog.LevelDebug))
case "info":
opts.Level = slog.LevelInfo
handlerOpts = append(handlerOpts, shandler.WithLogLevel(slog.LevelInfo))
case "warn":
opts.Level = slog.LevelWarn
case "trace":
opts.Level = LevelTrace
handlerOpts = append(handlerOpts, shandler.WithLogLevel(slog.LevelWarn))
default:
handlerOpts = append(handlerOpts, shandler.WithLogLevel(slog.LevelError))
}

switch Opts.LogTimeFormat {
case "DateOnly":
handlerOpts = append(handlerOpts, shandler.WithTimeFormat(time.DateOnly))
case "Stamp":
handlerOpts = append(handlerOpts, shandler.WithTimeFormat(time.Stamp))
case "RFC822":
handlerOpts = append(handlerOpts, shandler.WithTimeFormat(time.RFC822))
case "RFC3339":
handlerOpts = append(handlerOpts, shandler.WithTimeFormat(time.RFC3339))
default:
opts.Level = slog.LevelError
handlerOpts = append(handlerOpts, shandler.WithTimeFormat(time.DateTime))
}

var logger *slog.Logger
if Opts.LogJSON {
logger = slog.New(slog.NewJSONHandler(os.Stdout, &opts))
} else {
logger = slog.New(slog.NewTextHandler(os.Stdout, &opts))
handlerOpts = append(handlerOpts, shandler.WithJSON())
}
if Opts.LogsColorized {
handlerOpts = append(handlerOpts, shandler.WithColor())
}

logger = slog.New(shandler.NewHandler(handlerOpts...))

switch cmd {
case tui.FullCommand():
Expand Down

0 comments on commit 26f279d

Please sign in to comment.