From 1cfae5917ec29816e3572df3e48109db9a586791 Mon Sep 17 00:00:00 2001 From: spsobole Date: Tue, 16 Apr 2024 11:35:00 -0600 Subject: [PATCH] Add default name to logger --- server.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server.go b/server.go index 030a684..8469707 100644 --- a/server.go +++ b/server.go @@ -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 }