Skip to content

Instantly share code, notes, and snippets.

@ericpatrick
Created December 3, 2018 12:38
Show Gist options
  • Save ericpatrick/c76c5196dbb02d20cef7272a15d75e9f to your computer and use it in GitHub Desktop.
Save ericpatrick/c76c5196dbb02d20cef7272a15d75e9f to your computer and use it in GitHub Desktop.

Revisions

  1. ericpatrick created this gist Dec 3, 2018.
    139 changes: 139 additions & 0 deletions logger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@
    // @flow
    /* eslint-disable no-console, no-plusplus, no-param-reassign */
    import { Sentry, SentrySeverity } from 'react-native-sentry';

    type Level = 'debug' | 'warning' | 'error' | 'info';

    function log(message: string, ...args: Array<any>): void {
    if (__DEV__) {
    console.log(message, ...args);
    } else {
    let _message = message;
    if (typeof _message !== 'string') {
    try {
    _message = JSON.stringify(message);
    } catch (err) {
    _message = String(message);
    }
    }
    breadcrumb(_message, 'debug');
    }
    }

    /**
    * Manda um breadcrumb para o sentry
    */
    function breadcrumb(message: string, category: string, level: Level = 'debug'): void {
    if (!__DEV__) {
    if (typeof message !== 'string') return;
    Sentry.captureBreadcrumb({
    message,
    category,
    level,
    });
    }
    }

    /**
    * Exibe um warn em desenvolvimento e manda para o sentry em produção
    */
    function warn(title: string, ...args: Array<any>): void {
    if (__DEV__) {
    console.warn(...args);
    } else if (typeof title === 'string') {
    Sentry.captureMessage(title, {
    level: SentrySeverity.Warning,
    extra: {
    args: JSON.stringify(args, null, 2),
    },
    });
    }
    }

    function warnLocal(...args: Array<any>): void {
    if (__DEV__) {
    console.warn(...args);
    }
    }

    function error(err: Error): void {
    if (__DEV__) {
    if (err instanceof Error) {
    // $FlowFixMe
    err.framesToPop = 1;
    }
    console.error(err);
    } else if (err instanceof Error) {
    // $FlowFixMe
    err.framesToPop = 1;
    Sentry.captureException(err);
    } else if (typeof err === 'string') {
    Sentry.captureMessage(err);
    }
    }


    function requestError(err: any, message?: ?string): void {
    if (err.config) {
    if (__DEV__) {
    if (message && typeof message === 'string') {
    const newError = new Error(message);
    error(newError);
    } else {
    error(err);
    }
    } else {
    breadcrumb(`[HTTP_ERROR] URL:${err.config.url}`, 'http', 'error');
    breadcrumb(`[HTTP_ERROR] METHOD:${err.config.method}`, 'http', 'error');
    breadcrumb(`[HTTP_ERROR] DATA:${JSON.stringify(err.config.data)}`, 'http', 'error');
    if (err.response && err.response.data) {
    breadcrumb(
    `[HTTP_ERROR] RESPONSE:${JSON.stringify(err.response.data)}`,
    'http',
    'error',
    );
    }
    if (message && typeof message === 'string') {
    const newError = new Error(message);
    error(newError);
    } else {
    error(err);
    }
    }
    } else {
    error(err);
    }
    }

    /**
    * Verifica se uma condição é verdadeira, caso contrario lança uma exceção
    * @example
    * ```javascript
    * logger.assert(false, 'Sempre vai lançar uma exceção: %s', 'Nome da Exceção')
    * ```
    */
    function assert(condition: boolean, format: string, ...args: Array<mixed>) {
    if (__DEV__) {
    if (condition) return;

    if (typeof format !== 'string') {
    throw new Error('"logger.assert" needs a format string');
    }
    let argIndex = 0;
    const newError = new Error(format.replace(/%s/g, () => String(args[argIndex++])));
    newError.name = 'AssertError';
    // $FlowFixMe
    newError.framesToPop = 1;
    throw newError;
    }
    }

    export default {
    log,
    error,
    warn,
    assert,
    warnLocal,
    breadcrumb,
    requestError,
    };