Skip to content

Commit

Permalink
lint, small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiAndreiev committed Aug 5, 2024
1 parent 0085f15 commit 1447a1f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/node/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 13 additions & 12 deletions packages/node/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
interface Log {
message: string;
args?: Record<string, unknown>;
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 {
Expand All @@ -28,24 +21,32 @@ 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)
}
console.debug(...params)
}

debug({ message, args }: Log) {
const params: unknown[] = formatMessage('DEBUG', message)
const params: unknown[] = this.formatMessage('DEBUG', message)
if (args) {
params.push('\nArguments:', args)
}
console.debug(...params)
}

error({ message, args, err }: ErrorLog) {
const params: unknown[] = formatMessage('ERROR', message)
const params: unknown[] = this.formatMessage('ERROR', message)
if (args) {
params.push('\nArguments:', args)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/node/src/lib/metrics-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 1447a1f

Please sign in to comment.