ugit @main -
refs -
log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
feat: multiple log levels and json logging
Signed-off-by: jolheiser <git@jolheiser.com>
Signature
-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgBTEvCQk6VqUAdN2RuH6bj1dNkY
oOpbPWj+jw4ua1B1cAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
AAAAQCJYXWhtp7EamzqF/ZD0Z1OnOjHdInCNX5EuU1rqvQJaFt62LDIalhGSFG2vf2qWBa
KSlfcJ/nHdR66GHi6JRQg=
-----END SSH SIGNATURE-----
5 changed files, 42 additions(+), 17 deletions(-)
diff --git a/cmd/ugitd/args.go b/cmd/ugitd/args.go
index a1a0e46ffecc4167880a9d9a77c26d8338d56d4f..32f682cb7eea8595baac966014d69e5b9c36863d 100644
--- a/cmd/ugitd/args.go
+++ b/cmd/ugitd/args.go
@@ -5,17 +5,18 @@ "flag"
"fmt"
"strings"
+ "github.com/charmbracelet/log"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffyaml"
)
type cliArgs struct {
- Debug bool
RepoDir string
SSH sshArgs
HTTP httpArgs
Meta metaArgs
Profile profileArgs
+ Log logArgs
}
type sshArgs struct {
@@ -46,6 +47,11 @@ Name string
URL string
}
+type logArgs struct {
+ Level log.Level
+ JSON bool
+}
+
func parseArgs(args []string) (c cliArgs, e error) {
fs := flag.NewFlagSet("ugitd", flag.ContinueOnError)
fs.String("config", "ugit.yaml", "Path to config file")
@@ -65,10 +71,21 @@ },
Meta: metaArgs{
Title: "ugit",
Description: "Minimal git server",
+ },
+ Log: logArgs{
+ Level: log.InfoLevel,
},
}
- fs.BoolVar(&c.Debug, "debug", c.Debug, "Debug logging")
+ fs.Func("log.level", "Logging level", func(s string) error {
+ lvl, err := log.ParseLevel(s)
+ if err != nil {
+ return err
+ }
+ c.Log.Level = lvl
+ return nil
+ })
+ fs.BoolVar(&c.Log.JSON, "log.json", c.Log.JSON, "Print logs in JSON(L) format")
fs.StringVar(&c.RepoDir, "repo-dir", c.RepoDir, "Path to directory containing repositories")
fs.StringVar(&c.SSH.AuthorizedKeys, "ssh.authorized-keys", c.SSH.AuthorizedKeys, "Path to authorized_keys")
fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base")
diff --git a/cmd/ugitd/main.go b/cmd/ugitd/main.go
index e7fab794e4e55915ced861e8b15647ca0bcf36e5..a46daf2b43116b59954cd699e9284ba8f1905634 100644
--- a/cmd/ugitd/main.go
+++ b/cmd/ugitd/main.go
@@ -4,21 +4,21 @@ import (
"errors"
"flag"
"fmt"
+ "log/slog"
"os"
"os/signal"
"path/filepath"
"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"
+ "github.com/go-git/go-git/v5/utils/trace"
"go.jolheiser.com/ugit/internal/git"
-
"go.jolheiser.com/ugit/internal/http"
"go.jolheiser.com/ugit/internal/ssh"
-
- "github.com/charmbracelet/log"
- "github.com/go-chi/chi/v5/middleware"
- "github.com/go-git/go-git/v5/utils/trace"
)
func main() {
@@ -39,14 +39,24 @@ if err != nil {
panic(err)
}
- if args.Debug {
+ log.SetLevel(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,
+ }))
+
+ if args.Log.Level == log.DebugLevel {
trace.SetTarget(trace.Packet)
- log.SetLevel(log.DebugLevel)
} else {
middleware.DefaultLogger = http.NoopLogger
ssh.DefaultLogger = ssh.NoopLogger
}
+ if args.Log.JSON {
+ log.SetFormatter(log.JSONFormatter)
+ }
+
if err := requiredFS(args.RepoDir); err != nil {
panic(err)
}
@@ -63,7 +73,7 @@ if err != nil {
panic(err)
}
go func() {
- fmt.Printf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
+ log.Debugf("SSH listening on ssh://localhost:%d\n", sshSettings.Port)
if err := sshSrv.ListenAndServe(); err != nil {
panic(err)
}
@@ -88,7 +98,7 @@ })
}
httpSrv := http.New(httpSettings)
go func() {
- fmt.Printf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
+ log.Debugf("HTTP listening on http://localhost:%d\n", httpSettings.Port)
if err := httpSrv.ListenAndServe(); err != nil {
panic(err)
}
diff --git a/flake.nix b/flake.nix
index e93f15e7047370e1fb817580d6f8c52f4ebba421..bf67a0f284186c24abc2176f46d84a9553f74779 100644
--- a/flake.nix
+++ b/flake.nix
@@ -128,11 +128,6 @@ default = "ugit";
description = "Group account under which ugit runs";
};
- debug = mkOption {
- type = types.bool;
- default = false;
- };
-
openFirewall = mkOption {
type = types.bool;
default = false;
@@ -160,7 +155,7 @@ authorizedKeysPath =
if (builtins.length cfg.authorizedKeys) > 0
then authorizedKeysFile
else cfg.authorizedKeysFile;
- args = ["--config=${configFile}" "--repo-dir=${cfg.repoDir}" "--ssh.authorized-keys=${authorizedKeysPath}" "--ssh.host-key=${cfg.hostKeyFile}"] ++ lib.optionals cfg.debug ["--debug"];
+ args = ["--config=${configFile}" "--repo-dir=${cfg.repoDir}" "--ssh.authorized-keys=${authorizedKeysPath}" "--ssh.host-key=${cfg.hostKeyFile}"];
in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
wantedBy = ["multi-user.target"];
after = ["network.target"];
diff --git a/go.mod b/go.mod
index 20c78ab22603fea524ea9c10b31ed9b14c723493..2d22bd7def304664216b9226fcf95620ee3a6e3d 100644
--- a/go.mod
+++ b/go.mod
@@ -12,6 +12,7 @@ github.com/charmbracelet/ssh v0.0.0-20240201134204-3f297de25560
github.com/charmbracelet/wish v1.3.0
github.com/dustin/go-humanize v1.0.1
github.com/go-chi/chi/v5 v5.0.11
+ github.com/go-chi/httplog/v2 v2.1.1
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/peterbourgon/ff/v3 v3.4.0
diff --git a/go.sum b/go.sum
index 82a129f69bbf9c217f1a5d7f38c11254a4411958..511fb70ed83802d0d2361d3ecdbba3030aef4b2d 100644
--- a/go.sum
+++ b/go.sum
@@ -64,6 +64,8 @@ github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
+github.com/go-chi/httplog/v2 v2.1.1 h1:ojojiu4PIaoeJ/qAO4GWUxJqvYUTobeo7zmuHQJAxRk=
+github.com/go-chi/httplog/v2 v2.1.1/go.mod h1:/XXdxicJsp4BA5fapgIC3VuTD+z0Z/VzukoB3VDc1YE=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=