Skip to content

Commit 691ef70

Browse files
mondorealeCopilot
andauthored
refactor: Avoid NodeJS-specific module (use explicit logger scopes) (#3309)
This pull request standardizes logger initialization throughout the codebase by replacing the use of `new Logger(module)` with a more descriptive string identifier for each module or class. This improves the clarity of log messages and makes it easier to trace logs back to their source components. ### Changes **Logger initialization updates (all changes):** * Replaced `new Logger(module)` with `new Logger('<ClassOrFileName>')` in all relevant files across `autocertifier-client`, `autocertifier-server`, `cdn-location`, and `dht` packages to ensure consistent and descriptive logging. * Replaced all occurrences of `loggerFactory.createLogger(module)` with `loggerFactory.createLogger('<ClassOrFileName>')` in all relevant files across the monorepo. ### Future steps > [!NOTE] > Because logger instances are created at the top level of each module, simply importing the package into a host project will **eagerly instantiate all associated loggers**, even if the corresponding exports are never used. 1. Many modules currently instantiate a logger at import time (e.g. `new Logger('Foo')`). This introduces a side effect during module evaluation, which prevents effective tree-shaking of both the module itself and the `Logger` implementation. A future improvement would be to decouple logger creation from module imports by moving initialization behind explicit factory functions or lazy accessors. This would reduce import-time side effects and allow unused modules (and the logger code) to be safely tree-shaken by the bundler. 2. `Logger` was the key reason why we kept using (and polyfilling) `module`. Consumers will be able to drop that env-specific "dependency" from their setups soon. --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: mondoreale <320066+mondoreale@users.noreply.github.com>
1 parent 8977cb8 commit 691ef70

File tree

168 files changed

+173
-175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+173
-175
lines changed

packages/autocertifier-client/src/AutoCertifierClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface AutoCertifierClientEvents {
1515

1616
export type HasSession = (request: HasSessionRequest, context: ServerCallContext) => Promise<HasSessionResponse>
1717

18-
const logger = new Logger(module)
18+
const logger = new Logger('AutoCertifierClient')
1919

2020
const ensureConfigFileWritable = (directory: string): void => {
2121
const baseDirectory = getBaseDirectory(directory)

packages/autocertifier-client/src/RestClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CreateCertifiedSubdomainRequest } from './data/CreateCertifiedSubdomain
55
import { Logger } from '@streamr/utils'
66
import { makeHttpRequest } from './makeHttpRequest'
77

8-
const logger = new Logger(module)
8+
const logger = new Logger('RestClient')
99

1010
export class RestClient {
1111

packages/autocertifier-server/bin/copyARecordsToRoute53.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import chunk from 'lodash/chunk'
66
import { ChangeAction } from '@aws-sdk/client-route-53'
77

88
(async () => {
9-
const logger = new Logger(module)
9+
const logger = new Logger('copyARecordsToRoute53')
1010

1111
const domainName = validateEnvironmentVariable('AUTOCERTIFIER_DOMAIN_NAME')
1212
const databaseFilePath = validateEnvironmentVariable('AUTOCERTIFIER_DATABASE_FILE_PATH')

packages/autocertifier-server/src/AutoCertifierServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { ChallengeManager } from './ChallengeManager'
1212
import { RRType } from '@aws-sdk/client-route-53'
1313
import { Route53Api } from './Route53Api'
1414

15-
const logger = new Logger(module)
15+
const logger = new Logger('AutoCertifierServer')
1616

1717
export const validateEnvironmentVariable = (name: string): string | never => {
1818
const value = process.env[name]

packages/autocertifier-server/src/CertificateCreator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'path'
55
import { ChallengeManager } from './ChallengeManager'
66
import { Challenge } from 'acme-client/types/rfc8555'
77

8-
const logger = new Logger(module)
8+
const logger = new Logger('CertificateCreator')
99

1010
// https://letsencrypt.org/docs/challenge-types/#dns-01-challenge
1111
const DNS_01_CHALLENGE = 'dns-01'

packages/autocertifier-server/src/Database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { open, Statement, Database as SqliteDatabase } from 'sqlite'
55
import { Logger, filePathToNodeFormat } from '@streamr/utils'
66
import { DatabaseError, InvalidSubdomainOrToken } from '@streamr/autocertifier-client'
77

8-
const logger = new Logger(module)
8+
const logger = new Logger('Database')
99

1010
export class Database {
1111

packages/autocertifier-server/src/DnsServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type AsyncDnsHandler = (
88
...args: Parameters<DnsHandler>
99
) => Promise<void>
1010

11-
const logger = new Logger(module)
11+
const logger = new Logger('DnsServer')
1212

1313
// https://help.dnsfilter.com/hc/en-us/articles/4408415850003-DNS-Return-Codes
1414
// DNS Query Format Error

packages/autocertifier-server/src/RestServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import bodyParser from 'body-parser'
1414
import * as https from 'https'
1515
import * as fs from 'fs'
1616

17-
const logger = new Logger(module)
17+
const logger = new Logger('RestServer')
1818

1919
type ExpressType = ReturnType<typeof express>
2020
type ServerType = ReturnType<ExpressType['listen']>

packages/autocertifier-server/src/StreamrChallenger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { toProtoRpcClient } from '@streamr/proto-rpc'
1515
import { Logger } from '@streamr/utils'
1616
import { FailedToConnectToStreamrWebSocket, AutoCertifierRpcClient, SERVICE_ID } from '@streamr/autocertifier-client'
1717

18-
const logger = new Logger(module)
18+
const logger = new Logger('StreamrChallenger')
1919

2020
// This is a dummy peer descriptor that is used to connect to the streamr websocket
2121
// To ensure that the autocertified subdomain is used for the Streamr Network

packages/cdn-location/src/fetchAirportCodeFromCdn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Logger, withTimeout } from '@streamr/utils'
22

3-
const logger = new Logger(module)
3+
const logger = new Logger('fetchAirportCodeFromCdn')
44

55
const fetchHeader: (url: string, header: string, timeout: number) => Promise<string | null> = async (
66
url: string,

0 commit comments

Comments
 (0)