Skip to content

Commit

Permalink
Merge pull request #139 from sima-land/log-handlers-and-docs-upd
Browse files Browse the repository at this point in the history
Экспорт хендлеров логгирования для Sentry и Pino, актуализация документации раздела log
  • Loading branch information
krutoo authored Aug 9, 2024
2 parents 077a176 + 7ddb70e commit 97a692e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 6 deletions.
9 changes: 4 additions & 5 deletions docs/docs/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ if (somethingWrong) {

### Примеры использования

Подготовим логгер и обработчик для него. Для работы обработчику нужен Sentry Hub.
Подготовим логгер и обработчик для него. Для работы обработчику нужен Sentry Scope.

```ts
import { createLogger } from '@sima-land/isomorph/log';
import { createSentryHandler } from '@sima-land/isomorph/log/handler/sentry';
import { BrowserClient, Hub } from '@sentry/browser';
import { createLogger, createSentryHandler } from '@sima-land/isomorph/log';
import { BrowserClient, Scope } from '@sentry/browser';

const sentryClient = new BrowserClient({ dsn: process.env.SENTRY_DSN });
const sentryHub = new Hub(sentryClient);
const sentryScope = new Scope(); // пустой scope в качестве примера

const logger = createLogger();

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ sidebar_position: 4

### DI-пресеты

TODO ссылки на разделы в документации по пресетам
WIP ссылки на разделы в документации по пресетам
43 changes: 43 additions & 0 deletions src/log/handler/__test__/pino.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pino from 'pino';
import { createPinoHandler } from '../pino';
import { createLogger } from '../../logger';

describe('createPinoHandler', () => {
it('should make handler properly', () => {
const stub: pino.Logger = {
info: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
} as unknown as pino.Logger;

const logger = createLogger();

logger.subscribe(createPinoHandler(stub));

expect(stub.info).toHaveBeenCalledTimes(0);
logger.info('AAA');
expect(stub.info).toHaveBeenCalledTimes(1);
expect(stub.info).toHaveBeenCalledWith('AAA');

expect(stub.info).toHaveBeenCalledTimes(1);
logger.log('AAA');
expect(stub.info).toHaveBeenCalledTimes(2);
expect(stub.info).toHaveBeenCalledWith('AAA');

expect(stub.warn).toHaveBeenCalledTimes(0);
logger.warn('BBB');
expect(stub.warn).toHaveBeenCalledTimes(1);
expect(stub.warn).toHaveBeenCalledWith('BBB');

expect(stub.debug).toHaveBeenCalledTimes(0);
logger.debug('CCC');
expect(stub.debug).toHaveBeenCalledTimes(1);
expect(stub.debug).toHaveBeenCalledWith('CCC');

expect(stub.error).toHaveBeenCalledTimes(0);
logger.error('DDD');
expect(stub.error).toHaveBeenCalledTimes(1);
expect(stub.error).toHaveBeenCalledWith('DDD');
});
});
96 changes: 96 additions & 0 deletions src/log/handler/__test__/sentry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { createSentryHandler } from '../sentry';
import { createLogger } from '../../logger';
import { Breadcrumb, DetailedError } from '../../errors';
import type { Scope } from '@sentry/types';

describe('createSentryHandler', () => {
it('should handle function as scopeInit', () => {
const scope: Scope = {
setLevel: jest.fn(),
setContext: jest.fn(),
setExtra: jest.fn(),
captureException: jest.fn(),
addBreadcrumb: jest.fn(),
} as unknown as Scope;

const getScope = jest.fn(() => scope);

const logger = createLogger();

logger.subscribe(createSentryHandler(getScope));

expect(scope.captureException).toHaveBeenCalledTimes(0);
logger.error('fake error 1');
logger.error('fake error 2');
logger.error('fake error 3');
expect(scope.captureException).toHaveBeenCalledTimes(3);
});

it('should make handler properly', () => {
const scope: Scope = {
setLevel: jest.fn(),
setContext: jest.fn(),
setExtra: jest.fn(),
captureException: jest.fn(),
addBreadcrumb: jest.fn(),
} as unknown as Scope;

const logger = createLogger();

logger.subscribe(createSentryHandler(scope));

// error level with non DetailedError
expect(scope.captureException).toHaveBeenCalledTimes(0);
logger.error('fake error');
expect(scope.captureException).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledWith('fake error');

// error level with DetailedError with defined level
const error = new DetailedError('fake', { level: 'fatal' });
expect(scope.setLevel).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(1);
logger.error(error);
expect(scope.setLevel).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(2);
expect(scope.captureException).toHaveBeenCalledWith(error);

// error level with DetailedError with defined context
const error2 = new DetailedError('fake', { context: { key: 'CTX', data: {} } });
expect(scope.setContext).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(2);
logger.error(error2);
expect(scope.setContext).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(3);
expect(scope.captureException).toHaveBeenCalledWith(error2);

// error level with DetailedError with defined context as array
const error3 = new DetailedError('fake', { context: [{ key: 'CTX', data: {} }] });
expect(scope.setContext).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(3);
logger.error(error3);
expect(scope.setContext).toHaveBeenCalledTimes(2);
expect(scope.captureException).toHaveBeenCalledTimes(4);
expect(scope.captureException).toHaveBeenCalledWith(error3);

// error level with DetailedError with defined extra
const error4 = new DetailedError('fake', { extra: { key: 'EXTRA', data: {} } });
expect(scope.setExtra).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(4);
logger.error(error4);
expect(scope.setExtra).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(5);
expect(scope.captureException).toHaveBeenCalledWith(error4);

// non error level with non Breadcrumb
expect(scope.captureException).toHaveBeenCalledTimes(5);
logger.info('hello');
expect(scope.captureException).toHaveBeenCalledTimes(5);

// non error level with Breadcrumb
const breadcrumb = new Breadcrumb({ category: 'test' });
expect(scope.addBreadcrumb).toHaveBeenCalledTimes(0);
logger.info(breadcrumb);
expect(scope.addBreadcrumb).toHaveBeenCalledTimes(1);
expect(scope.addBreadcrumb).toHaveBeenCalledWith(breadcrumb.data);
});
});
2 changes: 2 additions & 0 deletions src/log/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export type {

export { createLogger } from './logger';
export { DetailedError, Breadcrumb } from './errors';
export { createPinoHandler } from './handler/pino';
export { createSentryHandler } from './handler/sentry';

0 comments on commit 97a692e

Please sign in to comment.