Skip to content

Commit

Permalink
feat: add copy/truncate support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbystedt authored Feb 5, 2024
1 parent d3c8866 commit f59ff7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The environment variable `CRON_ROTATE` is used to schedule the rotation of the f

If any files are rotated then, optionally, `LOGROTATE_POSTROTATE_COMMAND` is called. It can be necessary to signal the application that the rotation occurred so it can open a new file.

Files are normally just renamed. You can set `LOGROTATE_COPYTRUNCATE_SUFFIXES` to instead copy and then truncate the file. Some files, like redirected output, can't be renamed and then deleted.

Rotated files are appended with the file's change date and the current UTC timestamp. See: https://nodejs.org/api/fs.html#stat-time-values

### Stage 2 - Compress log file
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const LOGROTATE_AGE_MAX = process.env.LOGROTATE_AGE_MAX
: 0;

export const LOGROTATE_SUFFIX = process.env.LOGROTATE_SUFFIX ?? 'log';
export const LOGROTATE_COPYTRUNCATE_SUFFIXES =
process.env.LOGROTATE_COPYTRUNCATE_SUFFIXES?.split(',') ?? [];
export const LOGROTATE_POSTROTATE_COMMAND =
process.env.LOGROTATE_POSTROTATE_COMMAND ?? '';
export const JANITOR_COPIES = Number.parseInt(
Expand Down
21 changes: 19 additions & 2 deletions src/cron/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LOGROTATE_POSTROTATE_COMMAND,
LOGROTATE_DIRECTORY,
LOGROTATE_SUFFIX,
LOGROTATE_COPYTRUNCATE_SUFFIXES,
LOGROTATE_FILESIZE_MIN,
LOGROTATE_AGE_MAX,
} from '../constants';
Expand Down Expand Up @@ -64,9 +65,16 @@ export async function rotateLogs(db: DatabaseService) {
async function rotateLog(db: DatabaseService, file: string) {
const oldPath = path.join(LOGROTATE_DIRECTORY, file);
const newPath = path.join(LOGROTATE_DIRECTORY, newLogName(file));
const strategy = computeRotateStrategy(file);

console.log(`rotate: ${oldPath} -> ${newPath}`);
fs.renameSync(oldPath, newPath);
console.log(`rotate: [${strategy}] ${oldPath} -> ${newPath}`);

if (strategy === 'rename') {
fs.renameSync(oldPath, newPath);
} else if (strategy === 'copytruncate') {
fs.copyFileSync(oldPath, newPath);
fs.truncateSync(oldPath);
}

// Add log to database
await db.addLog(file, newPath);
Expand All @@ -85,3 +93,12 @@ function changeDate(filePath: string) {
const { ctime } = fs.statSync(filePath);
return ctime;
}

function computeRotateStrategy(file: string) {
for (const suffix of LOGROTATE_COPYTRUNCATE_SUFFIXES) {
if (file.endsWith(suffix)) {
return 'copytruncate';
}
}
return 'rename';
}

0 comments on commit f59ff7b

Please sign in to comment.