Logger for [gotcha](https://gitlab.uaprom/gotcha/gotcha). Will use global gotcha config (if gotcha client is already present in page than *window._GOTCHA_IS_HERE* will be *true* ). Or if there is no gotcha on page, it will not work until you will set the


1.5.16 months ago5 years agoMinified + gzip package size for @evo/gotcha-log in KB



Logger for gotcha. Will use global gotcha config (if gotcha client is already present in page than window._GOTCHA_IS_HERE will be true ). Or if there is no gotcha on page, it will not work until you will set the config with initGotchaLogger function.


npm install --save -E @evo/gotcha-log


gotcha.log(msg, extra);

gotcha.error(msg, extra);

Usage with global gotcha client

import * as gotcha from '@evo/gotcha-log';
// or import { log } from '@evo/gotcha-log';

gotcha.log('test msg')
// or log('test msg')
gotcha.error('error msg')

Usage as a standalone logger

import * as gotcha from '@evo/gotcha-log';

gotcha.initGotchaLogger(<GOTCHA_SITE>, <GOTCHA_HOST>, <TXID>)


See details about GOTCHA_SITE, GOTCHA_HOST here

Example of the resulting message:

    "message":"test msg",

logging extra

const extra = { user: { name: 'test', age: 1 } };
gotcha.log('test msg', extra);

Will log:

    message: "test msg",
    ctx: { user_name_str: "test", user_age_int: 1 },

extra will be flattened as described here


By default gotcha log will apply txid from global gotcha configuration. But you can also specify txid per log in the extra argument:

gotcha.log('test msg', { txid: '<some id>' });


There was a need to transfer the numerical data of event metrics to the backend from the frontend to save them in the Prometheus. For this purpose a sub-module of metrics was created.

The design of the module has been inspired by the package prom-client

All metrics are uploaded to backend using window.sendBeacon(). If browser does not support this API, it will fallbacks to XMLHttpRequest()


Before add metrics to your project, you must add your project config to gotcha-server


Common usage:

// at start of your application, initialize metrics module
import {initGotchaMetrics, metric} from '@evo/gotcha-log/src/metrics';

    project: <project-id>, // do not forget, to add your project to gotcha-server config
    host: <gotcha-backend-host>

// create metric: metric(<name>, [<value>], [<labels>])
    'pageview', // metric name
     1, // value
    {page:'koshyk'} // labels

Also you can create metric instance manually:

import { Counter } from '@evo/gotcha-log/src/metrics';

const reduxActionsErrorsCounter=new Counter({
    name: 'redux-errors'
reduxActionsErrorsCounter.inc(123, {action: 'createOrder', version: '1.2.3'})

// or

const incCounter=reduxActionsErrorsCounter.getStandaloneInc( {action: 'createOrder', version: '1.2.3'})
incCounter(3) // which is equivalent to reduxActionsErrorsCounter.inc(3, {action: 'createOrder', version: '1.2.3'})

All metrics will upload to backend automatically each 5s. You can stop uploading by calling:

import { stopUploadByInterval } from '@evo/gotcha-log/src/metrics';

And resume it by calling:

import { startUploadByInterval } from '@evo/gotcha-log/src/metrics';


startUploadByInterval and stopUploadByInterval also add beforeonload event listener, which will upload the last metrics to backend, before user leaves current page

Send metrics to backend immediately:

import { immediatelyUploadMetrics } from '@evo/gotcha-log/src/metrics';

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.