diff --git a/desktop/flipper-common/src/index.tsx b/desktop/flipper-common/src/index.tsx index 0adc56355..c99b69625 100644 --- a/desktop/flipper-common/src/index.tsx +++ b/desktop/flipper-common/src/index.tsx @@ -9,6 +9,10 @@ export { Logger, + LoggerExtractError, + LoggerFormat, + LoggerInfo, + LoggerPerformanceEntry, LoggerTrackType, LoggerTypes, LoggerArgs, diff --git a/desktop/flipper-common/src/utils/Logger.tsx b/desktop/flipper-common/src/utils/Logger.tsx index 68bfecee8..0488b6e30 100644 --- a/desktop/flipper-common/src/utils/Logger.tsx +++ b/desktop/flipper-common/src/utils/Logger.tsx @@ -8,6 +8,7 @@ */ import {isTest} from '../utils/isTest'; +import {getErrorFromErrorLike, getStringFromErrorLike} from '../utils/errors'; export type LoggerTypes = 'error' | 'warn' | 'info' | 'debug'; @@ -22,6 +23,21 @@ export type LoggerArgs = { isTest?: boolean; }; +export type LoggerInfo = { + formatted: Array; + namespace: string; + type: LoggerTypes; + msg: any; + time: number; +}; + +export type LoggerPerformanceEntry = { + startTime: number; + name: string; + entryType: string; + duration: number; +}; + export interface Logger { track( type: LoggerTrackType, @@ -90,3 +106,59 @@ export class NoopLogger implements Logger { debug(_data: any, _category: string) {} } + +export function LoggerExtractError(...data: Array): { + message: string; + error: Error; +} { + const message = getStringFromErrorLike(data); + const error = getErrorFromErrorLike(data) ?? new Error(message); + return { + message, + error, + }; +} + +export function LoggerFormat( + type: LoggerTypes, + ...data: Array +): LoggerInfo { + function stringify(value: any) { + if ( + value === undefined || + value === null || + typeof value == 'object' || + typeof value == 'function' || + typeof value == 'symbol' + ) { + try { + return JSON.stringify(value, null, 2); + } catch (e) { + return JSON.stringify( + { + error: `Failed to serialise: ${e}`, + }, + null, + 2, + ); + } + } + return value; + } + + const date = new Date().toISOString(); + const human = 'flipper'; + const args = [`[${date}] ${human}`]; + if (data) { + args[0] += ':'; + data.forEach((e) => args.push(e)); + } + const msg = data.map((e) => stringify(e)).join(' '); + return { + msg, + namespace: 'flipper', + time: Date.now(), + type, + formatted: args, + }; +} diff --git a/desktop/flipper-ui-core/src/fb-stubs/__mocks__/Logger.tsx b/desktop/flipper-ui-core/src/fb-stubs/__mocks__/Logger.tsx index a72663b96..1ca32e51f 100644 --- a/desktop/flipper-ui-core/src/fb-stubs/__mocks__/Logger.tsx +++ b/desktop/flipper-ui-core/src/fb-stubs/__mocks__/Logger.tsx @@ -20,7 +20,7 @@ const instance = { debug: jest.fn(), }; -export function extractError(...data: Array): { +export function LoggerExtractError(...data: Array): { message: string; error: Error; } { diff --git a/desktop/yarn.lock b/desktop/yarn.lock index 4e26a25d0..863ca1685 100644 --- a/desktop/yarn.lock +++ b/desktop/yarn.lock @@ -13028,11 +13028,6 @@ ws@^8.5.0, ws@^8.6.0: resolved "https://registry.yarnpkg.com/ws/-/ws-8.6.0.tgz#e5e9f1d9e7ff88083d0c0dd8281ea662a42c9c23" integrity sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw== -ws@^8.6.0: - version "8.6.0" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.6.0.tgz#e5e9f1d9e7ff88083d0c0dd8281ea662a42c9c23" - integrity sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw== - ws@~8.2.3: version "8.2.3" resolved "https://registry.yarnpkg.com/ws/-/ws-8.2.3.tgz#63a56456db1b04367d0b721a0b80cae6d8becbba"