feat: replace logging with zerolog

This commit is contained in:
Manuel Rüger 2026-03-28 09:55:58 +01:00
parent 74c4a306c3
commit e160121005
19 changed files with 204 additions and 180 deletions

View File

@ -19,7 +19,7 @@ import (
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/vfs"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
const (
@ -101,7 +101,7 @@ func ResolveAttachments(
}
for i, attachment := range creating {
log.Infof(nil, "creating attachment: %q", attachment.Name)
log.Info().Msgf("creating attachment: %q", attachment.Name)
info, err := api.CreateAttachment(
page.ID,
@ -127,7 +127,7 @@ func ResolveAttachments(
}
for i, attachment := range updating {
log.Infof(nil, "updating attachment: %q", attachment.Name)
log.Info().Msgf("updating attachment: %q", attachment.Name)
info, err := api.UpdateAttachment(
page.ID,
@ -153,7 +153,7 @@ func ResolveAttachments(
}
for i := range existing {
log.Infof(nil, "keeping unmodified attachment: %q", existing[i].Name)
log.Info().Msgf("keeping unmodified attachment: %q", existing[i].Name)
}
attachments = []Attachment{}
@ -260,7 +260,7 @@ func CompileAttachmentLinks(markdown []byte, attachments []Attachment) []byte {
if bytes.Contains(markdown, []byte("attachment://"+replace)) {
from := "attachment://" + replace
log.Debugf(nil, "replacing legacy link: %q -> %q", from, to)
log.Debug().Msgf("replacing legacy link: %q -> %q", from, to)
markdown = bytes.ReplaceAll(
markdown,
@ -274,7 +274,7 @@ func CompileAttachmentLinks(markdown []byte, attachments []Attachment) []byte {
if bytes.Contains(markdown, []byte(replace)) {
from := replace
log.Debugf(nil, "replacing link: %q -> %q", from, to)
log.Debug().Msgf("replacing link: %q -> %q", from, to)
markdown = bytes.ReplaceAll(
markdown,
@ -286,7 +286,7 @@ func CompileAttachmentLinks(markdown []byte, attachments []Attachment) []byte {
}
if !found {
log.Warningf(nil, "unused attachment: %s", replace)
log.Warn().Msgf("unused attachment: %s", replace)
}
}

View File

@ -6,7 +6,7 @@ import (
"os"
"github.com/kovetskiy/mark/v16/util"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v3"
)
@ -34,6 +34,6 @@ func main() {
}
if err := cmd.Run(context.TODO(), os.Args); err != nil {
log.Fatal(err)
log.Fatal().Msg(err.Error())
}
}

View File

@ -13,9 +13,9 @@ import (
"unicode/utf8"
"github.com/kovetskiy/gopencils"
"github.com/kovetskiy/lorg"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type User struct {
@ -96,7 +96,7 @@ type tracer struct {
}
func (tracer *tracer) Printf(format string, args ...interface{}) {
log.Tracef(nil, tracer.prefix+" "+format, args...)
log.Trace().Msgf(tracer.prefix+" "+format, args...)
}
func NewAPI(baseURL string, username string, password string, insecureSkipVerify bool) *API {
@ -132,7 +132,7 @@ func NewAPI(baseURL string, username string, password string, insecureSkipVerify
json := gopencils.Api(baseURL+"/rpc/json-rpc/confluenceservice-v2", auth, httpClient, 3)
if log.GetLevel() == lorg.LevelTrace {
if zerolog.GlobalLevel() == zerolog.TraceLevel {
rest.Logger = &tracer{"rest:"}
json.Logger = &tracer{"json-rpc:"}
}

View File

@ -14,7 +14,7 @@ import (
"github.com/chromedp/chromedp"
"github.com/kovetskiy/mark/v16/attachment"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
@ -59,7 +59,7 @@ func ProcessD2(title string, d2Diagram []byte, scale float64) (attachment.Attach
return attachment.Attachment{}, err
}
log.Debugf(nil, "Rendering: %q", title)
log.Debug().Msgf("Rendering: %q", title)
pngBytes, boxModel, err := convertSVGtoPNG(ctx, out, scale)
if err != nil {
return attachment.Attachment{}, err
@ -73,7 +73,7 @@ func ProcessD2(title string, d2Diagram []byte, scale float64) (attachment.Attach
checkSum, err := attachment.GetChecksum(bytes.NewReader(d2Bytes))
log.Debugf(nil, "Checksum: %q -> %s", title, checkSum)
log.Debug().Msgf("Checksum: %q -> %s", title, checkSum)
if err != nil {
return attachment.Attachment{}, err

7
go.mod
View File

@ -8,9 +8,8 @@ require (
github.com/chromedp/chromedp v0.15.1
github.com/dreampuf/mermaid.go v0.0.40
github.com/kovetskiy/gopencils v0.0.0-20250404051442-0b776066936a
github.com/kovetskiy/lorg v1.2.1-0.20240830111423-ba4fe8b6f7c4
github.com/reconquest/karma-go v1.5.0
github.com/reconquest/pkg v1.3.1-0.20240901105413-68c2adbf2b64
github.com/rs/zerolog v1.35.0
github.com/stefanfritsch/goldmark-admonitions v1.1.1
github.com/stretchr/testify v1.11.1
github.com/urfave/cli-altsrc/v3 v3.1.0
@ -41,13 +40,13 @@ require (
github.com/google/pprof v0.0.0-20240927180334-d43a67379298 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mazznoer/csscolorparser v0.1.5 // indirect
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/reconquest/cog v0.0.0-20240830113510-c7ba12d0beeb // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/zazab/zhash v0.0.0-20221031090444-2b0d50417446 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/image v0.20.0 // indirect
golang.org/x/net v0.44.0 // indirect

14
go.sum
View File

@ -51,8 +51,6 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kovetskiy/gopencils v0.0.0-20250404051442-0b776066936a h1:OPt6gCghZXQ/WZpT6EhGkA7v+YMAYzcCb8SPQWmsb/8=
github.com/kovetskiy/gopencils v0.0.0-20250404051442-0b776066936a/go.mod h1:gRW37oDEg9LzOHApv31YzxKBICcCmPtDogaImsxZ6xc=
github.com/kovetskiy/lorg v1.2.1-0.20240830111423-ba4fe8b6f7c4 h1:2eV8tF1u58dqRJMlFUD/Df26BxcIlGVy71rZHN+aNoI=
github.com/kovetskiy/lorg v1.2.1-0.20240830111423-ba4fe8b6f7c4/go.mod h1:p1RuSvyflTF/G4ubeATGurCRKWkULOrN/4PUAEFRq0s=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@ -64,6 +62,10 @@ github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kUL
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mazznoer/csscolorparser v0.1.5 h1:Wr4uNIE+pHWN3TqZn2SGpA2nLRG064gB7WdSfSS5cz4=
github.com/mazznoer/csscolorparser v0.1.5/go.mod h1:OQRVvgCyHDCAquR1YWfSwwaDcM0LhnSffGnlbOew/3I=
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785 h1:J1//5K/6QF10cZ59zLcVNFGmBfiSrH8Cho/lNrViK9s=
@ -71,17 +73,15 @@ github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785/go.mod h1:nZgzb
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/reconquest/cog v0.0.0-20240830113510-c7ba12d0beeb h1:hJ1ExqE2lTMgTRmjmSiC2hm+sMXCCjjbyiGo3irbEW8=
github.com/reconquest/cog v0.0.0-20240830113510-c7ba12d0beeb/go.mod h1:n+lvvNLeoQmYVvYTFGCtLvoyD9Wz46RO3yCk6GKyZ/4=
github.com/reconquest/karma-go v1.5.0 h1:Chn4LtauwnvKfz13ZbmGNrRLKO1NciExHQSOBOsQqt4=
github.com/reconquest/karma-go v1.5.0/go.mod h1:52XRXXa2ec/VNrlCirwasdJfNmjI1O87q098gmqILh0=
github.com/reconquest/pkg v1.3.1-0.20240901105413-68c2adbf2b64 h1:OBNLiZay5PYLmGRXGIMEgWSIgbSjOj8nHZxqwLbSsF4=
github.com/reconquest/pkg v1.3.1-0.20240901105413-68c2adbf2b64/go.mod h1:r1Z1JNh3in9xLWbhv5u7cdox9vvGFjlKp89VI10Jrdo=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/zerolog v1.35.0 h1:VD0ykx7HMiMJytqINBsKcbLS+BJ4WYjz+05us+LRTdI=
github.com/rs/zerolog v1.35.0/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
github.com/stefanfritsch/goldmark-admonitions v1.1.1 h1:SncsICdQrIYYaq02Ta+zyc9gNmMfYqQH2qwLSCJYxA4=
github.com/stefanfritsch/goldmark-admonitions v1.1.1/go.mod h1:cOZK5O0gE6eWfpxTdjGUmeONW2IL9j3Zujv3KlZWlLo=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
@ -95,8 +95,6 @@ github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3i
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/zazab/zhash v0.0.0-20221031090444-2b0d50417446 h1:75pcOSsb40+ub185cJI7g5uykl9Uu76rD5ONzK/4s40=
github.com/zazab/zhash v0.0.0-20221031090444-2b0d50417446/go.mod h1:NtepZ8TEXErPsmQDMUoN72f8aIy4+xNinSJ3f1giess=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=

View File

@ -12,7 +12,7 @@ import (
"go.yaml.in/yaml/v3"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
// <!-- Include: <template path>
@ -146,7 +146,7 @@ func ProcessIncludes(
return spec
}
log.Tracef(vardump(facts, data), "including template %q", path)
log.Trace().Interface("vardump", vardump(facts, data)).Msgf("including template %q", path)
templates, err = LoadTemplate(base, includePath, path, left, right, templates)
if err != nil {

View File

@ -8,8 +8,7 @@ import (
"text/template"
"github.com/kovetskiy/mark/v16/includes"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
"go.yaml.in/yaml/v3"
)
@ -190,11 +189,9 @@ func ExtractMacros(
macro.Config = config
log.Tracef(
facts.Describe("config", macro.Config),
"loaded macro %q",
expr,
)
log.Trace().
Interface("vardump", facts.Describe("config", macro.Config)).
Msgf("loaded macro %q", expr)
macros = append(macros, macro)

41
mark.go
View File

@ -25,7 +25,7 @@ import (
"github.com/kovetskiy/mark/v16/types"
"github.com/kovetskiy/mark/v16/vfs"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
// Config holds all configuration options for running Mark.
@ -96,7 +96,7 @@ func Run(config Config) error {
if len(files) == 0 {
msg := "no files matched"
if config.CI {
log.Warning(msg)
log.Warn().Msg(msg)
} else {
return fmt.Errorf("%s", msg)
}
@ -104,12 +104,12 @@ func Run(config Config) error {
var hasErrors bool
for _, file := range files {
log.Infof(nil, "processing %s", file)
log.Info().Msgf("processing %s", file)
target, err := ProcessFile(file, api, config)
if err != nil {
if config.ContinueOnError {
log.Errorf(err, "processing %s", file)
log.Error().Err(err).Msgf("processing %s", file)
hasErrors = true
continue
}
@ -117,7 +117,7 @@ func Run(config Config) error {
}
if target != nil {
log.Infof(nil, "page successfully updated: %s", api.BaseURL+target.Links.Full)
log.Info().Msgf("page successfully updated: %s", api.BaseURL+target.Links.Full)
if _, err := fmt.Fprintln(config.output(), api.BaseURL+target.Links.Full); err != nil {
return err
}
@ -156,7 +156,7 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
}
if config.PageID != "" && meta != nil {
log.Warning(
log.Warn().Msg(
`specified file contains metadata, ` +
`but it will be ignored due specified command line URL`,
)
@ -255,7 +255,7 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
if config.CompileOnly || config.DryRun {
if config.DropH1 {
log.Info("the leading H1 heading will be excluded from the Confluence output")
log.Info().Msg("the leading H1 heading will be excluded from the Confluence output")
}
imageAlign, err := getImageAlign(config.ImageAlign, meta)
@ -335,7 +335,7 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
markdown = attachment.CompileAttachmentLinks(markdown, attaches)
if config.DropH1 {
log.Info("the leading H1 heading will be excluded from the Confluence output")
log.Info().Msg("the leading H1 heading will be excluded from the Confluence output")
}
imageAlign, err := getImageAlign(config.ImageAlign, meta)
@ -399,13 +399,13 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
if config.ChangesOnly {
contentHash := sha1Hash(html)
log.Debugf(nil, "content hash: %s", contentHash)
log.Debug().Msgf("content hash: %s", contentHash)
re := regexp.MustCompile(`\[v([a-f0-9]{40})]$`)
if matches := re.FindStringSubmatch(target.Version.Message); len(matches) > 1 {
log.Debugf(nil, "previous content hash: %s", matches[1])
log.Debug().Msgf("previous content hash: %s", matches[1])
if matches[1] == contentHash {
log.Infof(nil, "page %q is already up to date", target.Title)
log.Info().Msgf("page %q is already up to date", target.Title)
shouldUpdatePage = false
}
}
@ -436,8 +436,7 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
}
if config.EditLock {
log.Infof(
nil,
log.Info().Msgf(
`edit locked on page %q by user %q to prevent manual edits`,
target.Title,
config.Username,
@ -456,18 +455,18 @@ func updateLabels(api *confluence.API, target *confluence.PageInfo, metaLabels [
return err
}
log.Debug("Page Labels:")
log.Debug(labelInfo.Labels)
log.Debug("Meta Labels:")
log.Debug(metaLabels)
log.Debug().Msg("Page Labels:")
log.Debug().Interface("labels", labelInfo.Labels).Send()
log.Debug().Msg("Meta Labels:")
log.Debug().Interface("labels", metaLabels).Send()
delLabels := determineLabelsToRemove(labelInfo, metaLabels)
log.Debug("Del Labels:")
log.Debug(delLabels)
log.Debug().Msg("Del Labels:")
log.Debug().Interface("labels", delLabels).Send()
addLabels := determineLabelsToAdd(metaLabels, labelInfo)
log.Debug("Add Labels:")
log.Debug(addLabels)
log.Debug().Msg("Add Labels:")
log.Debug().Interface("labels", addLabels).Send()
if len(addLabels) > 0 {
if _, err = api.AddPageLabels(target, addLabels); err != nil {

View File

@ -9,7 +9,7 @@ import (
crenderer "github.com/kovetskiy/mark/v16/renderer"
"github.com/kovetskiy/mark/v16/stdlib"
"github.com/kovetskiy/mark/v16/types"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
mkDocsParser "github.com/stefanfritsch/goldmark-admonitions"
"github.com/yuin/goldmark"
@ -91,7 +91,7 @@ func (c *ConfluenceExtension) Extend(m goldmark.Markdown) {
}
func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types.MarkConfig) (string, []attachment.Attachment, error) {
log.Tracef(nil, "rendering markdown:\n%s", string(markdown))
log.Trace().Msgf("rendering markdown:\n%s", string(markdown))
confluenceExtension := NewConfluenceExtension(stdlib, path, cfg)
@ -124,7 +124,7 @@ func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types
html := buf.Bytes()
log.Tracef(nil, "rendered markdown to html:\n%s", string(html))
log.Trace().Msgf("rendered markdown to html:\n%s", string(html))
return string(html), confluenceExtension.Attachments, nil
}

View File

@ -10,7 +10,7 @@ import (
mermaid "github.com/dreampuf/mermaid.go"
"github.com/kovetskiy/mark/v16/attachment"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
var renderTimeout = 120 * time.Second
@ -19,14 +19,14 @@ func ProcessMermaidLocally(title string, mermaidDiagram []byte, scale float64) (
ctx, cancel := context.WithTimeout(context.TODO(), renderTimeout)
defer cancel()
log.Debugf(nil, "Setting up Mermaid renderer: %q", title)
log.Debug().Msgf("Setting up Mermaid renderer: %q", title)
renderer, err := mermaid.NewRenderEngine(ctx)
if err != nil {
return attachment.Attachment{}, err
}
log.Debugf(nil, "Rendering: %q", title)
log.Debug().Msgf("Rendering: %q", title)
pngBytes, boxModel, err := renderer.RenderAsScaledPng(string(mermaidDiagram), scale)
if err != nil {
return attachment.Attachment{}, err
@ -39,7 +39,7 @@ func ProcessMermaidLocally(title string, mermaidDiagram []byte, scale float64) (
mermaidBytes := append(mermaidDiagram, scaleAsBytes...)
checkSum, err := attachment.GetChecksum(bytes.NewReader(mermaidBytes))
log.Debugf(nil, "Checksum: %q -> %s", title, checkSum)
log.Debug().Msgf("Checksum: %q -> %s", title, checkSum)
if err != nil {
return attachment.Attachment{}, err

View File

@ -9,7 +9,7 @@ import (
"regexp"
"strings"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
@ -132,12 +132,9 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFi
meta.ImageAlign = strings.ToLower(strings.TrimSpace(value))
default:
log.Errorf(
nil,
`encountered unknown header %q line: %#v`,
header,
line,
)
log.Error().
Err(nil).
Msgf(`encountered unknown header %q line: %#v`, header, line)
continue
}
@ -193,11 +190,7 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, titleFromFi
pathHash := sha256.Sum256([]byte(path))
// postfix is an 8-character hexadecimal string representation of the first 4 out of 32 bytes of the hash
meta.Title = fmt.Sprintf("%s - %x", meta.Title, pathHash[0:4])
log.Debugf(
nil,
"appended hash to page title: %s",
meta.Title,
)
log.Debug().Msgf("appended hash to page title: %s", meta.Title)
}
// Remove trailing spaces from title

View File

@ -6,7 +6,7 @@ import (
"github.com/kovetskiy/mark/v16/confluence"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
func EnsureAncestry(
@ -33,7 +33,7 @@ func EnsureAncestry(
break
}
log.Debugf(nil, "parent page %q exists: %s", title, page.Links.Full)
log.Debug().Msgf("parent page %q exists: %s", title, page.Links.Full)
rest = ancestry[i:]
parent = page
@ -57,12 +57,12 @@ func EnsureAncestry(
return parent, nil
}
log.Debugf(
nil,
"empty pages under %q to be created: %s",
parent.Title,
strings.Join(rest, ` > `),
)
log.Debug().
Msgf(
"empty pages under %q to be created: %s",
parent.Title,
strings.Join(rest, ` > `),
)
if !dryRun {
for _, title := range rest {
@ -78,13 +78,13 @@ func EnsureAncestry(
parent = page
}
} else {
log.Infof(
nil,
"skipping page creation due to enabled dry-run mode, "+
"need to create %d pages: %v",
len(rest),
rest,
)
log.Info().
Msgf(
"skipping page creation due to enabled dry-run mode, "+
"need to create %d pages: %v",
len(rest),
rest,
)
}
return parent, nil
@ -116,7 +116,7 @@ func ValidateAncestry(
}
if page.ID == homepage.ID {
log.Debugf(nil, "page is homepage for space %q", space)
log.Debug().Msgf("page is homepage for space %q", space)
isHomepage = true
} else {
return nil, fmt.Errorf(`page %q has no parents`, page.Title)

View File

@ -15,7 +15,7 @@ import (
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/metadata"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
type LinkSubstitution struct {
@ -51,13 +51,13 @@ func ResolveRelativeLinks(
links := []LinkSubstitution{}
for _, match := range matches {
log.Tracef(
nil,
"found a relative link: full=%s filename=%s hash=%s",
match.full,
match.filename,
match.hash,
)
log.Trace().
Msgf(
"found a relative link: full=%s filename=%s hash=%s",
match.full,
match.filename,
match.hash,
)
resolved, err := resolveLink(api, base, match, spaceForLinks, titleFromH1, titleFromFilename, parents, titleAppendGeneratedHash)
if err != nil {
return nil, karma.Format(err, "resolve link: %q", match.full)
@ -91,7 +91,7 @@ func resolveLink(
if len(link.filename) > 0 {
filepath := filepath.Join(base, link.filename)
log.Tracef(nil, "filepath: %s", filepath)
log.Trace().Msgf("filepath: %s", filepath)
stat, err := os.Stat(filepath)
if err != nil {
return "", nil
@ -109,7 +109,7 @@ func resolveLink(
contentType := http.DetectContentType(linkContents)
// Check if the MIME type starts with "text/"
if !strings.HasPrefix(contentType, "text/") {
log.Debugf(nil, "Ignoring link to file %q: detected content type %v", filepath, contentType)
log.Debug().Msgf("Ignoring link to file %q: detected content type %v", filepath, contentType)
return "", nil
}
@ -123,11 +123,12 @@ func resolveLink(
// not markdown or have mark required metadata
linkMeta, _, err := metadata.ExtractMeta(linkContents, spaceForLinks, titleFromH1, titleFromFilename, filepath, parents, titleAppendGeneratedHash, "")
if err != nil {
log.Errorf(
err,
"unable to extract metadata from %q; ignoring the relative link",
filepath,
)
log.Error().
Err(err).
Msgf(
"unable to extract metadata from %q; ignoring the relative link",
filepath,
)
return "", nil
}
@ -136,12 +137,12 @@ func resolveLink(
return "", nil
}
log.Tracef(
nil,
"extracted metadata: space=%s title=%s",
linkMeta.Space,
linkMeta.Title,
)
log.Trace().
Msgf(
"extracted metadata: space=%s title=%s",
linkMeta.Space,
linkMeta.Title,
)
result, err = getConfluenceLink(api, linkMeta.Space, linkMeta.Title)
if err != nil {
@ -172,7 +173,7 @@ func SubstituteLinks(markdown []byte, links []LinkSubstitution) []byte {
continue
}
log.Tracef(nil, "substitute link: %q -> %q", link.From, link.To)
log.Trace().Msgf("substitute link: %q -> %q", link.From, link.To)
markdown = bytes.ReplaceAll(
markdown,

View File

@ -6,7 +6,7 @@ import (
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/metadata"
"github.com/reconquest/karma-go"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
func ResolvePage(
@ -27,11 +27,11 @@ func ResolvePage(
}
if meta.Type == "blogpost" {
log.Infof(
nil,
"blog post will be stored as: %s",
meta.Title,
)
log.Info().
Msgf(
"blog post will be stored as: %s",
meta.Title,
)
return nil, page, nil
}
@ -69,21 +69,21 @@ func ResolvePage(
}
if page == nil {
log.Warningf(
nil,
"page %q is not found ",
ancestry[len(ancestry)-1],
)
log.Warn().
Msgf(
"page %q is not found ",
ancestry[len(ancestry)-1],
)
}
path := meta.Parents
path = append(path, meta.Title)
log.Debugf(
nil,
"resolving page path: ??? > %s",
strings.Join(path, ` > `),
)
log.Debug().
Msgf(
"resolving page path: ??? > %s",
strings.Join(path, ` > `),
)
}
parent, err := EnsureAncestry(
@ -107,8 +107,7 @@ func ResolvePage(
titles = append(titles, parent.Title)
log.Infof(
nil,
log.Info().Msgf(
"page will be stored under path: %s > %s",
strings.Join(titles, ` > `),
meta.Title,

View File

@ -6,7 +6,7 @@ import (
"text/template"
"github.com/kovetskiy/mark/v16/confluence"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
"github.com/reconquest/karma-go"
)
@ -42,7 +42,7 @@ func templates(api *confluence.API) (*template.Template, error) {
}
user, err := api.GetUserByName(name)
if err != nil {
log.Error(err)
log.Error().Err(err).Send()
}
return user

View File

@ -7,9 +7,9 @@ import (
"path/filepath"
"strings"
"github.com/kovetskiy/lorg"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
mark "github.com/kovetskiy/mark/v16"
"github.com/reconquest/pkg/log"
"github.com/urfave/cli/v3"
)
@ -18,14 +18,54 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
return err
}
if cmd.String("color") == "never" {
log.GetLogger().SetFormat(
lorg.NewFormat(
`${time:2006-01-02 15:04:05.000} ${level:%s:left:true} ${prefix}%s`,
),
)
log.GetLogger().SetOutput(os.Stderr)
zerolog.TimeFieldFormat = "2006-01-02 15:04:05.000"
output := zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "2006-01-02 15:04:05.000",
FormatLevel: func(i interface{}) string {
var l string
if ll, ok := i.(string); ok {
switch ll {
case "trace":
l = "TRACE"
case "debug":
l = "DEBUG"
case "info":
l = "INFO"
case "warn":
l = "WARNING"
case "error":
l = "ERROR"
case "fatal":
l = "FATAL"
case "panic":
l = "PANIC"
default:
l = strings.ToUpper(ll)
}
} else {
l = strings.ToUpper(fmt.Sprintf("%s", i))
}
return l
},
FormatFieldName: func(i interface{}) string {
return ""
},
FormatFieldValue: func(i interface{}) string {
return fmt.Sprintf("%s", i)
},
FormatErrFieldName: func(i interface{}) string {
return ""
},
FormatErrFieldValue: func(i interface{}) string {
return fmt.Sprintf("%s", i)
},
}
if cmd.String("color") == "never" {
output.NoColor = true
}
log.Logger = zerolog.New(output).With().Timestamp().Logger()
creds, err := GetCredentials(
cmd.String("username"),
@ -38,13 +78,13 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
return err
}
log.Debug("config:")
log.Debug().Msg("config:")
for _, f := range cmd.Flags {
flag := f.Names()
if flag[0] == "password" {
log.Debugf(nil, "%20s: %v", flag[0], "******")
log.Debug().Msgf("%20s: %v", flag[0], "******")
} else {
log.Debugf(nil, "%20s: %v", flag[0], cmd.Value(flag[0]))
log.Debug().Msgf("%20s: %v", flag[0], cmd.Value(flag[0]))
}
}
@ -93,7 +133,7 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
func ConfigFilePath() string {
fp, err := os.UserConfigDir()
if err != nil {
log.Fatal(err)
log.Fatal().Err(err).Send()
}
return filepath.Join(fp, "mark.toml")
}
@ -101,18 +141,18 @@ func ConfigFilePath() string {
func SetLogLevel(cmd *cli.Command) error {
logLevel := cmd.String("log-level")
switch strings.ToUpper(logLevel) {
case lorg.LevelTrace.String():
log.SetLevel(lorg.LevelTrace)
case lorg.LevelDebug.String():
log.SetLevel(lorg.LevelDebug)
case lorg.LevelInfo.String():
log.SetLevel(lorg.LevelInfo)
case lorg.LevelWarning.String():
log.SetLevel(lorg.LevelWarning)
case lorg.LevelError.String():
log.SetLevel(lorg.LevelError)
case lorg.LevelFatal.String():
log.SetLevel(lorg.LevelFatal)
case "TRACE":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARNING":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
default:
return fmt.Errorf("unknown log level: %s", logLevel)
}

View File

@ -4,7 +4,7 @@ import (
"context"
"testing"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
)
@ -83,22 +83,22 @@ func Test_setLogLevel(t *testing.T) {
}
tests := map[string]struct {
args args
want log.Level
want zerolog.Level
expectedErr string
}{
"invalid": {args: args{lvl: "INVALID"}, want: log.LevelInfo, expectedErr: "unknown log level: INVALID"},
"empty": {args: args{lvl: ""}, want: log.LevelInfo, expectedErr: "unknown log level: "},
"info": {args: args{lvl: log.LevelInfo.String()}, want: log.LevelInfo},
"debug": {args: args{lvl: log.LevelDebug.String()}, want: log.LevelDebug},
"trace": {args: args{lvl: log.LevelTrace.String()}, want: log.LevelTrace},
"warning": {args: args{lvl: log.LevelWarning.String()}, want: log.LevelWarning},
"error": {args: args{lvl: log.LevelError.String()}, want: log.LevelError},
"fatal": {args: args{lvl: log.LevelFatal.String()}, want: log.LevelFatal},
"invalid": {args: args{lvl: "INVALID"}, want: zerolog.InfoLevel, expectedErr: "unknown log level: INVALID"},
"empty": {args: args{lvl: ""}, want: zerolog.InfoLevel, expectedErr: "unknown log level: "},
"info": {args: args{lvl: "INFO"}, want: zerolog.InfoLevel},
"debug": {args: args{lvl: "DEBUG"}, want: zerolog.DebugLevel},
"trace": {args: args{lvl: "TRACE"}, want: zerolog.TraceLevel},
"warning": {args: args{lvl: "WARNING"}, want: zerolog.WarnLevel},
"error": {args: args{lvl: "ERROR"}, want: zerolog.ErrorLevel},
"fatal": {args: args{lvl: "FATAL"}, want: zerolog.FatalLevel},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
prev := log.GetLevel()
t.Cleanup(func() { log.SetLevel(prev) })
prev := zerolog.GlobalLevel()
t.Cleanup(func() { zerolog.SetGlobalLevel(prev) })
cmd := &cli.Command{
Name: "test",
Flags: []cli.Flag{
@ -114,7 +114,7 @@ func Test_setLogLevel(t *testing.T) {
assert.EqualError(t, err, tt.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, log.GetLevel())
assert.Equal(t, tt.want, zerolog.GlobalLevel())
}
})
}

View File

@ -1,9 +1,7 @@
package util
import (
"fmt"
"github.com/reconquest/pkg/log"
"github.com/rs/zerolog/log"
)
type FatalErrorHandler struct {
@ -20,15 +18,15 @@ func (h *FatalErrorHandler) Handle(err error, format string, args ...interface{}
if err == nil {
if h.ContinueOnError {
log.Error(fmt.Sprintf(format, args...))
log.Error().Msgf(format, args...)
return
}
log.Fatal(fmt.Sprintf(format, args...))
log.Fatal().Msgf(format, args...)
}
if h.ContinueOnError {
log.Errorf(err, format, args...)
log.Error().Err(err).Msgf(format, args...)
return
}
log.Fatalf(err, format, args...)
log.Fatal().Err(err).Msgf(format, args...)
}