Skip to content

Commit

Permalink
feat: prettier console output (#27)
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
agaffney authored Feb 18, 2024
1 parent 7cdd25f commit 8cc4ac3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
10 changes: 7 additions & 3 deletions cmd/cardano-up/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"log/slog"
"os"

"github.com/blinklabs-io/cardano-up/internal/consolelog"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -47,9 +49,11 @@ func main() {
if globalFlags.debug {
logLevel = slog.LevelDebug
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: logLevel,
}))
logger := slog.New(
consolelog.NewHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}),
)
slog.SetDefault(logger)
},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cardano-up/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"log/slog"

"github.com/blinklabs-io/cardano-up/internal/version"
"github.com/spf13/cobra"
Expand All @@ -26,7 +27,7 @@ func versionCommand() *cobra.Command {
Use: "version",
Short: "Displays the version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s %s\n", programName, version.GetVersionString())
slog.Info(fmt.Sprintf("%s %s", programName, version.GetVersionString()))
},
}
}
77 changes: 77 additions & 0 deletions internal/consolelog/consolelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package consolelog

import (
"context"
"fmt"
"io"
"log/slog"
)

const (
colorBrightRed = 91
colorBrightYellow = 93
colorBrightMagenta = 95
)

type Handler struct {
h slog.Handler
out io.Writer
}

func NewHandler(out io.Writer, opts *slog.HandlerOptions) *Handler {
if opts == nil {
opts = &slog.HandlerOptions{}
}
return &Handler{
out: out,
h: slog.NewTextHandler(out, &slog.HandlerOptions{
Level: opts.Level,
}),
}
}

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return h.h.Enabled(ctx, level)
}

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &Handler{h: h.h.WithAttrs(attrs)}
}

func (h *Handler) WithGroup(name string) slog.Handler {
return &Handler{h: h.h.WithGroup(name)}
}

func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
var levelTag string
switch r.Level {
case slog.LevelDebug:
levelTag = fmt.Sprintf("\033[%dmDEBUG:\033[0m ", colorBrightMagenta)
case slog.LevelInfo:
// No tag for INFO
levelTag = ""
case slog.LevelWarn:
levelTag = fmt.Sprintf("\033[%dmWARNING:\033[0m ", colorBrightYellow)
case slog.LevelError:
levelTag = fmt.Sprintf("\033[%dmERROR:\033[0m ", colorBrightRed)
}
msg := levelTag + r.Message + "\n"
if _, err := h.out.Write([]byte(msg)); err != nil {
return err
}
return nil
}

0 comments on commit 8cc4ac3

Please sign in to comment.