From 1447a1f635dfd8c66a5da3f82ec8ca7dc5c824a5 Mon Sep 17 00:00:00 2001 From: Andrii Andreiev <129078694+AndriiAndreiev@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:44:20 +0300 Subject: [PATCH] lint, small refactoring --- packages/node/src/lib/log.ts | 2 +- packages/node/src/lib/logger.ts | 25 +++++++++++++------------ packages/node/src/lib/metrics-log.ts | 2 ++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/node/src/lib/log.ts b/packages/node/src/lib/log.ts index e5fa96212..bbdc46fd1 100644 --- a/packages/node/src/lib/log.ts +++ b/packages/node/src/lib/log.ts @@ -12,10 +12,10 @@ import config from '../config'; import { constructPayload } from './construct-payload'; import { getProjectBaseUrl } from './get-project-base-url'; import isRequest from './is-request'; +import { logger } from './logger'; import { metricsAPICall } from './metrics-log'; import { patchRequest } from './patch-request'; import { patchResponse } from './patch-response'; -import { logger } from './logger'; let queue: OutgoingLogBody[] = []; function doSend(readmeApiKey: string, options: Options) { diff --git a/packages/node/src/lib/logger.ts b/packages/node/src/lib/logger.ts index 2b3cff9a3..ab63f8cca 100644 --- a/packages/node/src/lib/logger.ts +++ b/packages/node/src/lib/logger.ts @@ -1,21 +1,14 @@ interface Log { - message: string; args?: Record; + message: string; } type ErrorLog = Log & { err: Error } -function formatMessage( - level: 'TRACE' | 'DEBUG' | 'ERROR', - message: string -) { - return [`${level} ${new Date().toISOString()} [readmeio] ${message}`] -} - export interface Logger { - trace(log: Log): void; debug(log: Log): void; error(log: ErrorLog): void; + trace(log: Log): void; } class DefaultLogger implements Logger { @@ -28,8 +21,16 @@ class DefaultLogger implements Logger { return DefaultLogger.instance; } + // eslint-disable-next-line class-methods-use-this + private formatMessage( + level: 'DEBUG' | 'ERROR' | 'TRACE', + message: string + ) { + return [`${level} ${new Date().toISOString()} [readmeio] ${message}`] + } + trace({ message, args }: Log) { - const params: unknown[] = formatMessage('TRACE', message) + const params: unknown[] = this.formatMessage('TRACE', message) if (args) { params.push('\nArguments:', args) } @@ -37,7 +38,7 @@ class DefaultLogger implements Logger { } debug({ message, args }: Log) { - const params: unknown[] = formatMessage('DEBUG', message) + const params: unknown[] = this.formatMessage('DEBUG', message) if (args) { params.push('\nArguments:', args) } @@ -45,7 +46,7 @@ class DefaultLogger implements Logger { } error({ message, args, err }: ErrorLog) { - const params: unknown[] = formatMessage('ERROR', message) + const params: unknown[] = this.formatMessage('ERROR', message) if (args) { params.push('\nArguments:', args) } diff --git a/packages/node/src/lib/metrics-log.ts b/packages/node/src/lib/metrics-log.ts index 1701d1665..0a292577b 100644 --- a/packages/node/src/lib/metrics-log.ts +++ b/packages/node/src/lib/metrics-log.ts @@ -7,6 +7,8 @@ import timeoutSignal from 'timeout-signal'; import pkg from '../../package.json'; import config from '../config'; +import { logger } from './logger'; + export interface GroupingObject { /** * API Key used to make the request. Note that this is different from the `readmeAPIKey`