-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(do_backup): Move logs to helper functions & enhance messages
- Loading branch information
1 parent
8c910b5
commit 9af949f
Showing
2 changed files
with
130 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |