Files
flipper/desktop/flipper-server/src/logger.tsx
Lorenzo Blasa 6405a7bfdd Hook with FB logger
Summary:
This change replaces the existing logging infra with the one defined and exposed in flipper-server-core.

Functionality remains the same with the addition of having support for Scribe.

Reviewed By: aigoncharov

Differential Revision: D48515246

fbshipit-source-id: 7970f6ad069821ee4f15136adc8da40d0b1fb0c7
2023-08-24 06:18:39 -07:00

74 lines
1.9 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import path from 'path';
import {
addLogTailer,
EnvironmentInfo,
LoggerExtractError,
LoggerFormat,
LoggerTypes,
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';
import {initializeLogger as initLogger} from 'flipper-server-core';
export const loggerOutputFile = 'flipper-server-log.out';
export async function initializeLogger(
environmentInfo: EnvironmentInfo,
staticDir: string,
) {
// Suppress stdout debug messages, but keep writing them to the file.
console.debug = function () {};
const logger = initLogger(environmentInfo);
setLoggerInstance(logger);
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);
}
addLogTailer((level: LoggerTypes, ...data: Array<any>) => {
const logInfo = LoggerFormat(level, ...data);
logStream?.write(`${JSON.stringify(logInfo)}\n`);
if (level === 'error') {
const {
message,
error: {stack, interaction, name},
} = LoggerExtractError(data);
const logInfo = LoggerFormat(level, {
name,
stack,
interaction,
message,
});
logStream?.write(`${JSON.stringify(logInfo)}\n`);
}
});
}