@evo/gotcha-log

中文文档
1.6.0发布于 2026/05/28ISCESM + CJS零依赖近 30 天下载 4822

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

原始使用文档
文档目录9 节
  1. gotcha-log
  2. Installation
  3. API
  4. Usage with global gotcha client
  5. Usage as a standalone logger
  6. logging extra
  7. txid
  8. @evo/gotcha-log/dist/metrics
  9. Usage:

gotcha-log

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.

Installation

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

API

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>)

gotcha.log('test');
gotcha.error('test');

See details about GOTCHA_SITE, GOTCHA_HOST here

Example of the resulting message:

{
    "message":"test msg",
    "filename":"default",
    "screenWidth":1855,
    "screenHeight":1056,
    "userLanguage":"US",
    "currentUrl":"http://localhost:8000/",
    "site":"evodoc-dev",
    "type":"log"
}

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

txid

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>' });

@evo/gotcha-log/dist/metrics

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()

NOTE:

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

Usage:

Common usage:

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

initGotchaMetrics({
    project: <project-id>, // do not forget, to add your project to gotcha-server config
    host: <gotcha-backend-host>
});

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

Also you can create metric instance manually:

import { Counter } from '@evo/gotcha-log/dist/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/dist/metrics';
stopUploadByInterval()

And resume it by calling:

import { startUploadByInterval } from '@evo/gotcha-log/dist/metrics';
startUploadByInterval()

NOTE:

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/dist/metrics';
immediatelyUploadMetrics()

版本与发布记录

@evo/gotcha-log 共发布 20 个版本,首次发布于 2017/01/25;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.6.0latest2026/05/285 个月前
1.5.12021/04/285 年前
1.5.02021/04/285 年前
1.4.02019/04/257 年前
1.3.12018/01/228 年前
1.3.02017/11/089 年前

关于 @evo/gotcha-log 的常见问题

@evo/gotcha-log 是什么?

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

如何安装 @evo/gotcha-log?

在项目目录运行 npm install @evo/gotcha-log,或使用 pnpm add / yarn add / bun add。

@evo/gotcha-log 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/evo__gotcha-log 是否存在。

@evo/gotcha-log 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@evo/gotcha-log 可以商用吗?

@evo/gotcha-log 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@evo/gotcha-log 的下载量有多少?

根据 npm 官方下载统计,@evo/gotcha-log 在 2026/10/14 – 2026/11/12 这 30 天内被下载 4,822 次,日均约 161 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@evo/gotcha-log 还在维护吗?

最新版本 1.6.0 发布于 2026/05/28(5 个月前);至今共发布 20 个版本,其中过去 12 个月发布了 1 个;首次发布于 2017/01/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。