Skip to content

Commit

Permalink
Add more timestamp data to log messages (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jul 19, 2022
1 parent 977ac63 commit 539993a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
22 changes: 21 additions & 1 deletion pkg/gcrcleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ func (c *Cleaner) Clean(ctx context.Context, repo string, since time.Time, keep
return manifests[j].Info.Created.Before(manifests[i].Info.Created)
})

// Generate an ordered map
manifestListForLog := make([]map[string]any, len(manifests))
for _, m := range manifests {
manifestListForLog = append(manifestListForLog, map[string]any{
"repo": m.Repo,
"digest": m.Digest,
"tags": m.Info.Tags,
"created": m.Info.Created.Format(time.RFC3339),
"uploaded": m.Info.Uploaded.Format(time.RFC3339),
})
}
c.logger.Debug("computed all manifests",
"keep", keep,
"manifests", manifestListForLog)

for _, m := range manifests {
// Store copy of manifest for thread safety in delete job pool
m := m
Expand All @@ -91,6 +106,7 @@ func (c *Cleaner) Clean(ctx context.Context, repo string, since time.Time, keep
"repo", repo,
"digest", m.Digest,
"tags", m.Info.Tags,
"created", m.Info.Created.Format(time.RFC3339),
"uploaded", m.Info.Uploaded.Format(time.RFC3339))

if c.shouldDelete(m, since, tagFilter) {
Expand All @@ -100,7 +116,9 @@ func (c *Cleaner) Clean(ctx context.Context, repo string, since time.Time, keep
"repo", repo,
"digest", m.Digest,
"keep", keep,
"keep_count", keepCount)
"keep_count", keepCount,
"created", m.Info.Created.Format(time.RFC3339),
"uploaded", m.Info.Uploaded.Format(time.RFC3339))

keepCount++
continue
Expand Down Expand Up @@ -212,6 +230,7 @@ func (c *Cleaner) shouldDelete(m *manifest, since time.Time, tagFilter TagFilter
"digest", m.Digest,
"reason", "too new",
"since", since.Format(time.RFC3339),
"created", m.Info.Created.Format(time.RFC3339),
"uploaded", uploaded.Format(time.RFC3339),
"delta", uploaded.Sub(since).String())
return false
Expand All @@ -234,6 +253,7 @@ func (c *Cleaner) shouldDelete(m *manifest, since time.Time, tagFilter TagFilter
"repo", m.Repo,
"digest", m.Digest,
"reason", "matches tag filter",
"tags", m.Info.Tags,
"tag_filter", tagFilter.Name())
return true
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/gcrcleaner/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,28 @@ func NewLogger(level string, outw, errw io.Writer) *Logger {
return &Logger{level: v, stdout: outw, stderr: errw}
}

func (l *Logger) Debug(msg string, fields ...interface{}) {
func (l *Logger) Debug(msg string, fields ...any) {
l.log(l.stdout, msg, SeverityDebug, fields...)
}

func (l *Logger) Info(msg string, fields ...interface{}) {
func (l *Logger) Info(msg string, fields ...any) {
l.log(l.stdout, msg, SeverityInfo, fields...)
}

func (l *Logger) Warn(msg string, fields ...interface{}) {
func (l *Logger) Warn(msg string, fields ...any) {
l.log(l.stdout, msg, SeverityWarn, fields...)
}

func (l *Logger) Error(msg string, fields ...interface{}) {
func (l *Logger) Error(msg string, fields ...any) {
l.log(l.stderr, msg, SeverityError, fields...)
}

func (l *Logger) Fatal(msg string, fields ...interface{}) {
func (l *Logger) Fatal(msg string, fields ...any) {
l.log(l.stderr, msg, SeverityFatal, fields...)
os.Exit(1)
}

func (l *Logger) log(w io.Writer, msg string, sev Severity, fields ...interface{}) {
func (l *Logger) log(w io.Writer, msg string, sev Severity, fields ...any) {
if len(fields)%2 != 0 {
panic("number of fields must be even")
}
Expand All @@ -107,7 +107,7 @@ func (l *Logger) log(w io.Writer, msg string, sev Severity, fields ...interface{
return
}

data := make(map[string]interface{}, len(fields)/2)
data := make(map[string]any, len(fields)/2)
for i := 0; i < len(fields); i += 2 {
key, ok := fields[i].(string)
if !ok {
Expand Down Expand Up @@ -141,11 +141,11 @@ type LogEntry struct {
Time *time.Time
Severity Severity
Message string
Data map[string]interface{}
Data map[string]any
}

func (l *LogEntry) MarshalJSON() ([]byte, error) {
d := make(map[string]interface{}, 8)
d := make(map[string]any, 8)

if l.Time != nil {
d["time"] = l.Time.Format(time.RFC3339)
Expand Down
6 changes: 3 additions & 3 deletions pkg/gcrcleaner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (s sortedStringSlice) MarshalJSON() ([]byte, error) {
}

func (s *sortedStringSlice) UnmarshalJSON(b []byte) error {
var v interface{}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
Expand All @@ -304,7 +304,7 @@ func (s *sortedStringSlice) UnmarshalJSON(b []byte) error {
if t := strings.TrimSpace(val); t != "" {
m[t] = struct{}{}
}
case []interface{}:
case any:
for i, v := range val {
s, ok := v.(string)
if !ok {
Expand Down Expand Up @@ -341,7 +341,7 @@ func (d duration) MarshalJSON() ([]byte, error) {
}

func (d *duration) UnmarshalJSON(b []byte) error {
var v interface{}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
Expand Down

0 comments on commit 539993a

Please sign in to comment.