From 4fcb1c19a7aff87df3ae664fb7cc5f6f9df8daab Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Wed, 13 Mar 2024 12:33:18 +0100 Subject: [PATCH] Add log scopes --- src/lib/DataProviderManager.ts | 2 +- src/lib/logger.ts | 5 +++-- src/lib/util.ts | 2 +- src/providers/bitcoind.ts | 2 +- src/providers/esplora.ts | 2 +- src/providers/mempool.ts | 2 +- src/server.tsx | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib/DataProviderManager.ts b/src/lib/DataProviderManager.ts index fbfc824..7a6a53e 100644 --- a/src/lib/DataProviderManager.ts +++ b/src/lib/DataProviderManager.ts @@ -3,7 +3,7 @@ import { LOGLEVEL } from "./util"; import { logger } from "./logger"; import { MempoolProvider } from "../providers/mempool"; -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "data-provider-manager"); export class DataProviderManager { private providers: Provider[] = []; diff --git a/src/lib/logger.ts b/src/lib/logger.ts index dd4f1ef..3b62f11 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -12,7 +12,7 @@ import pino, { type Logger } from "pino"; * @param loglevel - The log level to set for the logger. * @returns A new logger with the specified log level. */ -export function logger(loglevel: string): Logger { +export function logger(loglevel: string, scope?: string | undefined): Logger { let log: Logger; const pinoOptions = { level: loglevel, @@ -34,5 +34,6 @@ export function logger(loglevel: string): Logger { log = pino(pinoOptions); } } - return log; + + return scope ? log.child({ scope }) : log; } diff --git a/src/lib/util.ts b/src/lib/util.ts index fdcc346..a37d6ef 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -4,7 +4,7 @@ import { logger } from "./logger"; export const TIMEOUT = config.get("settings.timeout"); export const LOGLEVEL = config.get("settings.loglevel"); -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "util"); /** * Fetches a resource from a URL with a timeout. diff --git a/src/providers/bitcoind.ts b/src/providers/bitcoind.ts index 1c9c241..e738b10 100644 --- a/src/providers/bitcoind.ts +++ b/src/providers/bitcoind.ts @@ -3,7 +3,7 @@ import { LOGLEVEL } from "../lib/util"; import { logger } from "../lib/logger"; import { promisify } from "util"; -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "bitcoind"); /** * A class that provides data from a Bitcoind server. diff --git a/src/providers/esplora.ts b/src/providers/esplora.ts index 0c41560..b017f16 100644 --- a/src/providers/esplora.ts +++ b/src/providers/esplora.ts @@ -1,7 +1,7 @@ import { fetchData, LOGLEVEL, TIMEOUT } from "../lib/util"; import { logger } from "../lib/logger"; -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "esplora"); /** * A class that provides data from an Esplora server. diff --git a/src/providers/mempool.ts b/src/providers/mempool.ts index a953f63..57ecff6 100644 --- a/src/providers/mempool.ts +++ b/src/providers/mempool.ts @@ -1,7 +1,7 @@ import { fetchData, LOGLEVEL, TIMEOUT } from "../lib/util"; import { logger } from "../lib/logger"; -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "mempool"); /** * A class that provides data from a Mempool server. diff --git a/src/server.tsx b/src/server.tsx index fe1a3b9..850c016 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -39,7 +39,7 @@ const MAX_HEIGHT_DELTA = config.get("settings.maxHeightDelta"); const CACHE_STDTTL = config.get("cache.stdTTL"); const CACHE_CHECKPERIOD = config.get("cache.checkperiod"); -const log = logger(LOGLEVEL); +const log = logger(LOGLEVEL, "server"); const middlewareLogger = (message: string, ...rest: string[]) => { log.info({ msg: message, ...rest });