replace debug() with our custom logger

mostly we want trace() and log(). trace() can be enabled whenever
we want by flipping a flag and restarting box
This commit is contained in:
Girish Ramakrishnan
2026-03-12 22:55:28 +05:30
parent d57554a48c
commit 01d0c738bc
104 changed files with 1187 additions and 1174 deletions
+22 -22
View File
@@ -6,12 +6,12 @@ import backups from './backups.js';
import backupFormats from './backupformats.js';
import backupSites from './backupsites.js';
import constants from './constants.js';
import debugModule from 'debug';
import logger from './logger.js';
import moment from 'moment';
import path from 'node:path';
import safe from 'safetydance';
const debug = debugModule('box:backupcleaner');
const { log, trace } = logger('backupcleaner');
function applyBackupRetention(allBackups, retention, referencedBackupIds) {
assert(Array.isArray(allBackups));
@@ -67,7 +67,7 @@ function applyBackupRetention(allBackups, retention, referencedBackupIds) {
}
for (const backup of allBackups) {
debug(`applyBackupRetention: ${backup.remotePath} keep/discard: ${backup.keepReason || backup.discardReason || 'unprocessed'}`);
log(`applyBackupRetention: ${backup.remotePath} keep/discard: ${backup.keepReason || backup.discardReason || 'unprocessed'}`);
}
}
@@ -88,21 +88,21 @@ async function removeBackup(site, backup, progressCallback) {
}
if (removeError) {
debug(`removeBackup: error removing backup ${removeError.message}`);
log(`removeBackup: error removing backup ${removeError.message}`);
return;
}
// remove integrity info
const [removeIntegrityError] = await safe(backupSites.storageApi(site).remove(site.config, `${remotePath}.backupinfo`));
if (removeIntegrityError) debug(`removeBackup: could not remove integrity info: ${removeIntegrityError.message}`);
if (removeIntegrityError) log(`removeBackup: could not remove integrity info: ${removeIntegrityError.message}`);
// prune empty directory if possible
const [pruneError] = await safe(backupSites.storageApi(site).remove(site.config, path.dirname(remotePath)));
if (pruneError) debug(`removeBackup: unable to prune backup directory ${path.dirname(remotePath)}: ${pruneError.message}`);
if (pruneError) log(`removeBackup: unable to prune backup directory ${path.dirname(remotePath)}: ${pruneError.message}`);
const [delError] = await safe(backups.del(backup.id));
if (delError) debug(`removeBackup: error removing ${backup.id} from database. %o`, delError);
else debug(`removeBackup: removed ${backup.remotePath}`);
if (delError) log(`removeBackup: error removing ${backup.id} from database. %o`, delError);
else log(`removeBackup: removed ${backup.remotePath}`);
}
async function cleanupAppBackups(site, referencedBackupIds, progressCallback) {
@@ -129,7 +129,7 @@ async function cleanupAppBackups(site, referencedBackupIds, progressCallback) {
let appBackupsToRemove = [];
for (const appId of Object.keys(appBackupsById)) {
const appRetention = Object.assign({ keepLatest: allAppIds.includes(appId) }, site.retention);
debug(`cleanupAppBackups: applying retention for appId ${appId} retention: ${JSON.stringify(appRetention)}`);
log(`cleanupAppBackups: applying retention for appId ${appId} retention: ${JSON.stringify(appRetention)}`);
applyBackupRetention(appBackupsById[appId], appRetention, referencedBackupIds);
appBackupsToRemove = appBackupsToRemove.concat(appBackupsById[appId].filter(b => !b.keepReason));
}
@@ -140,7 +140,7 @@ async function cleanupAppBackups(site, referencedBackupIds, progressCallback) {
await removeBackup(site, appBackup, progressCallback); // never errors
}
debug('cleanupAppBackups: done');
log('cleanupAppBackups: done');
return removedAppBackupPaths;
}
@@ -163,7 +163,7 @@ async function cleanupMailBackups(site, referencedBackupIds, progressCallback) {
await removeBackup(site, mailBackup, progressCallback); // never errors
}
debug('cleanupMailBackups: done');
log('cleanupMailBackups: done');
return removedMailBackupPaths;
}
@@ -193,7 +193,7 @@ async function cleanupBoxBackups(site, progressCallback) {
await removeBackup(site, boxBackup, progressCallback);
}
debug('cleanupBoxBackups: done');
log('cleanupBoxBackups: done');
return { removedBoxBackupPaths, referencedBackupIds };
}
@@ -223,7 +223,7 @@ async function cleanupMissingBackups(site, progressCallback) {
await progressCallback({ message: `Removing missing backup ${backup.remotePath}`});
const [delError] = await safe(backups.del(backup.id));
if (delError) debug(`cleanupMissingBackups: error removing ${backup.id} from database. %o`, delError);
if (delError) log(`cleanupMissingBackups: error removing ${backup.id} from database. %o`, delError);
missingBackupPaths.push(backup.remotePath);
}
@@ -231,7 +231,7 @@ async function cleanupMissingBackups(site, progressCallback) {
++ page;
} while (result.length === perPage);
debug('cleanupMissingBackups: done');
log('cleanupMissingBackups: done');
return missingBackupPaths;
}
@@ -242,7 +242,7 @@ async function removeOldAppSnapshots(site) {
const snapshotInfo = await backupSites.getSnapshotInfo(site);
const progressCallback = (progress) => { debug(`removeOldAppSnapshots: ${progress.message}`); };
const progressCallback = (progress) => { log(`removeOldAppSnapshots: ${progress.message}`); };
for (const appId of Object.keys(snapshotInfo)) {
if (appId === 'box' || appId === 'mail') continue;
@@ -253,16 +253,16 @@ async function removeOldAppSnapshots(site) {
const ext = backupFormats.api(site.format).getFileExtension(!!site.encryption);
const remotePath = `snapshot/app_${appId}${ext}`;
if (ext) {
await safe(backupSites.storageApi(site).remove(site.config, remotePath), { debug });
await safe(backupSites.storageApi(site).remove(site.config, remotePath), { debug: log });
} else {
await safe(backupSites.storageApi(site).removeDir(site.config, site.limits, remotePath, progressCallback), { debug });
await safe(backupSites.storageApi(site).removeDir(site.config, site.limits, remotePath, progressCallback), { debug: log });
}
await backupSites.setSnapshotInfo(site, appId, null /* info */);
debug(`removeOldAppSnapshots: removed snapshot of app ${appId}`);
log(`removeOldAppSnapshots: removed snapshot of app ${appId}`);
}
debug('removeOldAppSnapshots: done');
log('removeOldAppSnapshots: done');
}
async function run(siteId, progressCallback) {
@@ -272,14 +272,14 @@ async function run(siteId, progressCallback) {
const site = await backupSites.get(siteId);
if (!site) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Target not found');
debug(`run: retention is ${JSON.stringify(site.retention)}`);
log(`run: retention is ${JSON.stringify(site.retention)}`);
const status = await backupSites.ensureMounted(site);
debug(`run: mount point status is ${JSON.stringify(status)}`);
log(`run: mount point status is ${JSON.stringify(status)}`);
if (status.state !== 'active') throw new BoxError(BoxError.MOUNT_ERROR, `Backup endpoint is not mounted: ${status.message}`);
if (site.retention.keepWithinSecs < 0) {
debug('run: keeping all backups');
log('run: keeping all backups');
return {};
}