Skip to content

Commit

Permalink
fix(audit): logs not getting deleted after N days (hypermodeinc#7567)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza authored and aman-bansal committed Mar 16, 2021
1 parent 2905756 commit 96bf99e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ee/audit/audit_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (
Http = "Http"
)

var auditor *auditLogger = &auditLogger{}
var auditor = &auditLogger{}

type auditLogger struct {
log *x.Logger
Expand Down
5 changes: 1 addition & 4 deletions x/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ func ParseAttrList(attrs []string) []string {

func IsReverseAttr(attr string) bool {
AssertTrue(len(attr) >= 8)
if attr[8] == '~' {
return true
}
return false
return attr[8] == '~'
}

func FormatNsAttr(attr string) string {
Expand Down
13 changes: 8 additions & 5 deletions x/log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (l *LogWriter) open() error {
func backupName(name string) string {
dir := filepath.Dir(name)
prefix, ext := prefixAndExt(name)
timestamp := time.Now().Format(backupTimeFormat)
timestamp := time.Now().UTC().Format(backupTimeFormat)
return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext))
}

Expand Down Expand Up @@ -341,7 +341,7 @@ func (l *LogWriter) manageOldLogs() {
return
}

toRemove, toKeep, err := processOldLogFiles(l.FilePath, l.MaxSize)
toRemove, toKeep, err := processOldLogFiles(l.FilePath, l.MaxAge)
if err != nil {
return
}
Expand Down Expand Up @@ -373,6 +373,8 @@ func (l *LogWriter) manageOldLogs() {
}
}

// prefixAndExt extracts the filename and extension from a filepath.
// eg. prefixAndExt("/home/foo/file.ext") would return ("file", ".ext").
func prefixAndExt(file string) (prefix, ext string) {
filename := filepath.Base(file)
ext = filepath.Ext(filename)
Expand All @@ -393,8 +395,8 @@ func processOldLogFiles(fp string, maxAge int64) ([]string, []string, error) {
toRemove := make([]string, 0)
toKeep := make([]string, 0)

diff := time.Duration(int64(24*time.Hour) * int64(maxAge))
cutoff := time.Now().Add(-1 * diff)
diff := 24 * time.Hour * time.Duration(maxAge)
cutoff := time.Now().Add(-diff)

for _, f := range files {
if f.IsDir() || // f is directory
Expand All @@ -404,7 +406,8 @@ func processOldLogFiles(fp string, maxAge int64) ([]string, []string, error) {
}

_, e := prefixAndExt(fp)
ts, err := time.Parse(backupTimeFormat, f.Name()[len(defPrefix):len(f.Name())-len(e)])
tsString := f.Name()[len(defPrefix) : len(f.Name())-len(e)]
ts, err := time.Parse(backupTimeFormat, tsString)
if err != nil {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions x/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (l *Logger) AuditI(msg string, args ...interface{}) {
if l == nil {
return
}
flds := make([]zap.Field, 0)
flds := make([]zap.Field, 0, len(args))
for i := 0; i < len(args); i = i + 2 {
flds = append(flds, zap.Any(args[i].(string), args[i+1]))
}
Expand All @@ -97,7 +97,7 @@ func (l *Logger) AuditE(msg string, args ...interface{}) {
if l == nil {
return
}
flds := make([]zap.Field, 0)
flds := make([]zap.Field, 0, len(args))
for i := 0; i < len(args); i = i + 2 {
flds = append(flds, zap.Any(args[i].(string), args[i+1]))
}
Expand Down

0 comments on commit 96bf99e

Please sign in to comment.