-
Notifications
You must be signed in to change notification settings - Fork 615
chore: cleanup libp2p logger #12058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: cleanup libp2p logger #12058
Changes from 4 commits
c9efe9f
0c7288e
fb2b07a
646eadf
484e361
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { type ComponentLogger, type Logger } from '@libp2p/interface'; | ||
|
|
||
| import { getLogLevelFromFilters } from './log-filters.js'; | ||
| import { logFilters, logger } from './pino-logger.js'; | ||
|
|
||
| /** | ||
| * Creates a libp2p compatible logger that wraps our pino logger. | ||
| * This adapter implements the ComponentLogger interface required by libp2p. | ||
| */ | ||
| export function createLibp2pComponentLogger(namespace: string): ComponentLogger { | ||
| return { | ||
| forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}`), | ||
| }; | ||
| } | ||
|
|
||
| function createLibp2pLogger(component: string): Logger { | ||
| // Create a direct pino logger instance for libp2p that supports string interpolation | ||
| const log = logger.child({ module: component }, { level: getLogLevelFromFilters(logFilters, component) }); | ||
|
|
||
| // Default log level is trace as this is super super noisy | ||
| const logFn = (message: string, ...args: unknown[]) => { | ||
| log.trace(message, ...args); | ||
| }; | ||
|
|
||
| return Object.assign(logFn, { | ||
| enabled: log.isLevelEnabled('debug'), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I had already commented about whether this should be hardcoded, and you had replied, and I can't remember the answer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty much that it is extremely noisy, and we only want to be able to see it whenever we are running with LOG_LEVEL=debug and below |
||
|
|
||
| error(message: string, ...args: unknown[]) { | ||
| log.error(message, ...args); | ||
| }, | ||
|
|
||
| debug(message: string, ...args: unknown[]) { | ||
| log.debug(message, ...args); | ||
| }, | ||
|
|
||
| info(message: string, ...args: unknown[]) { | ||
| log.info(message, ...args); | ||
| }, | ||
|
|
||
| warn(message: string, ...args: unknown[]) { | ||
| log.warn(message, ...args); | ||
| }, | ||
|
|
||
| trace(message: string, ...args: unknown[]) { | ||
| log.trace(message, ...args); | ||
| }, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,78 +0,0 @@ | ||
| import { createLogger } from '@aztec/foundation/log'; | ||
|
|
||
| import { type ComponentLogger, type Logger } from '@libp2p/interface'; | ||
|
|
||
| /** | ||
| * Creates a libp2p compatible logger that wraps our pino logger. | ||
| * This adapter implements the ComponentLogger interface required by libp2p. | ||
| */ | ||
| export function createLibp2pComponentLogger(namespace: string, fixedTerms = {}): ComponentLogger { | ||
| return { | ||
| forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}`, fixedTerms), | ||
| }; | ||
| } | ||
|
|
||
| function createLibp2pLogger(component: string, fixedTerms = {}): Logger { | ||
| const logger = createLogger(component, fixedTerms); | ||
|
|
||
| // Default log level is trace as this is super super noisy | ||
| const logFn = (formatter: any, ...args: any[]) => { | ||
| // Handle %p format specifier by manually replacing with args | ||
| if (typeof formatter === 'string' && args.length > 0) { | ||
| // Handle %p, %a, %s and %d format specifiers | ||
| const parts = formatter.split(/(%p|%a|%s|%d)/); | ||
| let result = parts[0]; | ||
| let argIndex = 0; | ||
|
|
||
| for (let i = 1; i < parts.length; i += 2) { | ||
| if (argIndex < args.length) { | ||
| result += String(args[argIndex]) + (parts[i + 1] || ''); | ||
| argIndex++; | ||
| } | ||
| } | ||
|
|
||
| formatter = result; | ||
| // Only keep non-format args as data | ||
| args = args.slice(argIndex); | ||
| } | ||
|
|
||
| // Handle object args by spreading them, but only if they weren't used in formatting | ||
| if (args.length === 1 && typeof args[0] === 'object') { | ||
| logger.trace(formatter, args[0]); | ||
| } else if (args.length > 0) { | ||
| // If we have remaining args after formatting, pass them as data | ||
| logger.trace(formatter, { _args: args }); | ||
| } else { | ||
| logger.trace(formatter); | ||
| } | ||
| }; | ||
|
|
||
| return Object.assign(logFn, { | ||
| enabled: logger.isLevelEnabled('debug'), | ||
|
|
||
| error(...args: any[]) { | ||
| const [msg, ...rest] = args; | ||
| logger.error(msg as string, ...rest); | ||
| }, | ||
|
|
||
| debug(...args: any[]) { | ||
| const [msg, ...rest] = args; | ||
| logger.debug(msg as string, ...rest); | ||
| }, | ||
|
|
||
| info(...args: any[]) { | ||
| const [msg, ...rest] = args; | ||
| logger.info(msg as string, ...rest); | ||
| }, | ||
|
|
||
| warn(...args: any[]) { | ||
| const [msg, ...rest] = args; | ||
| logger.warn(msg as string, ...rest); | ||
| }, | ||
|
|
||
| trace(...args: any[]) { | ||
| const [msg, ...rest] = args; | ||
| logger.trace(msg as string, ...rest); | ||
| }, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -19,7 +19,7 @@ import { | |||
| } from '@aztec/circuit-types'; | ||||
| import { Fr } from '@aztec/circuits.js'; | ||||
| import { type EpochCacheInterface } from '@aztec/epoch-cache'; | ||||
| import { createLogger } from '@aztec/foundation/log'; | ||||
| import { createLibp2pComponentLogger, createLogger } from '@aztec/foundation/log'; | ||||
| import { SerialQueue } from '@aztec/foundation/queue'; | ||||
| import { RunningPromise } from '@aztec/foundation/running-promise'; | ||||
| import type { AztecAsyncKVStore } from '@aztec/kv-store'; | ||||
|
|
@@ -65,7 +65,6 @@ import { pingHandler, reqRespBlockHandler, reqRespTxHandler, statusHandler } fro | |||
| import { ReqResp } from '../reqresp/reqresp.js'; | ||||
| import type { P2PService, PeerDiscoveryService } from '../service.js'; | ||||
| import { GossipSubEvent } from '../types.js'; | ||||
| import { createLibp2pComponentLogger } from './libp2p_logger.js'; | ||||
|
|
||||
| interface MessageValidator { | ||||
| validator: { | ||||
|
|
@@ -269,7 +268,7 @@ export class LibP2PService<T extends P2PClientType> extends WithTracer implement | |||
| }), | ||||
| }, | ||||
| // Fix the peer id in libp2p logs so we can see the source of the log | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| logger: createLibp2pComponentLogger(logger.module, { sourcePeerId: peerId }), | ||||
| logger: createLibp2pComponentLogger(logger.module), | ||||
| }); | ||||
|
|
||||
| return new LibP2PService( | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add
libp2p/interfaceto foundation's dev dependencies