Add log rotating to flipper-server

Summary:
CHANGELOG: Rotate flipper-server logs

Otherwise, they re going to eat up all space eventually in a one monstrous file

Reviewed By: passy

Differential Revision: D37516868

fbshipit-source-id: 478a61c56ec007e4a3d695f7e6df2a61cb33c33a
This commit is contained in:
Andrey Goncharov
2022-06-30 07:01:40 -07:00
committed by Facebook GitHub Bot
parent 646b9d5a5d
commit 1052384154
5 changed files with 47 additions and 11 deletions

View File

@@ -78,7 +78,7 @@ const rootDir = argv.bundler
const staticDir = path.join(rootDir, 'static');
async function start() {
const enhanceLogger = initializeLogger(staticDir);
const enhanceLogger = await initializeLogger(staticDir);
let keytar: any = undefined;
try {

View File

@@ -7,7 +7,6 @@
* @format
*/
import * as fs from 'fs-extra';
import path from 'path';
import {
LoggerExtractError,
@@ -17,6 +16,11 @@ import {
Logger,
setLoggerInstance,
} from 'flipper-common';
// @ts-expect-error
import fsRotator from 'file-stream-rotator';
import {ensureFile} from 'fs-extra';
import {access} from 'fs/promises';
import {constants} from 'fs';
export const loggerOutputFile = 'flipper-server-log.out';
@@ -81,8 +85,8 @@ const consoleProxy = (proxy: FlipperLogProxy) => {
}
};
export function initializeLogger(staticDir: string) {
// Supress debug messages by default.
export async function initializeLogger(staticDir: string) {
// Suppress stdout debug messages, but keep writing them to the file.
console.debug = function () {};
const logger = createLogger();
@@ -90,9 +94,26 @@ export function initializeLogger(staticDir: string) {
let onConsoleEntry: ((entry: LoggerInfo) => void) | undefined;
const file = fs.createWriteStream(path.join(staticDir, loggerOutputFile));
const logFilename = path.join(staticDir, loggerOutputFile);
let logStream: NodeJS.WriteStream | undefined = undefined;
try {
await ensureFile(logFilename);
await access(logFilename, constants.W_OK);
logStream = fsRotator.getStream({
// Rotation number is going to be added after the file name
filename: logFilename,
// Rotate every 1MB
size: '1m',
// Keep last 5 rotations
max_logs: 20,
});
} catch (e) {
console.warn('initializeLogger -> cannot write logs to FS', e);
}
consoleProxy((entry: LoggerInfo) => {
file.write(`${JSON.stringify(entry)}\n`);
logStream?.write(`${JSON.stringify(entry)}\n`);
onConsoleEntry?.(entry);
});