mirror of https://git.jolheiser.com/ugit.git
change charm log to slog
parent
53945f5a62
commit
15c0850bab
|
@ -3,9 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
"github.com/peterbourgon/ff/v3"
|
"github.com/peterbourgon/ff/v3"
|
||||||
"github.com/peterbourgon/ff/v3/ffyaml"
|
"github.com/peterbourgon/ff/v3/ffyaml"
|
||||||
)
|
)
|
||||||
|
@ -51,7 +51,7 @@ type profileLink struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type logArgs struct {
|
type logArgs struct {
|
||||||
Level log.Level
|
Level slog.Level
|
||||||
JSON bool
|
JSON bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,14 +78,23 @@ func parseArgs(args []string) (c cliArgs, e error) {
|
||||||
Description: "Minimal git server",
|
Description: "Minimal git server",
|
||||||
},
|
},
|
||||||
Log: logArgs{
|
Log: logArgs{
|
||||||
Level: log.InfoLevel,
|
Level: slog.LevelError,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.Func("log.level", "Logging level", func(s string) error {
|
fs.Func("log.level", "Logging level", func(s string) error {
|
||||||
lvl, err := log.ParseLevel(s)
|
var lvl slog.Level
|
||||||
if err != nil {
|
switch strings.ToLower(s) {
|
||||||
return err
|
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
|
c.Log.Level = lvl
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -11,7 +12,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/go-chi/httplog/v2"
|
"github.com/go-chi/httplog/v2"
|
||||||
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
|
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
|
||||||
|
@ -39,14 +39,14 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetLevel(args.Log.Level)
|
slog.SetLogLoggerLevel(args.Log.Level)
|
||||||
middleware.DefaultLogger = httplog.RequestLogger(httplog.NewLogger("ugit", httplog.Options{
|
middleware.DefaultLogger = httplog.RequestLogger(httplog.NewLogger("ugit", httplog.Options{
|
||||||
JSON: args.Log.JSON,
|
JSON: args.Log.JSON,
|
||||||
LogLevel: slog.Level(args.Log.Level),
|
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)
|
trace.SetTarget(trace.Packet)
|
||||||
} else {
|
} else {
|
||||||
middleware.DefaultLogger = http.NoopLogger
|
middleware.DefaultLogger = http.NoopLogger
|
||||||
|
@ -54,7 +54,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.Log.JSON {
|
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 {
|
if err := requiredFS(args.RepoDir); err != nil {
|
||||||
|
@ -74,7 +75,7 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
go func() {
|
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 {
|
if err := sshSrv.ListenAndServe(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -102,7 +103,7 @@ func main() {
|
||||||
if args.HTTP.Enable {
|
if args.HTTP.Enable {
|
||||||
httpSrv := http.New(httpSettings)
|
httpSrv := http.New(httpSettings)
|
||||||
go func() {
|
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 {
|
if err := httpSrv.ListenAndServe(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -6,7 +6,6 @@ toolchain go1.23.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alecthomas/chroma/v2 v2.15.0
|
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/ssh v0.0.0-20241211182756-4fe22b0f1b7c
|
||||||
github.com/charmbracelet/wish v1.4.4
|
github.com/charmbracelet/wish v1.4.4
|
||||||
github.com/dustin/go-humanize v1.0.1
|
github.com/dustin/go-humanize v1.0.1
|
||||||
|
@ -31,6 +30,7 @@ require (
|
||||||
github.com/charmbracelet/bubbletea v1.2.4 // indirect
|
github.com/charmbracelet/bubbletea v1.2.4 // indirect
|
||||||
github.com/charmbracelet/keygen v0.5.1 // indirect
|
github.com/charmbracelet/keygen v0.5.1 // indirect
|
||||||
github.com/charmbracelet/lipgloss v1.0.0 // 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/ansi v0.6.0 // indirect
|
||||||
github.com/charmbracelet/x/conpty v0.1.0 // indirect
|
github.com/charmbracelet/x/conpty v0.1.0 // indirect
|
||||||
github.com/charmbracelet/x/errors v0.0.0-20250107110353-48b574af22a5 // indirect
|
github.com/charmbracelet/x/errors v0.0.0-20250107110353-48b574af22a5 // indirect
|
||||||
|
|
|
@ -2,9 +2,8 @@ package httperr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpError struct {
|
type httpError struct {
|
||||||
|
@ -41,7 +40,7 @@ func Handler(fn func(w http.ResponseWriter, r *http.Request) error) http.Handler
|
||||||
status = httpErr.status
|
status = httpErr.status
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Error(err)
|
slog.Error("httperr Handler error", "error", err)
|
||||||
http.Error(w, http.StatusText(status), status)
|
http.Error(w, http.StatusText(status), status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package ssh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
"github.com/charmbracelet/ssh"
|
"github.com/charmbracelet/ssh"
|
||||||
"github.com/charmbracelet/wish"
|
"github.com/charmbracelet/wish"
|
||||||
"github.com/charmbracelet/wish/logging"
|
"github.com/charmbracelet/wish/logging"
|
||||||
|
@ -42,7 +42,7 @@ func (a hooks) Push(_ string, _ ssh.PublicKey) {}
|
||||||
func (a hooks) Fetch(_ string, _ ssh.PublicKey) {}
|
func (a hooks) Fetch(_ string, _ ssh.PublicKey) {}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultLogger logging.Logger = log.StandardLog()
|
DefaultLogger logging.Logger = log.Default()
|
||||||
NoopLogger logging.Logger = noopLogger{}
|
NoopLogger logging.Logger = noopLogger{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,7 +13,6 @@ import (
|
||||||
|
|
||||||
"go.jolheiser.com/ugit/internal/git"
|
"go.jolheiser.com/ugit/internal/git"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
"github.com/charmbracelet/ssh"
|
"github.com/charmbracelet/ssh"
|
||||||
"github.com/charmbracelet/wish"
|
"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) {
|
if errors.Is(err, ErrInvalidRepo) {
|
||||||
Fatal(s, ErrInvalidRepo)
|
Fatal(s, ErrInvalidRepo)
|
||||||
}
|
}
|
||||||
log.Error("unknown git error", "error", err)
|
slog.Error("unknown git error", "error", err)
|
||||||
Fatal(s, ErrSystemMalfunction)
|
Fatal(s, ErrSystemMalfunction)
|
||||||
}
|
}
|
||||||
gh.Fetch(repo, pk)
|
gh.Fetch(repo, pk)
|
||||||
|
@ -103,7 +103,7 @@ func Middleware(repoDir string, cloneURL string, port int, gh Hooks) wish.Middle
|
||||||
if len(cmd) == 0 {
|
if len(cmd) == 0 {
|
||||||
des, err := os.ReadDir(repoDir)
|
des, err := os.ReadDir(repoDir)
|
||||||
if err != nil && err != fs.ErrNotExist {
|
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)
|
tw := tabwriter.NewWriter(s, 0, 0, 1, ' ', 0)
|
||||||
for _, de := range des {
|
for _, de := range des {
|
||||||
|
|
Loading…
Reference in New Issue