mirror of
https://github.com/coalaura/whiskr.git
synced 2025-09-07 08:15:31 +00:00
better logger
This commit is contained in:
2
debug.go
2
debug.go
@@ -19,7 +19,7 @@ func debug(format string, args ...any) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf(format+"\n", args...)
|
||||
log.Printf(format+"\n", args...)
|
||||
}
|
||||
|
||||
func debugIf(cond bool, format string, args ...any) {
|
||||
|
16
env.go
16
env.go
@@ -51,25 +51,25 @@ var env = Environment{
|
||||
|
||||
func init() {
|
||||
file, err := os.OpenFile("config.yml", os.O_RDONLY, 0)
|
||||
log.MustPanic(err)
|
||||
log.MustFail(err)
|
||||
|
||||
defer file.Close()
|
||||
|
||||
err = yaml.NewDecoder(file).Decode(&env)
|
||||
log.MustPanic(err)
|
||||
log.MustFail(err)
|
||||
|
||||
log.MustPanic(env.Init())
|
||||
log.MustFail(env.Init())
|
||||
}
|
||||
|
||||
func (e *Environment) Init() error {
|
||||
// print if debug is enabled
|
||||
if e.Debug {
|
||||
log.Warning("Debug mode enabled")
|
||||
log.Warnln("Debug mode enabled")
|
||||
}
|
||||
|
||||
// check if server secret is set
|
||||
if e.Tokens.Secret == "" {
|
||||
log.Warning("Missing tokens.secret, generating new...")
|
||||
log.Warnln("Missing tokens.secret, generating new...")
|
||||
|
||||
key := make([]byte, 32)
|
||||
|
||||
@@ -85,7 +85,7 @@ func (e *Environment) Init() error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("Stored new tokens.secret")
|
||||
log.Println("Stored new tokens.secret")
|
||||
}
|
||||
|
||||
// check if openrouter token is set
|
||||
@@ -95,12 +95,12 @@ func (e *Environment) Init() error {
|
||||
|
||||
// check if exa token is set
|
||||
if e.Tokens.Exa == "" {
|
||||
log.Warning("Missing token.exa, web search unavailable")
|
||||
log.Warnln("Missing token.exa, web search unavailable")
|
||||
}
|
||||
|
||||
// check if github token is set
|
||||
if e.Tokens.GitHub == "" {
|
||||
log.Warning("Missing token.github, limited api requests")
|
||||
log.Warnln("Missing token.github, limited api requests")
|
||||
}
|
||||
|
||||
// default title model
|
||||
|
@@ -162,14 +162,14 @@ func RepoOverview(ctx context.Context, arguments GitHubRepositoryArguments) (str
|
||||
|
||||
readme, err := GitHubRepositoryReadmeJson(ctx, arguments.Owner, arguments.Repo, repository.DefaultBranch)
|
||||
if err != nil {
|
||||
log.Warningf("failed to get repository readme: %v\n", err)
|
||||
log.Warnf("failed to get repository readme: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
markdown, err := readme.AsText()
|
||||
if err != nil {
|
||||
log.Warningf("failed to decode repository readme: %v\n", err)
|
||||
log.Warnf("failed to decode repository readme: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -185,7 +185,7 @@ func RepoOverview(ctx context.Context, arguments GitHubRepositoryArguments) (str
|
||||
|
||||
contents, err := GitHubRepositoryContentsJson(ctx, arguments.Owner, arguments.Repo, repository.DefaultBranch)
|
||||
if err != nil {
|
||||
log.Warningf("failed to get repository contents: %v\n", err)
|
||||
log.Warnf("failed to get repository contents: %v\n", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
9
go.mod
9
go.mod
@@ -1,22 +1,21 @@
|
||||
module chat
|
||||
|
||||
go 1.24.5
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/coalaura/logger v1.5.1
|
||||
github.com/go-chi/chi/v5 v5.2.2
|
||||
github.com/coalaura/plain v0.2.0
|
||||
github.com/go-chi/chi/v5 v5.2.3
|
||||
github.com/goccy/go-yaml v1.18.0
|
||||
github.com/revrost/go-openrouter v0.2.2
|
||||
golang.org/x/crypto v0.41.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/containerd/console v1.0.5 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/gookit/color v1.5.4 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/rs/zerolog v1.34.0 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
golang.org/x/term v0.34.0 // indirect
|
||||
)
|
||||
|
15
go.sum
15
go.sum
@@ -1,5 +1,7 @@
|
||||
github.com/coalaura/logger v1.5.1 h1:2no4UP1HYOKQBasAol7RP81V0emJ2sfJIIoKOtrATqM=
|
||||
github.com/coalaura/logger v1.5.1/go.mod h1:npioUhSPFmjxOmLzYbl9X0G6sdZgvuMikTlmc6VitWo=
|
||||
github.com/coalaura/plain v0.2.0 h1:naGiTT1nmZO78IGHOajm0wc/X4sqaG6g3CSR3Ha9f6w=
|
||||
github.com/coalaura/plain v0.2.0/go.mod h1:HR/sQt288EMTF3aSEGKHwPmGYFU4FOrfarMUf6ifnLo=
|
||||
github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc=
|
||||
github.com/containerd/console v1.0.5/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -7,11 +9,11 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
|
||||
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
|
||||
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
||||
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
@@ -29,13 +31,10 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
|
||||
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
|
17
main.go
17
main.go
@@ -6,30 +6,27 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/coalaura/logger"
|
||||
adapter "github.com/coalaura/logger/http"
|
||||
"github.com/coalaura/plain"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
var Version = "dev"
|
||||
|
||||
var log = logger.New().DetectTerminal().WithOptions(logger.Options{
|
||||
NoLevel: true,
|
||||
})
|
||||
var log = plain.New(plain.WithDate(plain.RFC3339Local))
|
||||
|
||||
func main() {
|
||||
icons, err := LoadIcons()
|
||||
log.MustPanic(err)
|
||||
log.MustFail(err)
|
||||
|
||||
models, err := LoadModels()
|
||||
log.MustPanic(err)
|
||||
log.MustFail(err)
|
||||
|
||||
log.Info("Preparing router...")
|
||||
log.Println("Preparing router...")
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(adapter.Middleware(log))
|
||||
r.Use(log.Middleware())
|
||||
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
r.Handle("/*", cache(http.StripPrefix("/", fs)))
|
||||
@@ -56,7 +53,7 @@ func main() {
|
||||
gr.Post("/-/chat", HandleChat)
|
||||
})
|
||||
|
||||
log.Info("Listening at http://localhost:3443/")
|
||||
log.Println("Listening at http://localhost:3443/")
|
||||
http.ListenAndServe(":3443", r)
|
||||
}
|
||||
|
||||
|
@@ -23,7 +23,7 @@ type Model struct {
|
||||
var ModelMap = make(map[string]*Model)
|
||||
|
||||
func LoadModels() ([]*Model, error) {
|
||||
log.Info("Loading models...")
|
||||
log.Println("Loading models...")
|
||||
|
||||
client := OpenRouterClient()
|
||||
|
||||
@@ -58,7 +58,7 @@ func LoadModels() ([]*Model, error) {
|
||||
ModelMap[model.ID] = m
|
||||
}
|
||||
|
||||
log.Infof("Loaded %d models\n", len(models))
|
||||
log.Printf("Loaded %d models\n", len(models))
|
||||
|
||||
return models, nil
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ func init() {
|
||||
var err error
|
||||
|
||||
Prompts, err = LoadPrompts()
|
||||
log.MustPanic(err)
|
||||
log.MustFail(err)
|
||||
}
|
||||
|
||||
func NewTemplate(name, text string) *template.Template {
|
||||
@@ -59,7 +59,7 @@ func NewTemplate(name, text string) *template.Template {
|
||||
func LoadPrompts() ([]Prompt, error) {
|
||||
var prompts []Prompt
|
||||
|
||||
log.Info("Loading prompts...")
|
||||
log.Println("Loading prompts...")
|
||||
|
||||
err := filepath.Walk("prompts", func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil || info.IsDir() {
|
||||
@@ -80,7 +80,7 @@ func LoadPrompts() ([]Prompt, error) {
|
||||
|
||||
index := bytes.Index(body, []byte("---"))
|
||||
if index == -1 {
|
||||
log.Warningf("Invalid prompt file: %q\n", path)
|
||||
log.Warnf("Invalid prompt file: %q\n", path)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func LoadPrompts() ([]Prompt, error) {
|
||||
return prompts[i].Name < prompts[j].Name
|
||||
})
|
||||
|
||||
log.Infof("Loaded %d prompts\n", len(prompts))
|
||||
log.Printf("Loaded %d prompts\n", len(prompts))
|
||||
|
||||
return prompts, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user