UNPKG

core-native

Version:

A lightweight framework based on React Native + Redux + Redux Saga, in strict TypeScript.

78 lines 2.91 kB
import { loggerContext } from "./platform/loggerContext"; import { app } from "./app"; import { APIException, NetworkConnectionException, ReactLifecycleException, RuntimeException } from "./Exception"; export class LoggerImpl { constructor() { this.environmentContext = {}; this.logQueue = []; this.environmentContext = loggerContext; } addContext(context) { this.environmentContext = { ...this.environmentContext, ...context }; } info(action, info) { return this.appendLog("OK", action, undefined, info); } warn(errorCode, action, info) { return this.appendLog("WARN", action, errorCode, info); } error(errorCode, action, info) { return this.appendLog("ERROR", action, errorCode, info); } exception(exception) { if (exception instanceof NetworkConnectionException) { return this.appendLog("WARN", undefined, "NETWORK_FAILURE", { errorMessage: exception.message }); } else { const info = { errorMessage: exception.message }; let errorCode = "ERROR"; if (exception instanceof APIException) { errorCode = `API_ERROR:${exception.statusCode}`; info.requestURL = exception.requestURL; if (exception.errorCode) { info.errorCode = exception.errorCode; } if (exception.errorId) { info.errorId = exception.errorId; } } else if (exception instanceof ReactLifecycleException) { errorCode = "LIFECYCLE_ERROR"; info.stackTrace = exception.componentStack; info.appState = JSON.stringify(app.store.getState().app); } else if (exception instanceof RuntimeException) { errorCode = "JS_ERROR"; info.stackTrace = JSON.stringify(exception.errorObject); info.appState = JSON.stringify(app.store.getState().app); } return this.appendLog("ERROR", undefined, errorCode, info); } } collect() { return this.logQueue; } empty() { this.logQueue = []; } appendLog(result, action, errorCode, info) { const completeContext = {}; Object.entries(this.environmentContext).map(([key, value]) => { completeContext[key] = typeof value === "string" ? value : value(); }); const event = { date: new Date(), result, action, errorCode, info: info || {}, context: completeContext, elapsedTime: 0, }; this.logQueue.push(event); return () => { event.elapsedTime = new Date().getTime() - event.date.getTime(); }; } } //# sourceMappingURL=Logger.js.map