Skip to content

Commit b9b4886

Browse files
feat(node): debug and trace logging
1 parent 25c0258 commit b9b4886

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

packages/node/src/lib/get-project-base-url.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import timeoutSignal from 'timeout-signal';
77
import pkg from '../../package.json';
88
import config from '../config';
99

10+
import { logger } from './logger';
11+
1012
export function getCache(readmeApiKey: string) {
1113
const encodedApiKey = Buffer.from(`${readmeApiKey}:`).toString('base64');
1214
const cacheDir = findCacheDir({ name: pkg.name, create: true });
@@ -48,7 +50,7 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c
4850
if (res.status >= 400 && res.status <= 599) {
4951
throw res;
5052
}
51-
53+
logger.debug({ message: `Fetch Base URL: Service responded with status ${res.status}: ${res.statusText}` });
5254
return res.json() as Promise<{ baseUrl: string }>;
5355
})
5456
.then(project => {

packages/node/src/lib/log.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function doSend(readmeApiKey: string, options: Options) {
3131
logger.error({ message: 'Error making API call', err });
3232
}
3333
});
34+
35+
logger.debug({ message: 'Queue is cleared.', args: { queue } });
3436
}
3537
// Make sure we flush the queue if the process is exited
3638
process.on('exit', doSend);
@@ -168,6 +170,7 @@ export function log(
168170
);
169171

170172
queue.push(payload);
173+
logger.debug({ message: 'Queue is appended.', args: { queue } });
171174
if (queue.length >= bufferLength) doSend(readmeApiKey, options);
172175

173176
cleanup(); // eslint-disable-line @typescript-eslint/no-use-before-define

packages/node/src/lib/logger.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ class DefaultLogger implements Logger {
6868
trace({ message, args }: Log): void {
6969
if (!this.config.isLoggingEnabled) return;
7070
const params: unknown[] = this.formatMessage('TRACE', message);
71+
console.log(...params);
7172
if (args) {
72-
params.push('\nArguments:', args);
73+
console.dir(args, { depth: null });
74+
console.log('\n');
7375
}
74-
console.debug(...params);
7576
}
7677

7778
/**
@@ -84,7 +85,7 @@ class DefaultLogger implements Logger {
8485
if (args) {
8586
params.push('\nArguments:', args);
8687
}
87-
console.debug(...params);
88+
console.debug(...params, '\n');
8889
}
8990

9091
/**
@@ -100,7 +101,7 @@ class DefaultLogger implements Logger {
100101
if (err) {
101102
params.push('\n', err);
102103
}
103-
console.error(...params);
104+
console.error(...params, '\n');
104105
}
105106
}
106107

packages/node/src/lib/metrics-log.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export function metricsAPICall(readmeAPIKey: string, body: OutgoingLogBody[], op
101101
// after the backoff expires, erase the old expiration time
102102
backoffExpiresAt = undefined;
103103
}
104+
logger.trace({ message: 'Making a POST request to a service...', args: { body } });
104105
return fetch(new URL('/v1/request', config.host), {
105106
method: 'post',
106107
body: JSON.stringify(body),
@@ -121,6 +122,7 @@ export function metricsAPICall(readmeAPIKey: string, body: OutgoingLogBody[], op
121122
backoffExpiresAt.setSeconds(backoffExpiresAt.getSeconds() + BACKOFF_SECONDS);
122123
}
123124
}
125+
logger.debug({ message: `Service responded with status ${response.status}: ${response.statusText}` });
124126
return response;
125127
})
126128
.finally(() => {

0 commit comments

Comments
 (0)