Skip to content

Commit

Permalink
json logging and ironing kinks
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 18, 2024
1 parent 2fc218d commit 39f7473
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
var (
TraceFile string
LogFile string
JsonHandler bool
Verbose bool
AccessibleMode = (os.Getenv("ACCESSIBLE") != "" && os.Getenv("ACCESSIBLE") != "0")

Expand Down Expand Up @@ -103,6 +104,7 @@ const (
func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.StringVar(&TraceFile, "trace", os.Getenv("TRACE_FILE"), "trace `filename`")
fs.StringVar(&LogFile, "log", os.Getenv("LOG_FILE"), "log `file`, if not specified, messages are printed to STDERR")
fs.BoolVar(&JsonHandler, "log-json", osenv.Value("JSON_LOG", false), "log in JSON format")
fs.BoolVar(&Verbose, "v", osenv.Value("DEBUG", false), "verbose messages")

if mask&OmitAuthFlags == 0 {
Expand Down
42 changes: 24 additions & 18 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func invoke(cmd *base.Command, args []string) error {
defer task.End()

// initialise default logging.
if lg, err := initLog(cfg.LogFile, cfg.Verbose); err != nil {
if lg, err := initLog(cfg.LogFile, cfg.JsonHandler, cfg.Verbose); err != nil {
return err
} else {
lg.With("command", cmd.Name())
Expand Down Expand Up @@ -247,30 +247,36 @@ func initTrace(filename string) error {
// an error, if any. The stop function must be called in the deferred call, it
// will close the log file, if it is open. If the error is returned the stop
// function is nil.
func initLog(filename string, verbose bool) (*slog.Logger, error) {
func initLog(filename string, jsonHandler bool, verbose bool) (*slog.Logger, error) {
if verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

if filename == "" {
return slog.Default(), nil
var opts = &slog.HandlerOptions{
Level: iftrue(verbose, slog.LevelDebug, slog.LevelInfo),
}

slog.Debug("log messages will be written to file", "filename", filename)
lf, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return slog.Default(), fmt.Errorf("failed to create the log file: %w", err)
if jsonHandler {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, opts)))
}
sl := slog.New(slog.NewTextHandler(lf, &slog.HandlerOptions{
Level: iftrue(verbose, slog.LevelDebug, slog.LevelInfo),
}))
slog.SetDefault(sl)
if filename != "" {
slog.Debug("log messages will be written to file", "filename", filename)
lf, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return slog.Default(), fmt.Errorf("failed to create the log file: %w", err)
}

base.AtExit(func() {
if err := lf.Close(); err != nil {
dlog.Printf("failed to close the log file: %s", err)
var h slog.Handler = slog.NewTextHandler(lf, opts)
if jsonHandler {
h = slog.NewJSONHandler(lf, opts)
}
})

sl := slog.New(h)
slog.SetDefault(sl)
base.AtExit(func() {
if err := lf.Close(); err != nil {
dlog.Printf("failed to close the log file: %s", err)
}
})
}

return slog.Default(), nil
}
Expand Down
8 changes: 8 additions & 0 deletions internal/structures/url_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package structures

import (
"fmt"
"log/slog"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -32,6 +33,13 @@ func (u SlackLink) IsThread() bool {
return u.ThreadTS != ""
}

func (u SlackLink) LogValue() slog.Value {
if u.ThreadTS == "" {
return slog.GroupValue(slog.Group("channel", slog.String("id", u.Channel)))
}
return slog.GroupValue(slog.Group("thread", slog.String("channel_id", u.Channel), slog.String("thread_ts", u.ThreadTS)))
}

func (u SlackLink) IsValid() bool {
return u.Channel != "" || (u.Channel != "" && u.ThreadTS != "")
}
Expand Down
4 changes: 2 additions & 2 deletions stream/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func (cs *Stream) thread(ctx context.Context, sl *structures.SlackLink, callback
return fmt.Errorf("not a thread: %s", sl)
}

lg := slog.With("slack_link", sl.String())
lg.DebugContext(ctx, "- getting", sl)
lg := slog.With("slack_link", sl)
lg.DebugContext(ctx, "- getting")

var cursor string
for {
Expand Down

0 comments on commit 39f7473

Please sign in to comment.