fix: Allow configuring number of folders of old logs to keep (#26398)

Co-authored-by: Filip Vilicic <kusefin@hotmail.com>
This commit is contained in:
Filip
2025-02-17 14:04:39 +01:00
committed by GitHub
co-authored by Filip Vilicic
parent dec5ca8b3a
commit 845e03e48a
6 changed files with 28 additions and 2 deletions
+1
View File
@@ -178,6 +178,7 @@ declare global {
log_syslog: KeyValue;
log_debug_to_mqtt_frontend: boolean;
log_debug_namespace_ignore: string;
log_directories_to_keep: number;
pan_id: number | 'GENERATE';
ext_pan_id: number[] | 'GENERATE';
channel: number;
+1 -1
View File
@@ -232,7 +232,7 @@ class Logger {
});
directories.sort((a: KeyValue, b: KeyValue) => b.birth - a.birth);
directories = directories.slice(10, directories.length);
directories = directories.slice(settings.get().advanced.log_directories_to_keep, directories.length);
directories.forEach((dir) => {
this.debug(`Removing old log directory '${dir.path}'`);
rimrafSync(dir.path);
+8
View File
@@ -525,6 +525,14 @@
"default": "",
"examples": ["^zhc:legacy:fz:(tuya|moes)", "^zhc:legacy:fz:(tuya|moes)|^zh:ember:uart:|^zh:controller"]
},
"log_directories_to_keep": {
"type": "number",
"title": "Number of past log folders to keep",
"description": "Number of log directories to keep before deleting the oldest one",
"default": 10,
"minimum": 5,
"maximum": 1000
},
"log_syslog": {
"requiresRestart": true,
"oneOf": [
+1
View File
@@ -96,6 +96,7 @@ export const defaults: RecursivePartial<Settings> = {
log_syslog: {},
log_debug_to_mqtt_frontend: false,
log_debug_namespace_ignore: '',
log_directories_to_keep: 10,
pan_id: 0x1a62,
ext_pan_id: [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd],
channel: 11,
+1
View File
@@ -122,6 +122,7 @@ describe('Extension: Bridge', () => {
log_rotation: true,
log_symlink_current: false,
log_syslog: {},
log_directories_to_keep: 10,
output: 'json',
pan_id: 6754,
timestamp_format: 'YYYY-MM-DD HH:mm:ss',
+16 -1
View File
@@ -46,7 +46,7 @@ describe('Logger', () => {
expect(dirs.length).toBe(1);
});
it('Should cleanup', () => {
it('Should cleanup (default setting)', () => {
for (const d of fs.readdirSync(dir.name)) {
rimrafSync(path.join(dir.name, d));
}
@@ -60,6 +60,21 @@ describe('Logger', () => {
expect(fs.readdirSync(dir.name).length).toBe(10);
});
it('Should cleanup (15 folders setting)', () => {
for (const d of fs.readdirSync(dir.name)) {
rimrafSync(path.join(dir.name, d));
}
for (let i = 0; i < 20; i++) {
fs.mkdirSync(path.join(dir.name, `log_${i}`));
}
settings.set(['advanced', 'log_directories_to_keep'], 15);
expect(fs.readdirSync(dir.name).length).toBe(20);
logger.init();
expect(fs.readdirSync(dir.name).length).toBe(15);
});
it('Should not cleanup when there is no timestamp set', () => {
for (const d of fs.readdirSync(dir.name)) {
rimrafSync(path.join(dir.name, d));