Skip to content

Commit 3f2f960

Browse files
authored
feat: replace isLevelEnabled guards with Pino interpolation values (#4679)
Signed-off-by: nikolay <[email protected]>
1 parent 14955e9 commit 3f2f960

File tree

27 files changed

+336
-459
lines changed

27 files changed

+336
-459
lines changed

packages/relay/src/lib/clients/cache/localLRUCache.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export class LocalLRUCache implements ICacheClient {
122122
const cache = this.getCacheInstance(key);
123123
const value = cache.get(prefixedKey);
124124
if (value !== undefined) {
125-
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
126-
const censoredValue = JSON.stringify(value).replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
127125
if (this.logger.isLevelEnabled('trace')) {
128-
this.logger.trace(`Returning cached value ${censoredKey}:${censoredValue} on ${callingMethod} call`);
126+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
127+
const censoredValue = JSON.stringify(value).replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
128+
this.logger.trace('Returning cached value %s:%s on %s call', censoredKey, censoredValue, callingMethod);
129129
}
130130
return value;
131131
}
@@ -144,9 +144,8 @@ export class LocalLRUCache implements ICacheClient {
144144
const prefixedKey = this.prefixKey(key);
145145
const cache = this.getCacheInstance(key);
146146
const remainingTtl = cache.getRemainingTTL(prefixedKey); // in milliseconds
147-
if (this.logger.isLevelEnabled('trace')) {
148-
this.logger.trace(`returning remaining TTL ${key}:${remainingTtl} on ${callingMethod} call`);
149-
}
147+
this.logger.trace(`returning remaining TTL %s:%s on %s call`, key, remainingTtl, callingMethod);
148+
150149
return remainingTtl;
151150
}
152151

@@ -173,7 +172,7 @@ export class LocalLRUCache implements ICacheClient {
173172
const message = `Caching ${censoredKey}:${censoredValue} on ${callingMethod} for ${
174173
resolvedTtl > 0 ? `${resolvedTtl} ms` : 'indefinite time'
175174
}`;
176-
this.logger.trace(`${message} (cache size: ${this.cache.size}, max: ${this.options.max})`);
175+
this.logger.trace(`%s (cache size: %s, max: %s)`, message, this.cache.size, this.options.max);
177176
}
178177
}
179178

@@ -214,9 +213,9 @@ export class LocalLRUCache implements ICacheClient {
214213
*/
215214
public async delete(key: string, callingMethod: string): Promise<void> {
216215
const prefixedKey = this.prefixKey(key);
217-
if (this.logger.isLevelEnabled('trace')) {
218-
this.logger.trace(`delete cache for ${key} on ${callingMethod} call`);
219-
}
216+
217+
this.logger.trace(`delete cache for %s on %s call`, key, callingMethod);
218+
220219
const cache = this.getCacheInstance(key);
221220
cache.delete(prefixedKey);
222221
}
@@ -275,9 +274,8 @@ export class LocalLRUCache implements ICacheClient {
275274

276275
const matchingKeys = keys.filter((key) => regex.test(key));
277276

278-
if (this.logger.isLevelEnabled('trace')) {
279-
this.logger.trace(`retrieving keys matching ${pattern} on ${callingMethod} call`);
280-
}
277+
this.logger.trace(`retrieving keys matching %s on %s call`, pattern, callingMethod);
278+
281279
// Remove the prefix from the returned keys
282280
return matchingKeys.map((key) => key.substring(LocalLRUCache.CACHE_KEY_PREFIX.length));
283281
}

packages/relay/src/lib/clients/cache/redisCache/redisCache.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class RedisCache implements ICacheClient {
7979
if (this.logger.isLevelEnabled('trace')) {
8080
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
8181
const censoredValue = result.replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
82-
this.logger.trace(`Returning cached value ${censoredKey}:${censoredValue} on ${callingMethod} call`);
82+
this.logger.trace(`Returning cached value %s:%s on %s call`, censoredKey, censoredValue, callingMethod);
8383
}
8484
// TODO: add metrics
8585
return JSON.parse(result);
@@ -106,13 +106,13 @@ export class RedisCache implements ICacheClient {
106106
await this.client.set(prefixedKey, serializedValue);
107107
}
108108

109-
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
110-
const censoredValue = serializedValue.replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
111-
const message = `Caching ${censoredKey}:${censoredValue} on ${callingMethod} for ${
112-
resolvedTtl > 0 ? `${resolvedTtl} ms` : 'indefinite time'
113-
}`;
114109
if (this.logger.isLevelEnabled('trace')) {
115-
this.logger.trace(`${message}`);
110+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
111+
const censoredValue = serializedValue.replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
112+
const message = `Caching ${censoredKey}:${censoredValue} on ${callingMethod} for ${
113+
resolvedTtl > 0 ? `${resolvedTtl} ms` : 'indefinite time'
114+
}`;
115+
this.logger.trace(`%s`, message);
116116
}
117117
// TODO: add metrics
118118
}
@@ -138,9 +138,12 @@ export class RedisCache implements ICacheClient {
138138
await this.client.mSet(serializedKeyValuePairs);
139139

140140
// Log the operation
141-
const entriesLength = Object.keys(keyValuePairs).length;
142141
if (this.logger.isLevelEnabled('trace')) {
143-
this.logger.trace(`caching multiple keys via ${callingMethod}, total keys: ${entriesLength}`);
142+
this.logger.trace(
143+
`caching multiple keys via %s, total keys: %s`,
144+
callingMethod,
145+
Object.keys(keyValuePairs).length,
146+
);
144147
}
145148
}
146149

@@ -166,10 +169,13 @@ export class RedisCache implements ICacheClient {
166169
// Execute pipeline operation
167170
await pipeline.execAsPipeline();
168171

172+
// Log the operation
169173
if (this.logger.isLevelEnabled('trace')) {
170-
// Log the operation
171-
const entriesLength = Object.keys(keyValuePairs).length;
172-
this.logger.trace(`caching multiple keys via ${callingMethod}, total keys: ${entriesLength}`);
174+
this.logger.trace(
175+
`caching multiple keys via %s, total keys: %s`,
176+
callingMethod,
177+
Object.keys(keyValuePairs).length,
178+
);
173179
}
174180
}
175181

@@ -183,9 +189,7 @@ export class RedisCache implements ICacheClient {
183189
async delete(key: string, callingMethod: string): Promise<void> {
184190
const prefixedKey = this.prefixKey(key);
185191
await this.client.del(prefixedKey);
186-
if (this.logger.isLevelEnabled('trace')) {
187-
this.logger.trace(`delete cache for ${key} on ${callingMethod} call`);
188-
}
192+
this.logger.trace(`delete cache for %s on %s call`, key, callingMethod);
189193
// TODO: add metrics
190194
}
191195

@@ -200,9 +204,7 @@ export class RedisCache implements ICacheClient {
200204
async incrBy(key: string, amount: number, callingMethod: string): Promise<number> {
201205
const prefixedKey = this.prefixKey(key);
202206
const result = await this.client.incrBy(prefixedKey, amount);
203-
if (this.logger.isLevelEnabled('trace')) {
204-
this.logger.trace(`incrementing ${key} by ${amount} on ${callingMethod} call`);
205-
}
207+
this.logger.trace(`incrementing %s by %s on %s call`, key, amount, callingMethod);
206208
return result;
207209
}
208210

@@ -218,9 +220,7 @@ export class RedisCache implements ICacheClient {
218220
async lRange(key: string, start: number, end: number, callingMethod: string): Promise<any[]> {
219221
const prefixedKey = this.prefixKey(key);
220222
const result = await this.client.lRange(prefixedKey, start, end);
221-
if (this.logger.isLevelEnabled('trace')) {
222-
this.logger.trace(`retrieving range [${start}:${end}] from ${key} on ${callingMethod} call`);
223-
}
223+
this.logger.trace(`retrieving range [%s:%s] from %s on %s call`, start, end, key, callingMethod);
224224
return result.map((item) => JSON.parse(item));
225225
}
226226

@@ -236,9 +236,8 @@ export class RedisCache implements ICacheClient {
236236
const prefixedKey = this.prefixKey(key);
237237
const serializedValue = JSON.stringify(value);
238238
const result = await this.client.rPush(prefixedKey, serializedValue);
239-
if (this.logger.isLevelEnabled('trace')) {
240-
this.logger.trace(`pushing ${serializedValue} to ${key} on ${callingMethod} call`);
241-
}
239+
this.logger.trace(`pushing %s to %s on %s call`, serializedValue, key, callingMethod);
240+
242241
return result;
243242
}
244243

@@ -251,9 +250,7 @@ export class RedisCache implements ICacheClient {
251250
async keys(pattern: string, callingMethod: string): Promise<string[]> {
252251
const prefixedPattern = this.prefixKey(pattern);
253252
const result = await this.client.keys(prefixedPattern);
254-
if (this.logger.isLevelEnabled('trace')) {
255-
this.logger.trace(`retrieving keys matching ${pattern} on ${callingMethod} call`);
256-
}
253+
this.logger.trace(`retrieving keys matching %s on %s call`, pattern, callingMethod);
257254
// Remove the prefix from the returned keys
258255
return result.map((key) => key.substring(RedisCache.CACHE_KEY_PREFIX.length));
259256
}
@@ -277,9 +274,7 @@ export class RedisCache implements ICacheClient {
277274

278275
await pipeline.exec();
279276

280-
if (this.logger.isLevelEnabled('trace')) {
281-
this.logger.trace(`Cleared ${keysToDelete.length} cache keys`);
282-
}
277+
this.logger.trace(`Cleared %s cache keys`, keysToDelete.length);
283278
}
284279
}
285280
}

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ export class MirrorNodeClient {
224224
retries: mirrorNodeRetries,
225225
retryDelay: (retryCount, error) => {
226226
const delay = mirrorNodeRetryDelay * retryCount;
227-
if (this.logger.isLevelEnabled('trace')) {
228-
this.logger.trace(`Retry delay ${delay} ms on '${error?.request?.path}'`);
229-
}
227+
this.logger.trace(`Retry delay %s ms on '%s'`, delay, error?.request?.path);
230228
return delay;
231229
},
232230
retryCondition: (error) => {
@@ -290,7 +288,9 @@ export class MirrorNodeClient {
290288
});
291289

292290
this.logger.info(
293-
`Mirror Node client successfully configured to REST url: ${this.restUrl} and Web3 url: ${this.web3Url} `,
291+
`Mirror Node client successfully configured to REST url: %s and Web3 url: %s `,
292+
this.restUrl,
293+
this.web3Url,
294294
);
295295
this.cacheService = cacheService;
296296

@@ -377,7 +377,7 @@ export class MirrorNodeClient {
377377
try {
378378
return JSONBigInt.parse(data);
379379
} catch (error) {
380-
this.logger.warn(`Failed to parse response data from Mirror Node: ${error}`);
380+
this.logger.warn(`Failed to parse response data from Mirror Node: %s`, error);
381381
}
382382
}
383383

@@ -394,9 +394,12 @@ export class MirrorNodeClient {
394394
const ms = Date.now() - start;
395395
if (this.logger.isLevelEnabled('debug')) {
396396
this.logger.debug(
397-
`Successfully received response from mirror node server: method=${method}, path=${path}, status=${
398-
response.status
399-
}, duration:${ms}ms, data:${JSON.stringify(response.data)}`,
397+
`Successfully received response from mirror node server: method=%s, path=%s, status=%s, duration:%sms, data:%s`,
398+
method,
399+
path,
400+
response.status,
401+
ms,
402+
JSON.stringify(response.data),
400403
);
401404
}
402405
this.mirrorResponseHistogram.labels(pathLabel, response.status?.toString()).observe(ms);
@@ -451,27 +454,35 @@ export class MirrorNodeClient {
451454
const acceptedErrorResponses = MirrorNodeClient.acceptedErrorStatusesResponsePerRequestPathMap.get(pathLabel);
452455

453456
if (error.response && acceptedErrorResponses?.includes(effectiveStatusCode)) {
454-
if (this.logger.isLevelEnabled('debug')) {
455-
this.logger.debug(
456-
`An accepted error occurred while communicating with the mirror node server: method=${method}, path=${path}, status=${effectiveStatusCode}`,
457-
);
458-
}
457+
this.logger.debug(
458+
`An accepted error occurred while communicating with the mirror node server: method=%s, path=%s, status=%s}`,
459+
method,
460+
path,
461+
effectiveStatusCode,
462+
);
459463
return null;
460464
}
461465

462466
// Contract Call returns 400 for a CONTRACT_REVERT but is a valid response, expected and should not be logged as error:
463467
if (pathLabel === MirrorNodeClient.CONTRACT_CALL_ENDPOINT && effectiveStatusCode === 400) {
464468
if (this.logger.isLevelEnabled('debug')) {
465469
this.logger.debug(
466-
`[${method}] ${path} Contract Revert: ( StatusCode: '${effectiveStatusCode}', StatusText: '${
467-
error.response.statusText
468-
}', Detail: '${JSON.stringify(error.response.detail)}',Data: '${JSON.stringify(error.response.data)}')`,
470+
`[%s] %s Contract Revert: ( StatusCode: '%s', StatusText: '%s', Detail: '%s',Data: '%s')`,
471+
method,
472+
path,
473+
effectiveStatusCode,
474+
error.response.statusText,
475+
JSON.stringify(error.response.detail),
476+
JSON.stringify(error.response.data),
469477
);
470478
}
471479
} else {
472480
this.logger.error(
473481
new Error(error.message),
474-
`Error encountered while communicating with the mirror node server: method=${method}, path=${path}, status=${effectiveStatusCode}`,
482+
`Error encountered while communicating with the mirror node server: method=%s, path=%s, status=%s`,
483+
method,
484+
path,
485+
effectiveStatusCode,
475486
);
476487
}
477488

@@ -496,9 +507,7 @@ export class MirrorNodeClient {
496507

497508
if (page === pageMax) {
498509
// max page reached
499-
if (this.logger.isLevelEnabled('trace')) {
500-
this.logger.trace(`Max page reached ${pageMax} with ${results.length} results`);
501-
}
510+
this.logger.trace(`Max page reached %s with %s results`, pageMax, results.length);
502511
throw predefined.PAGINATION_MAX(pageMax);
503512
}
504513

@@ -575,15 +584,15 @@ export class MirrorNodeClient {
575584
const match = url.match(regex);
576585
const accountId = match ? match[1] : null;
577586
if (!accountId) {
578-
this.logger.error(`Unable to extract evm address from url ${url}`);
587+
this.logger.error(`Unable to extract evm address from url %s`, url);
579588
}
580589
return String(accountId);
581590
} else {
582591
// account id
583592
const match = url.match(MirrorNodeClient.EVM_ADDRESS_REGEX);
584593
const accountId = match ? match[1] : null;
585594
if (!accountId) {
586-
this.logger.error(`Unable to extract account ID from url ${url}`);
595+
this.logger.error(`Unable to extract account ID from url %s`, url);
587596
}
588597
return String(accountId);
589598
}
@@ -802,9 +811,9 @@ export class MirrorNodeClient {
802811
// Found immature record, log the info, set flag and exit record traversal
803812
if (this.logger.isLevelEnabled('debug')) {
804813
this.logger.debug(
805-
`Contract result contains nullable transaction_index or block_number, or block_hash is an empty hex (0x): contract_result=${JSON.stringify(
806-
contractObject,
807-
)}. ${!isLastAttempt ? `Retrying after a delay of ${mirrorNodeRetryDelay} ms.` : ``}`,
814+
`Contract result contains nullable transaction_index or block_number, or block_hash is an empty hex (0x): contract_result=%s. %s}`,
815+
JSON.stringify(contractObject),
816+
!isLastAttempt ? `Retrying after a delay of ${mirrorNodeRetryDelay} ms.` : ``,
808817
);
809818
}
810819

@@ -991,9 +1000,9 @@ export class MirrorNodeClient {
9911000
// Found immature record, log the info, set flag and exit record traversal
9921001
if (this.logger.isLevelEnabled('debug')) {
9931002
this.logger.debug(
994-
`Contract result log contains nullable transaction_index, block_number, index, or block_hash is an empty hex (0x): log=${JSON.stringify(
995-
log,
996-
)}. ${!isLastAttempt ? `Retrying after a delay of ${mirrorNodeRetryDelay} ms.` : ``}`,
1003+
`Contract result log contains nullable transaction_index, block_number, index, or block_hash is an empty hex (0x): log=%s. %s`,
1004+
JSON.stringify(log),
1005+
!isLastAttempt ? `Retrying after a delay of ${mirrorNodeRetryDelay} ms.` : ``,
9971006
);
9981007
}
9991008

@@ -1484,7 +1493,9 @@ export class MirrorNodeClient {
14841493
} else {
14851494
this.logger.warn(
14861495
e,
1487-
`Error raised during polling mirror node for updated records: method=${methodName}, args=${args}`,
1496+
`Error raised during polling mirror node for updated records: method=%s, args=%s`,
1497+
methodName,
1498+
args,
14881499
);
14891500
}
14901501
}
@@ -1495,9 +1506,12 @@ export class MirrorNodeClient {
14951506

14961507
if (this.logger.isLevelEnabled('debug')) {
14971508
this.logger.debug(
1498-
`Repeating request ${methodName} with args ${JSON.stringify(
1499-
args,
1500-
)} retry count ${i} of ${repeatCount}. Waiting ${this.MIRROR_NODE_RETRY_DELAY} ms before repeating request`,
1509+
`Repeating request %s with args %s retry count %s of %s. Waiting %s ms before repeating request`,
1510+
methodName,
1511+
JSON.stringify(args),
1512+
i,
1513+
repeatCount,
1514+
this.MIRROR_NODE_RETRY_DELAY,
15011515
);
15021516
}
15031517

@@ -1523,12 +1537,11 @@ export class MirrorNodeClient {
15231537
operatorAccountId: string,
15241538
requestDetails: RequestDetails,
15251539
): Promise<ITransactionRecordMetric> {
1526-
if (this.logger.isLevelEnabled('debug')) {
1527-
this.logger.debug(
1528-
`Get transaction record via mirror node: transactionId=${transactionId}, txConstructorName=${txConstructorName}`,
1529-
);
1530-
}
1531-
1540+
this.logger.debug(
1541+
`Get transaction record via mirror node: transactionId=%s, txConstructorName=%s`,
1542+
transactionId,
1543+
txConstructorName,
1544+
);
15321545
// Create a modified copy of requestDetails
15331546
const modifiedRequestDetails = new RequestDetails({
15341547
requestId: requestDetails.requestId,

0 commit comments

Comments
 (0)