Yurst

Logs and metrics

Log levels, the log file, and stats counters

Logs and metrics

Logger

Without YURST=dev, the internal logger stays silent. Enable dev mode for diagnostics:

YURST=dev
YURST_LOG=debug

Log levels

YURST_LOGWhat is shown
debugEverything, including detailed diagnostics
infoRegular events (default in dev mode)
warnWarnings and errors
errorErrors only

Example logs

[info]  bot start | MODE=ws | node=v22.0.0 | DEV
[info]  bot Ani @ani_ id=2251
[info]  bot commands → 200 /ping[all] /help[all]
[info]  ws connecting… (attempt 1)
[info]  ws socket open — waiting for greeting…
[info]  ws connected — server 1.2.3
[info]  update #12345 via=ws lag=1s | message /ping
[debug] update raw { ID: 12345, Type: 'message', ... }
[info]  api GET /dialogs/d.xxx/messages → 200 (89ms)

Features

  • ANSI colors are enabled only in a TTY
  • The token in output lines is replaced with ***
  • Data truncation: up to 500 characters at the info level, up to 4000 at debug
  • LOG_FILE appends logs to a file in addition to stdout
LOG_FILE=bot.log

Errors when writing to the file are ignored. Make sure the process has write permissions.

Custom logs

The logger is exported as L:

import { L } from 'yurst.js';

L.info('tag', 'повідомлення');
L.warn('tag', 'попередження');
L.error('tag', 'помилка');
L.debug('tag', 'деталі для відлагодження');

Call format

L.level(tag, message, data?);
ParameterDescription
tagShort context identifier
messageMessage text
dataOptional data for logging
L.info('bot', 'Запущено', { version: '1.0.0' });
L.error('db', 'Помилка підключення', err);

stats counters

stats is a shared object that is updated at runtime:

import { stats } from 'yurst.js';

console.log(stats);
// {
//   updates: 1523,
//   errors: 2,
//   apiCalls: 1540,
//   apiErrors: 0,
//   dupes: 3
// }

Counter descriptions

CounterWhen it increases
updatesEvery accepted update (not a duplicate)
errorsAn error in an update handler
apiCallsEvery HTTP request attempt (including retries)
apiErrorsEvery response with ok: false
dupesA skipped duplicate update

Usage in a command

import { bot, stats, fmtUptime } from 'yurst.js';

bot.command('stats', 'Статистика бота', (ctx) => {
  const lines = [
    `⏱ Uptime: ${fmtUptime()}`,
    `📨 Updates: ${stats.updates}`,
    `❌ Errors: ${stats.errors}`,
    `🔌 API calls: ${stats.apiCalls} (${stats.apiErrors} failed)`,
    `🔁 Duplicates: ${stats.dupes}`,
  ];
  return ctx.reply(lines.join('\n'));
});

fmtUptime

Returns the process uptime in the format 1h 23m 4s:

import { fmtUptime } from 'yurst.js';

console.log(fmtUptime());  // "2h 34m 12s"

Features:

  • Days are shown only if present: 1d 2h 3m 4s
  • The starting point is the moment the state.js module is loaded

Example: Health monitoring

import { bot, stats, fmtUptime, L } from 'yurst.js';

// Періодичний звіт про здоров'я
setInterval(() => {
  const errorRate = stats.apiCalls > 0
    ? ((stats.apiErrors / stats.apiCalls) * 100).toFixed(2)
    : 0;

  L.info('health', `Uptime: ${fmtUptime()}, Error rate: ${errorRate}%`);
}, 60000);  // кожну хвилину

// Команда для перевірки стану
bot.command('health', 'Стан бота', (ctx) => {
  const errorRate = stats.apiCalls > 0
    ? ((stats.apiErrors / stats.apiCalls) * 100).toFixed(2)
    : 0;

  const health = errorRate < 5 ? '✅ Healthy' : '⚠️ Issues detected';

  return ctx.reply([
    health,
    `Uptime: ${fmtUptime()}`,
    `Error rate: ${errorRate}%`,
    `Updates: ${stats.updates}`,
  ].join('\n'));
});

Next

API Reference

All exports, methods, and package structure

On this page