feat: multiple log levels and json logging

Signed-off-by: jolheiser <git@jolheiser.com>
form
jolheiser 2024-07-17 16:31:16 -05:00
parent fce5e40086
commit dcc55ceb04
No known key found for this signature in database
5 changed files with 43 additions and 18 deletions

View File

@ -5,17 +5,18 @@ import (
"fmt" "fmt"
"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"
) )
type cliArgs struct { type cliArgs struct {
Debug bool
RepoDir string RepoDir string
SSH sshArgs SSH sshArgs
HTTP httpArgs HTTP httpArgs
Meta metaArgs Meta metaArgs
Profile profileArgs Profile profileArgs
Log logArgs
} }
type sshArgs struct { type sshArgs struct {
@ -46,6 +47,11 @@ type profileLink struct {
URL string URL string
} }
type logArgs struct {
Level log.Level
JSON bool
}
func parseArgs(args []string) (c cliArgs, e error) { func parseArgs(args []string) (c cliArgs, e error) {
fs := flag.NewFlagSet("ugitd", flag.ContinueOnError) fs := flag.NewFlagSet("ugitd", flag.ContinueOnError)
fs.String("config", "ugit.yaml", "Path to config file") fs.String("config", "ugit.yaml", "Path to config file")
@ -66,9 +72,20 @@ func parseArgs(args []string) (c cliArgs, e error) {
Title: "ugit", Title: "ugit",
Description: "Minimal git server", 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.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.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") fs.StringVar(&c.SSH.CloneURL, "ssh.clone-url", c.SSH.CloneURL, "SSH clone URL base")

View File

@ -4,21 +4,21 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"log/slog"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"go.jolheiser.com/ugit/internal/git"
"go.jolheiser.com/ugit/internal/http"
"go.jolheiser.com/ugit/internal/ssh"
"github.com/charmbracelet/log" "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-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/utils/trace" "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"
) )
func main() { func main() {
@ -39,14 +39,24 @@ func main() {
panic(err) 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) trace.SetTarget(trace.Packet)
log.SetLevel(log.DebugLevel)
} else { } else {
middleware.DefaultLogger = http.NoopLogger middleware.DefaultLogger = http.NoopLogger
ssh.DefaultLogger = ssh.NoopLogger ssh.DefaultLogger = ssh.NoopLogger
} }
if args.Log.JSON {
log.SetFormatter(log.JSONFormatter)
}
if err := requiredFS(args.RepoDir); err != nil { if err := requiredFS(args.RepoDir); err != nil {
panic(err) panic(err)
} }
@ -63,7 +73,7 @@ func main() {
panic(err) panic(err)
} }
go func() { 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 { if err := sshSrv.ListenAndServe(); err != nil {
panic(err) panic(err)
} }
@ -88,7 +98,7 @@ func main() {
} }
httpSrv := http.New(httpSettings) httpSrv := http.New(httpSettings)
go func() { 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 { if err := httpSrv.ListenAndServe(); err != nil {
panic(err) panic(err)
} }

View File

@ -128,11 +128,6 @@
description = "Group account under which ugit runs"; description = "Group account under which ugit runs";
}; };
debug = mkOption {
type = types.bool;
default = false;
};
openFirewall = mkOption { openFirewall = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
@ -160,7 +155,7 @@
if (builtins.length cfg.authorizedKeys) > 0 if (builtins.length cfg.authorizedKeys) > 0
then authorizedKeysFile then authorizedKeysFile
else cfg.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}"; in "${cfg.package}/bin/ugitd ${builtins.concatStringsSep " " args}";
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
after = ["network.target"]; after = ["network.target"];

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/charmbracelet/wish v1.3.0 github.com/charmbracelet/wish v1.3.0
github.com/dustin/go-humanize v1.0.1 github.com/dustin/go-humanize v1.0.1
github.com/go-chi/chi/v5 v5.0.11 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-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0 github.com/go-git/go-git/v5 v5.11.0
github.com/peterbourgon/ff/v3 v3.4.0 github.com/peterbourgon/ff/v3 v3.4.0

2
go.sum
View File

@ -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/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 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 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 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= 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= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=