Cleanup logger
Summary: Some cleanup around Logger, like renaming `getInstance` -> `getLogger` Reviewed By: aigoncharov Differential Revision: D31480828 fbshipit-source-id: d2fa9dc2b45bc34d846d45e396a74b80906d3ad5
This commit is contained in:
committed by
Facebook GitHub Bot
parent
91d96774f6
commit
51bfc8f05d
@@ -9,7 +9,9 @@
|
||||
"license": "MIT",
|
||||
"bugs": "https://github.com/facebook/flipper/issues",
|
||||
"dependencies": {},
|
||||
"devDependencies": {},
|
||||
"devDependencies": {
|
||||
"flipper-plugin-lib": "0.0.0"
|
||||
},
|
||||
"peerDependencies": {},
|
||||
"scripts": {
|
||||
"reset": "rimraf lib *.tsbuildinfo",
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
export type LoggerTypes = 'error' | 'warn' | 'info' | 'debug';
|
||||
|
||||
export type LoggerTrackType =
|
||||
| 'duration'
|
||||
| 'usage'
|
||||
| 'performance'
|
||||
| 'success-rate'
|
||||
| 'operation-cancelled';
|
||||
|
||||
export type LoggerArgs = {
|
||||
isTest?: boolean;
|
||||
};
|
||||
|
||||
export interface Logger {
|
||||
track(
|
||||
type: LoggerTrackType,
|
||||
event: string,
|
||||
data?: any,
|
||||
plugin?: string,
|
||||
): void;
|
||||
|
||||
trackTimeSince(
|
||||
mark: string,
|
||||
eventName?: string | null | undefined,
|
||||
data?: any,
|
||||
): void;
|
||||
|
||||
info(data: any, category: string): void;
|
||||
|
||||
warn(data: any, category: string): void;
|
||||
|
||||
error(data: any, category: string): void;
|
||||
|
||||
debug(data: any, category: string): void;
|
||||
}
|
||||
@@ -12,7 +12,11 @@ export {
|
||||
LoggerTrackType,
|
||||
LoggerTypes,
|
||||
LoggerArgs,
|
||||
} from './fb-interfaces/Logger';
|
||||
getLogger,
|
||||
setLoggerInstance,
|
||||
NoopLogger,
|
||||
} from './utils/Logger';
|
||||
export * from './server-types';
|
||||
export {sleep} from './utils/sleep';
|
||||
export {timeout} from './utils/timeout';
|
||||
export {isTest} from './utils/isTest';
|
||||
|
||||
95
desktop/flipper-common/src/utils/Logger.tsx
Normal file
95
desktop/flipper-common/src/utils/Logger.tsx
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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 {isTest} from '../utils/isTest';
|
||||
|
||||
export type LoggerTypes = 'error' | 'warn' | 'info' | 'debug';
|
||||
|
||||
export type LoggerTrackType =
|
||||
| 'duration'
|
||||
| 'usage'
|
||||
| 'performance'
|
||||
| 'success-rate'
|
||||
| 'operation-cancelled';
|
||||
|
||||
export type LoggerArgs = {
|
||||
isTest?: boolean;
|
||||
};
|
||||
|
||||
export interface Logger {
|
||||
track(
|
||||
type: LoggerTrackType,
|
||||
event: string,
|
||||
data?: any,
|
||||
plugin?: string,
|
||||
): void;
|
||||
|
||||
trackTimeSince(
|
||||
mark: string,
|
||||
eventName?: string | null | undefined,
|
||||
data?: any,
|
||||
): void;
|
||||
|
||||
info(data: any, category: string): void;
|
||||
|
||||
warn(data: any, category: string): void;
|
||||
|
||||
error(data: any, category: string): void;
|
||||
|
||||
debug(data: any, category: string): void;
|
||||
}
|
||||
|
||||
let instance: Logger | null = null;
|
||||
|
||||
export function getLogger(): Logger {
|
||||
if (instance) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
if (isTest()) {
|
||||
instance = new NoopLogger();
|
||||
return instance;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Requested Logger instance without initializing it. Make sure init() is called at app start',
|
||||
);
|
||||
}
|
||||
|
||||
// only for testing
|
||||
export function setLoggerInstance(logger: Logger) {
|
||||
if (!isTest() && instance) {
|
||||
console.warn('Logger was already initialised');
|
||||
}
|
||||
instance = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* A logger that doesn't log anything, used in tests and public Flipper builds
|
||||
*/
|
||||
export class NoopLogger implements Logger {
|
||||
constructor() {}
|
||||
|
||||
track(
|
||||
_type: LoggerTrackType,
|
||||
_event: string,
|
||||
_data?: any,
|
||||
_plugin?: string,
|
||||
) {}
|
||||
|
||||
trackTimeSince(_mark: string, _eventName?: string) {}
|
||||
|
||||
info(_data: any, _category: string) {}
|
||||
|
||||
warn(_data: any, _category: string) {}
|
||||
|
||||
error(_data: any, _category: string) {}
|
||||
|
||||
debug(_data: any, _category: string) {}
|
||||
}
|
||||
14
desktop/flipper-common/src/utils/isTest.tsx
Normal file
14
desktop/flipper-common/src/utils/isTest.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
const _isTest = !!process.env.JEST_WORKER_ID;
|
||||
|
||||
export function isTest(): boolean {
|
||||
return _isTest;
|
||||
}
|
||||
Reference in New Issue
Block a user