Extract logger and write to file

Summary:
This change extracts logging logic out from startFlipperServer.

Logs will also be written to disk.

Reviewed By: passy

Differential Revision: D36473768

fbshipit-source-id: b1df9df79b4aced0d3ba2e8c243aa8d44cf83703
This commit is contained in:
Lorenzo Blasa
2022-06-01 02:37:03 -07:00
committed by Facebook GitHub Bot
parent 92cdb81096
commit ee64216725
3 changed files with 99 additions and 42 deletions

View File

@@ -14,6 +14,7 @@ import {startFlipperServer} from './startFlipperServer';
import {startServer} from './startServer';
import {attachSocketServer} from './attachSocketServer';
import {attachDevServer} from './attachDevServer';
import {initializeLogger} from './logger';
import yargs from 'yargs';
import open from 'open';
@@ -72,10 +73,7 @@ const rootDir = argv.bundler
const staticDir = path.join(rootDir, 'static');
async function start() {
// supress debug messages by default. TODO: make CLI flag
console.debug = function () {
// Noop
};
initializeLogger(staticDir);
const {app, server, socket} = await startServer({
port: argv.port,

View File

@@ -0,0 +1,95 @@
/**
* 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 * as fs from 'fs-extra';
import path from 'path';
import {
LoggerExtractError,
LoggerFormat,
LoggerInfo,
LoggerTypes,
Logger,
setLoggerInstance,
} from 'flipper-common';
export const loggerOutputFile = 'flipper-server-log.out';
const logTypes: LoggerTypes[] = ['debug', 'info', 'warn', 'error'];
function createLogger(): Logger {
return {
track(..._args: [any, any, any?, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipper track)', args);
},
trackTimeSince(..._args: [any, any, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipped trackTimeSince)', args);
},
debug(...args: any[]) {
console.debug(...args);
},
error(...args: any[]) {
console.error(...args);
},
warn(...args: any[]) {
console.warn(...args);
},
info(...args: any[]) {
console.info(...args);
},
};
}
type FlipperLogProxy = (entry: LoggerInfo) => void;
const consoleProxy = (proxy: FlipperLogProxy) => {
function log(level: LoggerTypes, ...data: Array<any>): void {
const logInfo = LoggerFormat(level, ...data);
proxy(logInfo);
if (level === 'error') {
const {
message,
error: {stack, interaction, name},
} = LoggerExtractError(data);
const logInfo = LoggerFormat(level, {
name,
stack,
interaction,
message,
});
proxy(logInfo);
}
}
for (const method of logTypes) {
const originalConsole: {[key: string]: any} = console;
const originalMethod = originalConsole[method];
const overrideMethod = (...args: Array<unknown>) => {
const result = originalMethod(...args);
log(method, ...args);
return result;
};
originalConsole[method] = overrideMethod;
}
};
export function initializeLogger(staticDir: string) {
// Supress debug messages by default.
console.debug = function () {};
const logger = createLogger();
setLoggerInstance(logger);
const file = fs.createWriteStream(path.join(staticDir, loggerOutputFile));
consoleProxy((entry: LoggerInfo) => {
file.write(`${JSON.stringify(entry)}\n`);
});
}

View File

@@ -16,12 +16,7 @@ import {
loadSettings,
getEnvironmentInfo,
} from 'flipper-server-core';
import {
parseEnvironmentVariables,
isTest,
Logger,
setLoggerInstance,
} from 'flipper-common';
import {parseEnvironmentVariables, isTest, getLogger} from 'flipper-common';
import path from 'path';
import fs from 'fs-extra';
@@ -53,9 +48,6 @@ export async function startFlipperServer(
desktopPath = os.homedir();
}
const logger = createLogger();
setLoggerInstance(logger);
let keytar: any = undefined;
try {
if (!isTest()) {
@@ -96,35 +88,7 @@ export async function startFlipperServer(
settings: await loadSettings(settingsString),
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
},
logger,
getLogger(),
keytar,
);
}
function createLogger(): Logger {
return {
track(..._args: [any, any, any?, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipper track)', args);
},
trackTimeSince(..._args: [any, any, any?]) {
// TODO: only if verbose console.debug(...args);
// console.warn('(skipped trackTimeSince)', args);
},
debug(..._args: any[]) {
// TODO: only if double verbose console.debug(...args);
},
error(...args: any[]) {
console.error(...args);
console.warn('(skipped error reporting)');
},
warn(...args: any[]) {
console.warn(...args);
console.warn('(skipped error reporting)');
},
info(..._args: any[]) {
// TODO: only if verbose console.debug(...args);
// console.info(...args);
},
};
}