change charm log to slog

main
jolheiser 2025-02-11 10:04:30 -06:00
parent 53945f5a62
commit 15c0850bab
No known key found for this signature in database
6 changed files with 31 additions and 22 deletions

View File

@ -3,9 +3,9 @@ package main
import (
"flag"
"fmt"
"log/slog"
"strings"
"github.com/charmbracelet/log"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffyaml"
)
@ -51,7 +51,7 @@ type profileLink struct {
}
type logArgs struct {
Level log.Level
Level slog.Level
JSON bool
}
@ -78,14 +78,23 @@ func parseArgs(args []string) (c cliArgs, e error) {
Description: "Minimal git server",
},
Log: logArgs{
Level: log.InfoLevel,
Level: slog.LevelError,
},
}
fs.Func("log.level", "Logging level", func(s string) error {
lvl, err := log.ParseLevel(s)
if err != nil {
return err
var lvl slog.Level
switch strings.ToLower(s) {
case "debug":
lvl = slog.LevelDebug
case "info":
lvl = slog.LevelInfo
case "warn", "warning":
lvl = slog.LevelWarn
case "error":
lvl = slog.LevelError
default:
return fmt.Errorf("unknown log level %q: options are [debug, info, warn, error]", s)
}
c.Log.Level = lvl
return nil

View File

@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
@ -11,7 +12,6 @@ import (
"strconv"
"strings"
"github.com/charmbracelet/log"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v2"
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
@ -39,14 +39,14 @@ func main() {
panic(err)
}
log.SetLevel(args.Log.Level)
slog.SetLogLoggerLevel(args.Log.Level)
middleware.DefaultLogger = httplog.RequestLogger(httplog.NewLogger("ugit", httplog.Options{
JSON: args.Log.JSON,
LogLevel: slog.Level(args.Log.Level),
Concise: args.Log.Level != log.DebugLevel,
Concise: args.Log.Level != slog.LevelDebug,
}))
if args.Log.Level == log.DebugLevel {
if args.Log.Level == slog.LevelDebug {
trace.SetTarget(trace.Packet)
} else {
middleware.DefaultLogger = http.NoopLogger
@ -54,7 +54,8 @@ func main() {
}
if args.Log.JSON {
log.SetFormatter(log.JSONFormatter)
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))
slog.SetDefault(logger)
}
if err := requiredFS(args.RepoDir); err != nil {
@ -74,7 +75,7 @@ func main() {
panic(err)
}
go func() {
log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
log.Printf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
if err := sshSrv.ListenAndServe(); err != nil {
panic(err)
}
@ -102,7 +103,7 @@ func main() {
if args.HTTP.Enable {
httpSrv := http.New(httpSettings)
go func() {
log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
log.Printf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
if err := httpSrv.ListenAndServe(); err != nil {
panic(err)
}

2
go.mod
View File

@ -6,7 +6,6 @@ toolchain go1.23.3
require (
github.com/alecthomas/chroma/v2 v2.15.0
github.com/charmbracelet/log v0.4.0
github.com/charmbracelet/ssh v0.0.0-20241211182756-4fe22b0f1b7c
github.com/charmbracelet/wish v1.4.4
github.com/dustin/go-humanize v1.0.1
@ -31,6 +30,7 @@ require (
github.com/charmbracelet/bubbletea v1.2.4 // indirect
github.com/charmbracelet/keygen v0.5.1 // indirect
github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/charmbracelet/x/ansi v0.6.0 // indirect
github.com/charmbracelet/x/conpty v0.1.0 // indirect
github.com/charmbracelet/x/errors v0.0.0-20250107110353-48b574af22a5 // indirect

View File

@ -2,9 +2,8 @@ package httperr
import (
"errors"
"log/slog"
"net/http"
"github.com/charmbracelet/log"
)
type httpError struct {
@ -41,7 +40,7 @@ func Handler(fn func(w http.ResponseWriter, r *http.Request) error) http.Handler
status = httpErr.status
}
log.Error(err)
slog.Error("httperr Handler error", "error", err)
http.Error(w, http.StatusText(status), status)
}
}

View File

@ -2,8 +2,8 @@ package ssh
import (
"fmt"
"log"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/logging"
@ -42,7 +42,7 @@ func (a hooks) Push(_ string, _ ssh.PublicKey) {}
func (a hooks) Fetch(_ string, _ ssh.PublicKey) {}
var (
DefaultLogger logging.Logger = log.StandardLog()
DefaultLogger logging.Logger = log.Default()
NoopLogger logging.Logger = noopLogger{}
)

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
@ -12,7 +13,6 @@ import (
"go.jolheiser.com/ugit/internal/git"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
)
@ -91,7 +91,7 @@ func Middleware(repoDir string, cloneURL string, port int, gh Hooks) wish.Middle
if errors.Is(err, ErrInvalidRepo) {
Fatal(s, ErrInvalidRepo)
}
log.Error("unknown git error", "error", err)
slog.Error("unknown git error", "error", err)
Fatal(s, ErrSystemMalfunction)
}
gh.Fetch(repo, pk)
@ -103,7 +103,7 @@ func Middleware(repoDir string, cloneURL string, port int, gh Hooks) wish.Middle
if len(cmd) == 0 {
des, err := os.ReadDir(repoDir)
if err != nil && err != fs.ErrNotExist {
log.Error("invalid repository", "error", err)
slog.Error("invalid repository", "error", err)
}
tw := tabwriter.NewWriter(s, 0, 0, 1, ' ', 0)
for _, de := range des {