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:
committed by
Facebook GitHub Bot
parent
92cdb81096
commit
ee64216725
@@ -14,6 +14,7 @@ import {startFlipperServer} from './startFlipperServer';
|
|||||||
import {startServer} from './startServer';
|
import {startServer} from './startServer';
|
||||||
import {attachSocketServer} from './attachSocketServer';
|
import {attachSocketServer} from './attachSocketServer';
|
||||||
import {attachDevServer} from './attachDevServer';
|
import {attachDevServer} from './attachDevServer';
|
||||||
|
import {initializeLogger} from './logger';
|
||||||
|
|
||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
import open from 'open';
|
import open from 'open';
|
||||||
@@ -72,10 +73,7 @@ const rootDir = argv.bundler
|
|||||||
const staticDir = path.join(rootDir, 'static');
|
const staticDir = path.join(rootDir, 'static');
|
||||||
|
|
||||||
async function start() {
|
async function start() {
|
||||||
// supress debug messages by default. TODO: make CLI flag
|
initializeLogger(staticDir);
|
||||||
console.debug = function () {
|
|
||||||
// Noop
|
|
||||||
};
|
|
||||||
|
|
||||||
const {app, server, socket} = await startServer({
|
const {app, server, socket} = await startServer({
|
||||||
port: argv.port,
|
port: argv.port,
|
||||||
|
|||||||
95
desktop/flipper-server/src/logger.tsx
Normal file
95
desktop/flipper-server/src/logger.tsx
Normal 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`);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -16,12 +16,7 @@ import {
|
|||||||
loadSettings,
|
loadSettings,
|
||||||
getEnvironmentInfo,
|
getEnvironmentInfo,
|
||||||
} from 'flipper-server-core';
|
} from 'flipper-server-core';
|
||||||
import {
|
import {parseEnvironmentVariables, isTest, getLogger} from 'flipper-common';
|
||||||
parseEnvironmentVariables,
|
|
||||||
isTest,
|
|
||||||
Logger,
|
|
||||||
setLoggerInstance,
|
|
||||||
} from 'flipper-common';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
|
|
||||||
@@ -53,9 +48,6 @@ export async function startFlipperServer(
|
|||||||
desktopPath = os.homedir();
|
desktopPath = os.homedir();
|
||||||
}
|
}
|
||||||
|
|
||||||
const logger = createLogger();
|
|
||||||
setLoggerInstance(logger);
|
|
||||||
|
|
||||||
let keytar: any = undefined;
|
let keytar: any = undefined;
|
||||||
try {
|
try {
|
||||||
if (!isTest()) {
|
if (!isTest()) {
|
||||||
@@ -96,35 +88,7 @@ export async function startFlipperServer(
|
|||||||
settings: await loadSettings(settingsString),
|
settings: await loadSettings(settingsString),
|
||||||
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
|
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
|
||||||
},
|
},
|
||||||
logger,
|
getLogger(),
|
||||||
keytar,
|
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);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user