diff --git a/src/capture.ts b/src/capture.ts index d052589..a322cd6 100644 --- a/src/capture.ts +++ b/src/capture.ts @@ -14,27 +14,13 @@ const captureMessage = ({ level = 'log', message, lineContext = {} }: LogMessage return; } - // run the beforeSend hooks - const data: LogMessage = (getOptions().hooks || { beforeSend: [] }).beforeSend.reduce((acc: LogMessage, fn: Function) => (acc == null ? null : fn(acc)), { - level, - message, - lineContext, - }); - - // beforeSend stopped the log - if (data == null) { - return; - } - - const logLine: LogDNALogLine = generateLogLine(data); - - process(logLine); + generateLogLine({ level, message, lineContext }); }; const captureError = (error: any) => { if (isSendingDisabled()) return; - const logLine: LogDNALogLine = generateLogLine({ + generateLogLine({ level: 'error', message: error.message, errorContext: { @@ -45,27 +31,38 @@ const captureError = (error: any) => { }, disableStacktrace: !!(error.stack || error.stacktrace), // Dont generate a second stacktrace for errors since they already have it }); - - process(logLine); }; -const generateLogLine = ({ level = 'log', message, lineContext = {}, errorContext = {}, disableStacktrace = false }: LogMessage): LogDNALogLine => { +const generateLogLine = ({ level = 'log', message, lineContext = {}, errorContext = null, disableStacktrace = false }: LogMessage) => { const opts = getOptions(); - return { + + // run the beforeSend hooks + const data: LogMessage = (getOptions().hooks || { beforeSend: [] }).beforeSend.reduce((acc: LogMessage, fn: Function) => (acc == null ? null : fn(acc)), { + level, + message, + lineContext, + }); + + // beforeSend stopped the log + if (data == null) { + return; + } + + process({ timestamp: Math.floor(Date.now() / 1000), app: opts.app || window.location.host, - line: typeof message === 'string' ? message : utils.stringify(message), - level, + line: typeof data.message === 'string' ? data.message : utils.stringify(data.message), + level: data.level, meta: { sessionId: getSessionId(), ...getStaticContext(), ...getDynamicContext(), stacktrace: disableStacktrace || !opts.enableStacktrace ? undefined : utils.getStackTrace(), context: { ...getContext() }, - lineContext, + lineContext: data.lineContext, errorContext, }, - }; + }); }; const internalErrorLogger = (...args: any[]) => { diff --git a/src/logdna.d.ts b/src/logdna.d.ts index d909e16..e8a72ad 100644 --- a/src/logdna.d.ts +++ b/src/logdna.d.ts @@ -1,7 +1,7 @@ import { GlobalErrorHandlerPlugin } from './plugins/global-handler'; -declare module 'logdna-browser-2' {} +declare module 'logdna-browser-2' { } -interface LogDNAMethods {} +interface LogDNAMethods { } // This is fallback to 3rd party plugin methods // Until TS has better Module Augmentation without @@ -69,7 +69,7 @@ export type LogMessage = { level: LogLevel; message: any; lineContext?: LineContext; - errorContext?: ErrorContext; + errorContext?: ErrorContext | null | undefined; disableStacktrace?: boolean; };