github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_local/logs.go (about)

     1  package db_local
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/turbot/steampipe/pkg/filepaths"
    10  )
    11  
    12  const logRetentionDays = 7
    13  
    14  func TrimLogs() {
    15  	fileLocation := filepaths.EnsureLogDir()
    16  	files, err := os.ReadDir(fileLocation)
    17  	if err != nil {
    18  		log.Println("[TRACE] error listing db log directory", err)
    19  	}
    20  	for _, file := range files {
    21  		fi, err := file.Info()
    22  		if err != nil {
    23  			log.Printf("[TRACE] error reading file info of %s. continuing\n", file.Name())
    24  			continue
    25  		}
    26  
    27  		fileName := fi.Name()
    28  		if filepath.Ext(fileName) != ".log" {
    29  			continue
    30  		}
    31  
    32  		age := time.Since(fi.ModTime()).Hours()
    33  		if age > logRetentionDays*24 {
    34  			logPath := filepath.Join(fileLocation, fileName)
    35  			err := os.Remove(logPath)
    36  			if err != nil {
    37  				log.Printf("[TRACE] failed to delete log file %s\n", logPath)
    38  			}
    39  		}
    40  	}
    41  }