Skip to content

Commit

Permalink
logging: make default logging less verbose, add useful logs
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Dec 19, 2024
1 parent c42ce1b commit 5b66978
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 7 additions & 4 deletions cmd/calendarsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func main() {
}

func Run(c *cli.Context) error {
log.Info("started calendarsync")

if c.Bool(flagVersion) {
fmt.Println("Version:", Version)
os.Exit(0)
Expand All @@ -111,7 +113,7 @@ func Run(c *cli.Context) error {
if err != nil {
return err
}
log.Info("loaded config file", "path", cfg.Path)
log.Debug("loaded config file", "path", cfg.Path)

if len(c.String(flagStorageEncryptionKey)) > 0 {
log.Warn("Parsing the encryption key using the flag is deprecated. Please use the environment variable $CALENDARSYNC_ENCRYPTION_KEY instead.")
Expand Down Expand Up @@ -163,7 +165,7 @@ func Run(c *cli.Context) error {
if err != nil {
return err
}
log.Info("loaded source adapter", "adapter", cfg.Source.Adapter.Type, "calendar", cfg.Source.Adapter.Calendar)
log.Debug("loaded source adapter", "adapter", cfg.Source.Adapter.Type, "calendar", cfg.Source.Adapter.Calendar)

sinkLogger := log.With("adapter", cfg.Sink.Adapter.Type, "type", "sink")

Expand All @@ -178,7 +180,7 @@ func Run(c *cli.Context) error {
if err != nil {
return err
}
log.Info("loaded sink adapter", "adapter", cfg.Sink.Adapter.Type, "calendar", cfg.Sink.Adapter.Calendar)
log.Debug("loaded sink adapter", "adapter", cfg.Sink.Adapter.Type, "calendar", cfg.Sink.Adapter.Calendar)

if log.GetLevel() == log.DebugLevel {
for _, transformation := range cfg.Transformations {
Expand All @@ -190,7 +192,7 @@ func Run(c *cli.Context) error {
if cfg.UpdateConcurrency != 0 {
controller.SetConcurrency(cfg.UpdateConcurrency)
}
log.Info("loaded sync controller")
log.Debug("loaded sync controller")

if c.Bool("clean") {
err = controller.CleanUp(c.Context, startTime, endTime)
Expand All @@ -204,5 +206,6 @@ func Run(c *cli.Context) error {
log.Fatalf("we had some errors during synchronization:\n%v", err)
}
}
log.Info("sync complete")
return nil
}
4 changes: 2 additions & 2 deletions internal/adapter/google/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
}

c.authenticated = true
c.logger.Info("using stored credentials")
c.logger.Debug("using stored credentials")
}

return nil
Expand Down Expand Up @@ -178,7 +178,7 @@ func (c *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time, en
return nil, err
}

c.logger.Infof("loaded %d events between %s and %s.", len(events), start.Format(time.RFC1123), end.Format(time.RFC1123))
c.logger.Infof("loaded %d events between %s and %s.", len(events), start.Format(time.DateOnly), end.Format(time.DateOnly))

return events, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
}

c.authenticated = true
c.logger.Info("using stored credentials")
c.logger.Debug("using stored credentials")
c.logger.Debugf("expiry time of stored token: %s", expiry.String())
}

Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time, en
return nil, err
}

c.logger.Infof("loaded %d events between %s and %s.", len(events), start.Format(time.RFC1123), end.Format(time.RFC1123))
c.logger.Infof("loaded %d events between %s and %s.", len(events), start.Format(time.DateOnly), end.Format(time.DateOnly))

return events, nil
}
Expand Down
9 changes: 9 additions & 0 deletions internal/adapter/zep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ type CalendarAPI struct {
client *caldav.Client

calendarID string
logger *log.Logger

principal string
homeSet string
}

// Assert that the expected interfaces are implemented
var _ port.Configurable = &CalendarAPI{}
var _ port.LogSetter = &CalendarAPI{}

func (zep *CalendarAPI) GetCalendarID() string {
return zep.generateCalendarID()
Expand Down Expand Up @@ -100,6 +102,9 @@ func (zep *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time,
}

var syncEvents []models.Event

zep.logger.Infof("loaded %d events between %s and %s.", len(absences), start.Format(time.DateOnly), end.Format(time.DateOnly))

for _, v := range absences {
syncEvents = append(syncEvents,
models.Event{
Expand Down Expand Up @@ -209,3 +214,7 @@ func safeGetComponentPropValueString(event ical.Event, key string) string {
}
return prop.Value
}

func (zep *CalendarAPI) SetLogger(logger *log.Logger) {
zep.logger = logger
}
7 changes: 4 additions & 3 deletions internal/sync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p Controller) SynchroniseTimeframe(ctx context.Context, start time.Time, e
filteredEventsInSource := []models.Event{}

for _, filter := range p.filters {
p.logger.Info("loaded filter", "name", filter.Name())
p.logger.Debug("loaded filter", "name", filter.Name())
}

for _, event := range eventsInSource {
Expand All @@ -96,14 +96,15 @@ func (p Controller) SynchroniseTimeframe(ctx context.Context, start time.Time, e

// Output which transformers were loaded
for _, trans := range p.transformers {
p.logger.Info("loaded transformer", "name", trans.Name())
p.logger.Debug("loaded transformer", "name", trans.Name())
}

for _, event := range filteredEventsInSource {
transformedEventsInSource = append(transformedEventsInSource, TransformEvent(event, p.transformers...))
}

toCreate, toUpdate, toDelete := p.diffEvents(transformedEventsInSource, eventsInSink)
log.Infof("found %d new, %d changed, and %d deleted events", len(toCreate), len(toUpdate), len(toDelete))
if dryRun {
p.logger.Warn("we're running in dry run mode, no changes will be executed")
return nil
Expand Down Expand Up @@ -200,7 +201,7 @@ func (p Controller) diffEvents(sourceEvents []models.Event, sinkEvents []models.
// - Delete event (in calendar A)
// - Run sync from calendar B to calendar A. This will copy (and thereby resurrect) the event.
//
// Solution: Ignore events the originate from the sink, but no longer exist there.
// Solution: Ignore events that originate from the sink, but no longer exist there.
if event.Metadata.SourceID == p.sink.GetCalendarID() {
p.logger.Info("skipping event as it originates from the sink, but no longer exists there", logFields(event)...)
continue
Expand Down

0 comments on commit 5b66978

Please sign in to comment.