0.1.6 • Published 9 months ago

@e22m4u/js-debug v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@e22m4u/js-debug

Утилита вывода сообщений отладки для JavaScript.

Содержание

Установка

npm install @e22m4u/js-debug

Поддержка ESM и CommonJS стандартов.

ESM

import {createDebugger} from '@e22m4u/js-debug';

CommonJS

const {createDebugger} = require('@e22m4u/js-debug');

Использование

Интерполяция строк (см. спецификаторы @e22m4u/js-format).

import {createDebugger} from '@e22m4u/js-debug';

const debug = createDebugger();
debug('Got value %v.', 100);
debug('Got values %l.', ['foo', 10, true]);
// Got value 100.
// Got values "foo", 10, true.

Дамп значений.

import {createDebugger} from '@e22m4u/js-debug';

const debug = createDebugger();
debug({
  email: 'john.doe@example.com',
  phone: {
    mobile: '+1-555-123-4567',
    home: '+1-555-987-6543'
  },
});
// {
//   email: 'john.doe@example.com',
//   phone: {
//     mobile: '+1-555-123-4567',
//     home: '+1-555-987-6543'
//   }
// }

Дополнительное сообщение над дампом.

import {createDebugger} from '@e22m4u/js-debug';

const debug = createDebugger();
debug({
  orderId: 988,
  date: '2023-10-27',
  totalAmount: 120.50,
}, 'Order details:');

// Order details:
// {
//   orderId: 988,
//   date: '2023-10-27',
//   totalAmount: 120.50,
// }

Определение пространства имен.

import {createDebugger} from '@e22m4u/js-debug';

const debug1 = createDebugger('myApp');
const debug2 = createDebugger('myApp', 'myService');
const debug3 = createDebugger('myApp:myService');
debug1('Hello world');
debug2('Hello world');
debug3('Hello world');
// myApp Hello world
// myApp:myService Hello world
// myApp:myService Hello world

Использование пространства имен из переменной окружения DEBUGGER_NAMESPACE.

import {createDebugger} from '@e22m4u/js-debug';

process.env['DEBUGGER_NAMESPACE'] = 'myApp';

const debug = createDebugger();
debug('Hello world');
// myApp Hello world

Исключение переменной окружения DEBUGGER_NAMESPACE из пространства имен.

import {createDebugger} from '@e22m4u/js-debug';

process.env['DEBUGGER_NAMESPACE'] = 'myApp';

const debug1 = createDebugger();
const debug2 = debug1.withoutEnvNs();
debug1('Hello world');
debug2('Hello world');
// myApp Hello world
// Hello world

Расширение сегментов пространства имен.

import {createDebugger} from '@e22m4u/js-debug';

const debug1 = createDebugger();
const debug2 = debug1.withNs('myApp');
const debug3 = debug2.withNs('myService');
debug1('Hello world');
debug2('Hello world');
debug3('Hello world');
// Hello world
// myApp Hello world
// myApp:myService Hello world

Использование статичного хэша.

import {createDebugger} from '@e22m4u/js-debug';

const debug1 = createDebugger().withHash();
debug1('Hi John');
debug1('Hi Tommy');
// r34s Hi John
// r34s Hi Tommy

const debug2 = createDebugger().withHash();
debug2('Hi John');
debug2('Hi Tommy');
// ier0 Hi John
// ier0 Hi Tommy

Определение длины хэша.

import {createDebugger} from '@e22m4u/js-debug';

const debug = createDebugger().withHash(15);
debug('Hi John');
debug('Hi Tommy');
// we1gf4uyc4dj8f0 Hi John
// we1gf4uyc4dj8f0 Hi Tommy

Использование смещений.

import {createDebugger} from '@e22m4u/js-debug';

const debug1 = createDebugger().withOffset(1);
const debug2 = createDebugger().withOffset(2);
const debug3 = createDebugger().withOffset(3);
debug1('Hello world');
debug2('Hello world');
debug3('Hello world');
// Hello world
//    Hello world
//        Hello world

Комбинирование методов.

import {createDebugger} from '@e22m4u/js-debug';

const debug = createDebugger('myApp').withNs('myService').withHash();
const debugWo1 = debug.withOffset(1);

const contact = {
  email: 'john.doe@example.com',
  phone: {
    mobile: '+1-555-123-4567',
    home: '+1-555-987-6543'
  },
}

debug('Iterating over %v participants.', 10);
debugWo1('Looking for contacts of %v participant.', 1);
debugWo1(contact, 'Participant contacts found:');

// myApp:myService:o3pk Iterating over 10 participants.
// myApp:myService:o3pk   Looking for contacts of 1 participant.
// myApp:myService:o3pk   Participant contacts found:
// myApp:myService:o3pk   {
// myApp:myService:o3pk     email: 'john.doe@example.com',
// myApp:myService:o3pk     phone: {
// myApp:myService:o3pk       mobile: '+1-555-123-4567',
// myApp:myService:o3pk       home: '+1-555-987-6543'
// myApp:myService:o3pk     },
// myApp:myService:o3pk   }

Управление выводом

По умолчанию вызовы функции debug ничего не выводят. Чтобы увидеть отладочные сообщения, необходимо указать, какие именно пространства имен вас интересуют. Это делается с помощью специального паттерна (шаблона). Механизм включения зависит от среды выполнения.

Node.js: Переменная окружения DEBUG

В среде Node.js используется переменная окружения DEBUG. Вы можете установить её при запуске вашего скрипта.

# включить конкретное пространство имен
DEBUG=myApp node your_script.js

# включить все пространства имен, начинающиеся с 'myApp:'
DEBUG=myApp:* node your_script.js

# включить несколько пространств имен через запятую
DEBUG=myApp:service,lib:utils node your_script.js
# если используете пробелы, нужны кавычки
DEBUG="myApp:service lib:utils" node your_script.js

# включить ВСЕ пространства имен
DEBUG=* node your_script.js

Браузер: localStorage.debug

В веб-браузерах для управления выводом используется ключ debug в localStorage. Вы можете установить его значение через консоль разработчика.

// включить конкретное пространство имен
localStorage.debug = 'myApp';

// включить все пространства имен, начинающиеся с 'myApp:'
localStorage.debug = 'myApp:*';

// включить несколько (через запятую или пробел)
localStorage.debug = 'myApp:service,lib:utils';
// или
localStorage.debug = 'myApp:service lib:utils';

// включить ВСЕ
localStorage.debug = '*';

// отключить вывод
localStorage.removeItem('debug');
// или
localStorage.debug = '';

i. После изменения localStorage.debug обычно требуется перезагрузить страницу, чтобы изменения вступили в силу.

Синтаксис паттернов

  • Точное совпадение (например, myApp:myService);
  • Wildcard (*): myApp* соответствует myApp, myApp:myService и т.д.;
  • Несколько паттернов можно указать разделив из запятой или пробелом;
  • Включить всё: * включает вывод для всех пространств имен;

Примеры шаблонов в окружении Node.js:

node main.js # ничего не выведет
DEBUG=* node main.js # выведет все сообщения
DEBUG=app node main.js # только пространство имен app
DEBUG=app:* node main.js # пространства имен с префиксом app:
DEBUG=app:worker,legacy node main.js # только app:worker и legacy

Тесты

npm run test

Лицензия

MIT

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago