[lxc-devel] [lxd/master] lxd/logging: Updates log rotate to only remove .log files

tomponline on Github lxc-bot at linuxcontainers.org
Wed Feb 26 09:43:51 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 448 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200226/b519652d/attachment.bin>
-------------- next part --------------
From 7704acc6d66ff832c2ef581ba59af8be6d2dd63e Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Wed, 26 Feb 2020 09:42:54 +0000
Subject: [PATCH] lxd/logging: Updates log rotate to only remove .log files

This allows non-log files to be kept, such as .monitor and .pid files used for VMs.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/logging.go | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/lxd/logging.go b/lxd/logging.go
index 86787358dd..21302a9d8e 100644
--- a/lxd/logging.go
+++ b/lxd/logging.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"io/ioutil"
 	"os"
+	"strings"
 	"time"
 
 	"github.com/lxc/lxd/lxd/db"
@@ -93,25 +94,19 @@ func expireLogs(ctx context.Context, state *state.State) error {
 			continue
 		}
 
-		// Check if the container still exists.
+		// Check if the instance still exists.
 		if shared.StringInSlice(entry.Name(), names) {
-			// Remove any log file which wasn't modified in the past 48 hours.
-			logs, err := ioutil.ReadDir(shared.LogPath(entry.Name()))
+			instDirEntries, err := ioutil.ReadDir(shared.LogPath(entry.Name()))
 			if err != nil {
 				return err
 			}
 
-			for _, logfile := range logs {
-				path := shared.LogPath(entry.Name(), logfile.Name())
-
-				// Always keep the config files.
-				if logfile.Name() == "lxc.conf" || logfile.Name() == "qemu.conf" {
-					continue
-				}
+			for _, instDirEntry := range instDirEntries {
+				path := shared.LogPath(entry.Name(), instDirEntry.Name())
 
 				// Deal with directories (snapshots).
-				if logfile.IsDir() {
-					newest := newestFile(path, logfile)
+				if instDirEntry.IsDir() {
+					newest := newestFile(path, instDirEntry)
 					if time.Since(newest).Hours() >= 48 {
 						err := os.RemoveAll(path)
 						if err != nil {
@@ -122,8 +117,13 @@ func expireLogs(ctx context.Context, state *state.State) error {
 					continue
 				}
 
-				// Individual files.
-				if time.Since(logfile.ModTime()).Hours() >= 48 {
+				// Only remove old log files (keep other files, such as conf, pid, monitor etc).
+				if !strings.HasSuffix(instDirEntry.Name(), ".log") {
+					continue
+				}
+
+				// Remove any log file which wasn't modified in the past 48 hours.
+				if time.Since(instDirEntry.ModTime()).Hours() >= 48 {
 					err := os.Remove(path)
 					if err != nil {
 						return err


More information about the lxc-devel mailing list