Switch to a proper logging library

Structured logs can be enabled with `-log.json`.
This commit is contained in:
Conrad Hoffmann 2022-12-01 12:00:42 +01:00
parent 03fce79e57
commit 40bae8dc31
9 changed files with 75 additions and 64 deletions

View file

@ -12,11 +12,11 @@ import (
"path"
"path/filepath"
"github.com/rs/zerolog/log"
"github.com/emersion/go-ical"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/caldav"
"git.sr.ht/~sircmpwn/tokidoki/debug"
)
func (b *filesystemBackend) CalendarHomeSetPath(ctx context.Context) (string, error) {
@ -100,7 +100,7 @@ func (b *filesystemBackend) loadAllCalendars(ctx context.Context, propFilter []s
return nil
})
debug.Printf("filesystem.loadAllCalendars() returning %d results from %s", len(result), localPath)
log.Debug().Int("results", len(result)).Str("path", localPath).Msg("filesystem.loadAllCalendars() successful")
return result, err
}
@ -124,7 +124,7 @@ func createDefaultCalendar(path, localPath string) error {
}
func (b *filesystemBackend) Calendar(ctx context.Context) (*caldav.Calendar, error) {
debug.Printf("filesystem.Calendar()")
log.Debug().Msg("filesystem.Calendar()")
localPath, err := b.localCalDAVPath(ctx, "")
if err != nil {
@ -132,7 +132,7 @@ func (b *filesystemBackend) Calendar(ctx context.Context) (*caldav.Calendar, err
}
localPath = filepath.Join(localPath, "calendar.json")
debug.Printf("loading calendar from %s", localPath)
log.Debug().Str("local_path", localPath).Msg("loading calendar")
data, readErr := ioutil.ReadFile(localPath)
if os.IsNotExist(readErr) {
@ -141,7 +141,7 @@ func (b *filesystemBackend) Calendar(ctx context.Context) (*caldav.Calendar, err
return nil, err
}
urlPath = path.Join(urlPath, defaultResourceName) + "/"
debug.Printf("creating default calendar (URL:path): %s:%s", urlPath, localPath)
log.Debug().Str("local_path", localPath).Str("url_path", urlPath).Msg("creating calendar")
err = createDefaultCalendar(urlPath, localPath)
if err != nil {
return nil, err
@ -161,7 +161,7 @@ func (b *filesystemBackend) Calendar(ctx context.Context) (*caldav.Calendar, err
}
func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath string, req *caldav.CalendarCompRequest) (*caldav.CalendarObject, error) {
debug.Printf("filesystem.GetCalendarObject(%s, %v)", objPath, req)
log.Debug().Str("url_path", objPath).Msg("filesystem.GetCalendarObject()")
localPath, err := b.localCalDAVPath(ctx, objPath)
if err != nil {
@ -171,7 +171,7 @@ func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath strin
info, err := os.Stat(localPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
debug.Printf("not found: %s", localPath)
log.Debug().Str("local_path", localPath).Msg("object not found")
return nil, webdav.NewHTTPError(404, err)
}
return nil, err
@ -184,7 +184,7 @@ func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath strin
calendar, err := calendarFromFile(localPath, propFilter)
if err != nil {
debug.Printf("error reading calendar: %v", err)
log.Debug().Err(err).Msg("error reading calendar")
return nil, err
}
@ -204,7 +204,7 @@ func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath strin
}
func (b *filesystemBackend) ListCalendarObjects(ctx context.Context, req *caldav.CalendarCompRequest) ([]caldav.CalendarObject, error) {
debug.Printf("filesystem.ListCalendarObjects(%v)", req)
log.Debug().Msg("filesystem.ListCalendarObjects()")
var propFilter []string
if req != nil && !req.AllProps {
@ -215,7 +215,7 @@ func (b *filesystemBackend) ListCalendarObjects(ctx context.Context, req *caldav
}
func (b *filesystemBackend) QueryCalendarObjects(ctx context.Context, query *caldav.CalendarQuery) ([]caldav.CalendarObject, error) {
debug.Printf("filesystem.QueryCalendarObjects(%v)", query)
log.Debug().Msg("filesystem.QueryCalendarObjects()")
var propFilter []string
if query != nil && !query.CompRequest.AllProps {
@ -231,7 +231,7 @@ func (b *filesystemBackend) QueryCalendarObjects(ctx context.Context, query *cal
}
func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath string, calendar *ical.Calendar, opts *caldav.PutCalendarObjectOptions) (loc string, err error) {
debug.Printf("filesystem.PutCalendarObject(%s, %v, %v)", objPath, calendar, opts)
log.Debug().Str("url_path", objPath).Msg("filesystem.PutCalendarObject()")
_, uid, err := caldav.ValidateCalendarObject(calendar)
if err != nil {
@ -289,7 +289,7 @@ func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath strin
}
func (b *filesystemBackend) DeleteCalendarObject(ctx context.Context, path string) error {
debug.Printf("filesystem.DeleteCalendarObject(%s)", path)
log.Debug().Str("url_path", path).Msg("filesystem.DeleteCalendarObject()")
localPath, err := b.localCalDAVPath(ctx, path)
if err != nil {