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

@ -11,11 +11,11 @@ import (
"regexp"
"strings"
"github.com/rs/zerolog/log"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/caldav"
"github.com/emersion/go-webdav/carddav"
"git.sr.ht/~sircmpwn/tokidoki/debug"
)
type filesystemBackend struct {
@ -83,7 +83,7 @@ func (b *filesystemBackend) safeLocalPath(homeSetPath string, urlPath string) (s
}
// only accept simple file names for now
if !validFilenameRegex.MatchString(file) {
debug.Printf("%s does not match regex!\n", file)
log.Debug().Str("file", file).Msg("file name does not match regex")
err := fmt.Errorf("invalid file name: %s", file)
return "", webdav.NewHTTPError(400, err)
}

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 {

View file

@ -12,11 +12,11 @@ import (
"path"
"path/filepath"
"github.com/rs/zerolog/log"
"github.com/emersion/go-vcard"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/carddav"
"git.sr.ht/~sircmpwn/tokidoki/debug"
)
func (b *filesystemBackend) AddressbookHomeSetPath(ctx context.Context) (string, error) {
@ -95,14 +95,14 @@ func createDefaultAddressBook(path, localPath string) error {
}
func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBook, error) {
debug.Printf("filesystem.AddressBook()")
log.Debug().Msg("filesystem.AddressBook()")
localPath, err := b.localCardDAVPath(ctx, "")
if err != nil {
return nil, err
}
localPath = filepath.Join(localPath, "addressbook.json")
debug.Printf("loading addressbook from %s", localPath)
log.Debug().Str("local_path", localPath).Msg("loading addressbook")
data, readErr := ioutil.ReadFile(localPath)
if os.IsNotExist(readErr) {
@ -111,7 +111,7 @@ func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBo
return nil, err
}
urlPath = path.Join(urlPath, defaultResourceName) + "/"
debug.Printf("creating default addressbook (URL:path): %s:%s", urlPath, localPath)
log.Debug().Str("local_path", localPath).Str("url_path", urlPath).Msg("creating addressbook")
err = createDefaultAddressBook(urlPath, localPath)
if err != nil {
return nil, err
@ -131,7 +131,7 @@ func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBo
}
func (b *filesystemBackend) GetAddressObject(ctx context.Context, objPath string, req *carddav.AddressDataRequest) (*carddav.AddressObject, error) {
debug.Printf("filesystem.GetAddressObject(%s, %v)", objPath, req)
log.Debug().Str("url_path", objPath).Msg("filesystem.GetAddressObject()")
localPath, err := b.localCardDAVPath(ctx, objPath)
if err != nil {
return nil, err
@ -213,12 +213,12 @@ func (b *filesystemBackend) loadAllContacts(ctx context.Context, propFilter []st
return nil
})
debug.Printf("filesystem.loadAllContacts() returning %d results from %s", len(result), localPath)
log.Debug().Int("results", len(result)).Str("path", localPath).Msg("filesystem.loadAllContacts() successful")
return result, err
}
func (b *filesystemBackend) ListAddressObjects(ctx context.Context, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
debug.Printf("filesystem.ListAddressObjects(%v)", req)
log.Debug().Msg("filesystem.ListAddressObjects()")
var propFilter []string
if req != nil && !req.AllProp {
propFilter = req.Props
@ -228,7 +228,7 @@ func (b *filesystemBackend) ListAddressObjects(ctx context.Context, req *carddav
}
func (b *filesystemBackend) QueryAddressObjects(ctx context.Context, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
debug.Printf("filesystem.QueryAddressObjects(%v)", query)
log.Debug().Msg("filesystem.QueryAddressObjects()")
var propFilter []string
if query != nil && !query.DataRequest.AllProp {
propFilter = query.DataRequest.Props
@ -243,7 +243,7 @@ func (b *filesystemBackend) QueryAddressObjects(ctx context.Context, query *card
}
func (b *filesystemBackend) PutAddressObject(ctx context.Context, objPath string, card vcard.Card, opts *carddav.PutAddressObjectOptions) (loc string, err error) {
debug.Printf("filesystem.PutAddressObject(%v, %v, %v)", objPath, card, opts)
log.Debug().Str("url_path", objPath).Msg("filesystem.PutAddressObject()")
// Object always get saved as <UID>.vcf
dirname, _ := path.Split(objPath)
@ -296,7 +296,7 @@ func (b *filesystemBackend) PutAddressObject(ctx context.Context, objPath string
}
func (b *filesystemBackend) DeleteAddressObject(ctx context.Context, path string) error {
debug.Printf("filesystem.DeleteAddressObject(%s)", path)
log.Debug().Str("url_path", path).Msg("filesystem.DeleteAddressObject()")
localPath, err := b.localCardDAVPath(ctx, path)
if err != nil {