Refactor some of the types used by FBLogger into Logger

Summary:
^

This change aims to extract some bits and pieces that are not specific to FBLogger and could be used by other Logger implementations.

Reviewed By: passy

Differential Revision: D36473286

fbshipit-source-id: 57f02d132673dbac97384da4dca51bf3e6fb8738
This commit is contained in:
Lorenzo Blasa
2022-05-24 10:23:42 -07:00
committed by Facebook GitHub Bot
parent a73f736d92
commit 6d2bc5cc9a
4 changed files with 77 additions and 6 deletions

View File

@@ -9,6 +9,10 @@
export {
Logger,
LoggerExtractError,
LoggerFormat,
LoggerInfo,
LoggerPerformanceEntry,
LoggerTrackType,
LoggerTypes,
LoggerArgs,

View File

@@ -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<any>;
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<any>): {
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<any>
): 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,
};
}

View File

@@ -20,7 +20,7 @@ const instance = {
debug: jest.fn(),
};
export function extractError(...data: Array<any>): {
export function LoggerExtractError(...data: Array<any>): {
message: string;
error: Error;
} {

View File

@@ -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"