Skip to content

Commit

Permalink
refactor(logging): moved constants to separate file (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strazz1337 authored Mar 29, 2024
1 parent f1786dc commit f08260e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,8 @@ import { GlobalProviders } from '@zonneplan/open-telemetry-node';
import { LogAttributes, SeverityNumber } from '@opentelemetry/api-logs';
import { LoggerService } from '../services/logger.service';
import { context } from '@opentelemetry/api';
import { NEST_LOG_LEVEL_WINSTON_SEVERITY, SEVERITY_TEXT_TO_NEST_LOG_LEVEL } from '../constants';

/**
* @see https://github.com/winstonjs/winston?tab=readme-ov-file#logging
*/
const NEST_LOG_LEVEL_WINSTON_SEVERITY: Record<LogLevel, number> = {
fatal: 0,
error: 0,
warn: 1,
log: 2,
verbose: 4,
debug: 5
};

const SEVERITY_TEXT_TO_NEST_LOG_LEVEL: Record<string, LogLevel> = {
FATAL: 'fatal',
ERROR: 'error',
WARN: 'warn',
INFO: 'log',
DEBUG: 'debug',
TRACE: 'verbose',
UNSPECIFIED: 'log'
};

export class NestWinstonLoggerAdapter extends LoggerService {
private severityNumberToSeverityTextMap: Record<number, string> = {
Expand Down Expand Up @@ -127,7 +107,7 @@ export class NestWinstonLoggerAdapter extends LoggerService {

const winstonLogLevel = this.toWinstonLogLevel(nestLogLevel);
this.logger[winstonLogLevel](body, attributes);

if (!GlobalProviders.logProvider) {
console.error('OpenTelemetry log provider not initialized');
return;
Expand Down
23 changes: 23 additions & 0 deletions packages/open-telemetry-nest/src/logging/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { LogLevel } from '@nestjs/common';

/**
* @see https://github.com/winstonjs/winston?tab=readme-ov-file#logging
*/
export const NEST_LOG_LEVEL_WINSTON_SEVERITY: Record<LogLevel, number> = {
fatal: 0,
error: 0,
warn: 1,
log: 2,
verbose: 4,
debug: 5
};

export const SEVERITY_TEXT_TO_NEST_LOG_LEVEL: Record<string, LogLevel> = {
FATAL: 'fatal',
ERROR: 'error',
WARN: 'warn',
INFO: 'log',
DEBUG: 'debug',
TRACE: 'verbose',
UNSPECIFIED: 'log'
};

0 comments on commit f08260e

Please sign in to comment.