Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnobro): add json log format #2812

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
thehowl marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading