Skip to content

Commit

Permalink
feat(gnobro): add json log format (#2812)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored Sep 19, 2024
1 parent 01ee5a9 commit f87ba5d
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions contribs/gnodev/cmd/gnobro/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"log/slog"
"net"
"net/url"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/wish/logging"
"golang.org/x/sync/errgroup"

"github.com/gnolang/gno/contribs/gnodev/pkg/browser"
Expand All @@ -47,6 +47,7 @@ type broCfg struct {
sshListener string
sshHostKeyPath string
banner bool
jsonlog bool
}

var defaultBroOptions = broCfg{
Expand Down Expand Up @@ -152,6 +153,13 @@ func (c *broCfg) RegisterFlags(fs *flag.FlagSet) {
defaultBroOptions.readonly,
"readonly mode, no commands allowed",
)

fs.BoolVar(
&c.jsonlog,
"jsonlog",
defaultBroOptions.jsonlog,
"display server log as json format",
)
}

func execBrowser(cfg *broCfg, args []string, cio commands.IO) error {
Expand Down Expand Up @@ -277,9 +285,7 @@ func runLocal(ctx context.Context, gnocl *gnoclient.Client, cfg *broCfg, bcfg br

func runServer(ctx context.Context, gnocl *gnoclient.Client, cfg *broCfg, bcfg browser.Config, io commands.IO) error {
// setup logger
charmlogger := charmlog.New(io.Out())
charmlogger.SetLevel(charmlog.DebugLevel)
logger := slog.New(charmlogger)
logger := newLogger(io.Out(), cfg.jsonlog)

teaHandler := func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
shortid := fmt.Sprintf("%.10s", s.Context().SessionID())
Expand Down Expand Up @@ -326,8 +332,8 @@ func runServer(ctx context.Context, gnocl *gnoclient.Client, cfg *broCfg, bcfg b
bubbletea.Middleware(teaHandler),
activeterm.Middleware(), // ensure PTY
ValidatePathCommandMiddleware(bcfg.URLPrefix),
logging.StructuredMiddlewareWithLogger(
charmlogger, charmlog.DebugLevel,
StructuredMiddlewareWithLogger(
ctx, logger, slog.LevelInfo,
),
// XXX: add ip throttler
),
Expand Down Expand Up @@ -358,7 +364,9 @@ func runServer(ctx context.Context, gnocl *gnoclient.Client, cfg *broCfg, bcfg b
return err
}

io.Println("Bye!")
if !cfg.jsonlog {
io.Println("Bye!")
}
return nil
}

Expand Down Expand Up @@ -460,3 +468,47 @@ func ValidatePathCommandMiddleware(pathPrefix string) wish.Middleware {
}
}
}

func StructuredMiddlewareWithLogger(ctx context.Context, logger *slog.Logger, level slog.Level) wish.Middleware {
return func(next ssh.Handler) ssh.Handler {
return func(sess ssh.Session) {
ct := time.Now()
hpk := sess.PublicKey() != nil
pty, _, _ := sess.Pty()
logger.Log(
ctx,
level,
"connect",
"user", sess.User(),
"remote-addr", sess.RemoteAddr().String(),
"public-key", hpk,
"command", sess.Command(),
"term", pty.Term,
"width", pty.Window.Width,
"height", pty.Window.Height,
"client-version", sess.Context().ClientVersion(),
)
next(sess)
logger.Log(
ctx,
level,
"disconnect",
"user", sess.User(),
"remote-addr", sess.RemoteAddr().String(),
"duration", time.Since(ct),
)
}
}
}

func newLogger(out io.Writer, json bool) *slog.Logger {
if json {
return slog.New(slog.NewJSONHandler(out, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
}

charmlogger := charmlog.New(out)
charmlogger.SetLevel(charmlog.DebugLevel)
return slog.New(charmlogger)
}

0 comments on commit f87ba5d

Please sign in to comment.