From 43e119f12cc3b622bba982d20b07a5b80377f6af Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Wed, 6 Jul 2022 00:05:15 -0400 Subject: [PATCH] log improvements; todo with question --- caddyhugo.go | 23 ++++++++++++++++++++--- content.go | 1 + go.mod | 2 +- setup.go | 3 +-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/caddyhugo.go b/caddyhugo.go index 6544b35..186ba6f 100644 --- a/caddyhugo.go +++ b/caddyhugo.go @@ -1,8 +1,8 @@ package caddyhugo import ( + "fmt" "html/template" - "log" "net/http" "path" "path/filepath" @@ -12,6 +12,7 @@ import ( "git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/media" + "go.uber.org/zap" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" @@ -42,17 +43,20 @@ func (m *CaddyHugo) CaddyModule() caddy.ModuleInfo { } func (m *CaddyHugo) Provision(ctx caddy.Context) error { + m.logger = ctx.Logger(m) + if m.Site.CommentsEnabled { m.Comments = comments.WithStorage(comments.NewDiskv(path.Join(m.Site.Root, "comments"))) } + // TODO: not sure where m.Site is getting populated. + // m.Site.Root looks to be the working directory of caddy but it needs to be the directory of the site root, err := filepath.Abs(m.Site.Root) if err != nil { return err } - log.Println("SETTING UP") + err = m.Setup(root) - log.Println("SET UP; err:", err) if err != nil { return err } @@ -79,6 +83,8 @@ func (s *SiteConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { // CaddyHugo implements the plugin for a single site type CaddyHugo struct { + logger *zap.Logger + ServerType string Site SiteConfig HugoSites *hugolib.HugoSites @@ -98,6 +104,17 @@ type CaddyHugo struct { confirmingToClient map[uint64]struct{} } +func (ch *CaddyHugo) log(msg string, args ...interface{}) { + all := make([]any, len(args)+1) + all[0] = msg + copy(all[1:], args) + ch.logger.Info(fmt.Sprint(all...)) +} + +func (ch *CaddyHugo) logf(msg string, args ...interface{}) { + ch.logger.Info(fmt.Sprintf(msg, args...)) +} + // Build rebuilds the cached state of the site. TODO: determine if this republishes func (ch *CaddyHugo) Build() error { return buildSite(ch.HugoSites) diff --git a/content.go b/content.go index 716a84b..db04a24 100644 --- a/content.go +++ b/content.go @@ -98,6 +98,7 @@ func (ch *CaddyHugo) NewContent(name, ctype string) (string, error) { if os.IsNotExist(err) { cmd := exec.Command("hugo", "new", filename) cmd.Dir = ch.Dir + ch.logf(ch.Dir) out, err := cmd.CombinedOutput() if err != nil { return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out)) diff --git a/go.mod b/go.mod index 1bf0655..8cc4ea6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.stephensearles.com/stephen/caddy-hugo2 -go 1.16 +go 1.18 require ( git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d diff --git a/setup.go b/setup.go index 2a56099..f4fc328 100644 --- a/setup.go +++ b/setup.go @@ -3,7 +3,6 @@ package caddyhugo import ( "fmt" "html/template" - "log" "os" "path" @@ -49,7 +48,7 @@ func (ch *CaddyHugo) commentsSetting() { func (ch *CaddyHugo) Setup(dir string) error { var err error - log.Println("setting up caddy-hugo in", dir) + ch.log("setting up caddy-hugo in", dir) ch.Dir = dir ch.docs = make(map[string]*editSession) ch.confirmingToClient = make(map[uint64]struct{})