From 9af949fadfb83e9edb22b116ba6fa0dd8217b63f Mon Sep 17 00:00:00 2001 From: Ondrej Sika Date: Sat, 2 Oct 2021 11:15:09 +0200 Subject: [PATCH] refactor(do_backup): Move logs to helper functions & enhance messages --- do_backup/do_backup.go | 60 ++++---------------- do_backup/log_helpers.go | 118 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 48 deletions(-) create mode 100644 do_backup/log_helpers.go diff --git a/do_backup/do_backup.go b/do_backup/do_backup.go index f126ac3..c66ef85 100644 --- a/do_backup/do_backup.go +++ b/do_backup/do_backup.go @@ -47,44 +47,26 @@ func DoBackup(configPath, extraName string) { for _, b := range config.Backups { // Backup source - log.Info(). - Str("phase", "backup-start"). - Str("id", b.ID). - Msg("Start backing up " + b.ID + " ...") + logBackupStart(b) data, err := b.Source.Backup() if err != nil { bl.SaveEvent(b.ID, "---", err) - log.Warn(). - Str("phase", "backup-finish-err"). - Str("id", b.ID). - Msg("Backup failed " + b.ID + ": " + err.Error()) + logBackupFailed(b, err) continue } - log.Info(). - Str("phase", "backup-finish-ok"). - Str("id", b.ID). - Msg("Finish backing up " + b.ID) + logBackupDone(b) // Process Backup's Middlewares var errBackupMiddleware error = nil for _, m := range b.Middlewares { - log.Info(). - Str("phase", "backup-middleware-start"). - Str("id", b.ID). - Msg("Start backup middleware") + logBackupMiddlewareStart(b, m) data, errBackupMiddleware = m.Process(data) if errBackupMiddleware != nil { bl.SaveEvent(b.ID, "---", errBackupMiddleware) - log.Warn(). - Str("phase", "backup-middleware-err"). - Str("id", b.ID). - Msg("Backup middleware failed: " + errBackupMiddleware.Error()) + logBackupMiddlewareFailed(b, m, err) continue } - log.Info(). - Str("phase", "backup-middleware-ok"). - Str("id", b.ID). - Msg("Finish backup middleware " + b.ID) + logBackupMiddlewareDone(b, m) } if errBackupMiddleware != nil { @@ -98,47 +80,29 @@ func DoBackup(configPath, extraName string) { // Process Targets's Middlewares var errTargetMiddleware error = nil for _, m := range t.Middlewares { - log.Info(). - Str("phase", "target-middleware-start"). - Str("id", t.ID). - Msg("Start target middleware") + logTargetMiddlewareStart(b, t, m) targetData, errTargetMiddleware = m.Process(targetData) if errTargetMiddleware != nil { bl.SaveEvent(b.ID, t.ID, errTargetMiddleware) - log.Warn(). - Str("phase", "target-middleware-err"). - Str("id", t.ID). - Msg("Target middleware failed: " + errTargetMiddleware.Error()) + logTargetMiddlewareFailed(b, t, m, errTargetMiddleware) continue } - log.Info(). - Str("phase", "target-middleware-ok"). - Str("id", t.ID). - Msg("Finish target middleware " + t.ID) + logTargetMiddlewareDone(b, t, m) } if errTargetMiddleware != nil { continue } // Save backup to target - log.Info(). - Str("phase", "save-start"). - Str("id", t.ID). - Msg("Start save " + t.ID) + logTargetStart(b, t) err = t.Save(targetData) if err != nil { bl.SaveEvent(b.ID, t.ID, err) - log.Warn(). - Str("phase", "save-err"). - Str("id", t.ID). - Msg("Save " + t.ID + " failed: " + err.Error()) + logTargetFailed(b, t, err) continue } bl.SaveEvent(b.ID, t.ID, err) - log.Info(). - Str("phase", "save-ok"). - Str("id", t.ID). - Msg("Finish save " + t.ID) + logTargetDone(b, t) } } diff --git a/do_backup/log_helpers.go b/do_backup/log_helpers.go new file mode 100644 index 0000000..1900490 --- /dev/null +++ b/do_backup/log_helpers.go @@ -0,0 +1,118 @@ +package do_backup + +import ( + "github.com/rs/zerolog/log" + "github.com/sikalabs/tergum/backup" + "github.com/sikalabs/tergum/backup/middleware" + "github.com/sikalabs/tergum/backup/target" +) + +const PHASE_START = "START" +const PHASE_DONE = "DONE" +const PHASE_FAILED = "FAILED" + +func getMiddlewareID(m middleware.Middleware) string { + if m.Gzip != nil { + return "gzip" + } + return "" +} + +func metaLog( + b *backup.Backup, + t *target.Target, + m *middleware.Middleware, + method string, + phase string, + message string, +) { + backup_id := "" + scope := "" + if b != nil { + backup_id = b.ID + scope = backup_id + } + target_id := "" + if t != nil { + target_id = t.ID + scope = backup_id + "/" + target_id + } + middleware_id := "" + if m != nil { + middleware_id = getMiddlewareID(*m) + scope = scope + "+" + middleware_id + } + + message_space := "" + if message != "" { + message_space = " " + } + + log.Info(). + Str("method", method). + Str("phase", phase). + Str("backup_id", backup_id). + Str("target_id", target_id). + Str("middleware_id", middleware_id). + Msg(phase + "/" + method + "(" + scope + ")" + message_space + message) +} + +func logBackupStart(b backup.Backup) { + metaLog(&b, nil, nil, "BACKUP", PHASE_START, + "") +} + +func logBackupDone(b backup.Backup) { + metaLog(&b, nil, nil, "BACKUP", PHASE_DONE, + "Backup "+b.ID+" finished.") +} + +func logBackupFailed(b backup.Backup, err error) { + metaLog(&b, nil, nil, "BACKUP", PHASE_FAILED, + "Backup "+b.ID+" failed: "+err.Error()) +} + +func logTargetStart(b backup.Backup, t target.Target) { + metaLog(&b, &t, nil, "TARGET", PHASE_START, + "") +} + +func logTargetDone(b backup.Backup, t target.Target) { + metaLog(&b, &t, nil, "TARGET", PHASE_DONE, + "Target "+b.ID+" finished.") +} + +func logTargetFailed(b backup.Backup, t target.Target, err error) { + metaLog(&b, &t, nil, "TARGET", PHASE_FAILED, + "Backup "+b.ID+" failed: "+err.Error()) +} + +func logTargetMiddlewareStart(b backup.Backup, t target.Target, m middleware.Middleware) { + metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_START, + "") +} + +func logTargetMiddlewareDone(b backup.Backup, t target.Target, m middleware.Middleware) { + metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_DONE, + "") +} + +func logTargetMiddlewareFailed(b backup.Backup, t target.Target, m middleware.Middleware, err error) { + metaLog(&b, &t, &m, "TARGET_MIDDLEWARE", PHASE_FAILED, + "Backup "+b.ID+" failed: "+err.Error()) +} + +func logBackupMiddlewareStart(b backup.Backup, m middleware.Middleware) { + metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_START, + "") +} + +func logBackupMiddlewareDone(b backup.Backup, m middleware.Middleware) { + metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_DONE, + "") +} + +func logBackupMiddlewareFailed(b backup.Backup, m middleware.Middleware, err error) { + metaLog(&b, nil, &m, "BACKUP_MIDDLEWARE", PHASE_FAILED, + "Backup "+b.ID+" failed: "+err.Error()) +}