Add default name to logger

This commit is contained in:
spsobole 2024-04-16 11:35:00 -06:00
parent bba66fe4cc
commit 1cfae5917e
1 changed files with 11 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import (
)
type Server struct {
log log.Logger
address string
theme string
debug bool
@ -211,7 +212,7 @@ func (s *Server) LoadTemplatesFS(fs http.FileSystem, base string) (*template.Tem
if strings.Contains(path, ".html") {
_, err := s.parseTemplates(tmpl, path)
if err != nil {
log.Printf("%+v\n", err)
s.log.Printf("%+v\n", err)
}
}
return err
@ -227,7 +228,7 @@ func (s *Server) LoadTemplatesFS(fs http.FileSystem, base string) (*template.Tem
func (s *Server) loadTemplates() *template.Template {
tmpl, err := s.LoadTemplatesFS(s.fs, s.templates)
if err != nil {
log.Fatalf("loadTemplates %v %v\n", err, s.templates)
s.log.Fatalf("loadTemplates %v %v\n", err, s.templates)
}
return tmpl
}
@ -254,7 +255,7 @@ func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{})
}
err := s.getTemplates().ExecuteTemplate(w, tmpl, content)
if err != nil {
log.Warnf("%s\n", err)
s.log.Warnf("%s\n", err)
}
}
@ -284,7 +285,7 @@ func (s *Server) HandleFuncAuthenticatedWithLogin(path string, loginHandler func
func (s *Server) HandleFuncCustomAuth(auth auth.Authenticator, path, redirect string, f func(c *Context)) *mux.Route {
if auth == nil {
log.Warnf("Nil auth on %s\n", path)
s.log.Warnf("Nil auth on %s\n", path)
return nil
}
return s.router.HandleFunc(path, s.authenticated(auth, redirect, f))
@ -320,7 +321,7 @@ func (s *Server) Router() *mux.Router {
func (s *Server) ServeTLS(keyPath string, certPath string) error {
kpr, err := autocert.NewManager(certPath, keyPath)
if err != nil {
log.Fatalf("%v\n", err)
s.log.Fatalf("%v\n", err)
}
s.http = &http.Server{
@ -422,6 +423,10 @@ func (s *Server) WithHealthCheck(version, date string, status func() (bool, stri
})
}
func (s *Server) SetLogger(logger log.Logger) {
s.log = logger
}
func (s *Server) Dump() {
fmt.Printf(" Theme: %s\n", s.theme)
fmt.Printf(" Templates: %s\n", s.templates)
@ -443,6 +448,7 @@ func New(address string, path string, auth auth.Authenticator) *Server {
templateFuncs: builtinFuncMap,
theme: "/static/css/default.css",
meta: make(map[string]string),
log: log.WithName("SNAP"),
}
return &s
}