Skip to content

Commit

Permalink
Added logging with slog
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 10, 2023
1 parent f48b4dc commit 2d47fa7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 45 deletions.
35 changes: 0 additions & 35 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,6 @@
// integrated client.
package cli

import (
"fmt"
"os"
)

var cmdMap map[string]Command = make(map[string]Command)

// AddCommand adds a command using the specific symbol.
func AddCommand(symbol string, cmd Command) {
cmdMap[symbol] = cmd
}

// Session holds all necessary information about the current CLI session.
type Session struct {
}

// Run runs our CLI command, based on the args. We keep it very simple for now
// without any extra package, so we just take the first arg and see if if
// matches any of our commands
func Run(args []string) {
var (
cmd Command
ok bool
s *Session
)

// Create a new session. TODO(oxisto): We do not yet have auth, but in the
// future we need to fetch a token here
s = new(Session)

// Try to look up command in our command map
cmd, ok = cmdMap[args[1]]
if ok {
cmd.Exec(s, os.Args[1:]...)
} else {
fmt.Print("Command not found.\n")
}
}
44 changes: 38 additions & 6 deletions cmd/moneyd/moneyd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,59 @@ package main

import (
"log"
"log/slog"
"net/http"
"os"
"strings"
"time"

"github.com/mattn/go-colorable"
"github.com/alecthomas/kong"
"github.com/oxisto/money-gopher/gen/portfoliov1connect"
"github.com/oxisto/money-gopher/persistence"
"github.com/oxisto/money-gopher/service/portfolio"
"github.com/oxisto/money-gopher/service/securities"

"github.com/lmittmann/tint"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

var cmd moneydCmd

type moneydCmd struct {
Debug bool `help:"Enable debug mode."`
}

func main() {
log.SetPrefix("[🤑] ")
log.SetFlags(log.Lmsgprefix | log.Ltime)
log.Print("Welcome to The Money Gopher")
log.SetOutput(colorable.NewColorableStdout())
ctx := kong.Parse(&cmd)

err := ctx.Run()
ctx.FatalIfErrorf(err)
}

func (cmd *moneydCmd) Run() error {
var w = os.Stdout
var level = slog.LevelInfo
if cmd.Debug {
level = slog.LevelDebug
}

logger := slog.New(
tint.NewHandler(colorable.NewColorable(w), &tint.Options{
TimeFormat: time.TimeOnly,
Level: level,
NoColor: !isatty.IsTerminal(w.Fd()),
}),
)

slog.SetDefault(logger)
slog.Info("Welcome to the Money Gopher")

db, err := persistence.OpenDB(persistence.Options{})
if err != nil {
log.Fatalf("Error while opening database: %v", err)
slog.Error("Error while opening database", tint.Err(err))
}

mux := http.NewServeMux()
Expand All @@ -58,6 +89,7 @@ func main() {
h2c.NewHandler(handleCORS(mux), &http2.Server{}),
)
log.Fatalf("listen failed: %v", err)
return err
}

func handleCORS(h http.Handler) http.Handler {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ require (
github.com/alecthomas/kong v0.8.1
github.com/fatih/color v1.16.0
github.com/jotaen/kong-completion v0.0.6
github.com/lmittmann/tint v1.0.3
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-sqlite3 v1.14.18
github.com/oxisto/assert v0.0.6
github.com/posener/complete v1.2.3
Expand All @@ -17,7 +19,6 @@ require (
)

require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect
golang.org/x/sys v0.15.0 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jotaen/kong-completion v0.0.6 h1:VP1KGvXPeB7MytYR+zZQoWw1gf/HIV1/EvWC38BHZN4=
github.com/jotaen/kong-completion v0.0.6/go.mod h1:fuWw9snL6joY5mXbI0Dd5FWEZODaWXAeqaRxo6dAvLk=
github.com/lmittmann/tint v1.0.3 h1:W5PHeA2D8bBJVvabNfQD/XW9HPLZK1XoPZH0cq8NouQ=
github.com/lmittmann/tint v1.0.3/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
1 change: 1 addition & 0 deletions money-gopher.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"fieldmaskpb",
"headlessui",
"heroicons",
"isatty",
"ISIN",
"jotaen",
"kongcompletion",
Expand Down
10 changes: 9 additions & 1 deletion persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"log"
"log/slog"
"os"
"strings"

Expand All @@ -37,6 +38,13 @@ type Options struct {
DSN string
}

// LogValue implements slog.LogValuer.
func (o Options) LogValue() slog.Value {
return slog.GroupValue(
slog.Bool("in-memory", o.UseInMemory),
slog.String("dsn", o.DSN))
}

// DB is a wrapper around [sql.DB]. This allows us to access all the
// functionalities of [sql.DB] as well as accessing the DB object in our
// internal functions.
Expand Down Expand Up @@ -94,7 +102,7 @@ func OpenDB(opts Options) (db *DB, err error) {
db.log.SetPrefix("[📄] ")
db.initTables()

db.log.Print("Successfully opened database connection")
slog.Info("Successfully opened database connection", "opts", opts)

return
}
Expand Down
13 changes: 11 additions & 2 deletions service/securities/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package securities
import (
"context"
"log"
"log/slog"
"time"

"github.com/lmittmann/tint"
portfoliov1 "github.com/oxisto/money-gopher/gen"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -55,8 +57,15 @@ func (svc *service) TriggerSecurityQuoteUpdate(ctx context.Context, req *connect
}

// Trigger update from quote provider in separate go-routine
for _, ls := range sec.ListedOn {
go svc.updateQuote(qp, ls)
for idx, _ := range sec.ListedOn {
idx := idx
go func() {
ls := sec.ListedOn[idx]
err = svc.updateQuote(qp, ls)
if err != nil {
slog.Error("An error occurred during quote update", tint.Err(err), "ls", ls)

Check warning on line 66 in service/securities/quote.go

View check run for this annotation

Codecov / codecov/patch

service/securities/quote.go#L60-L66

Added lines #L60 - L66 were not covered by tests
}
}()
}
}

Expand Down

0 comments on commit 2d47fa7

Please sign in to comment.