Skip to content

Commit

Permalink
Add log scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Mar 13, 2024
1 parent cefba35 commit 4fcb1c1
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/lib/DataProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
5 changes: 3 additions & 2 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,5 +34,6 @@ export function logger(loglevel: string): Logger {
log = pino(pinoOptions);
}
}
return log;

return scope ? log.child({ scope }) : log;
}
2 changes: 1 addition & 1 deletion src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from "./logger";
export const TIMEOUT = config.get<number>("settings.timeout");
export const LOGLEVEL = config.get<string>("settings.loglevel");

const log = logger(LOGLEVEL);
const log = logger(LOGLEVEL, "util");

/**
* Fetches a resource from a URL with a timeout.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/esplora.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mempool.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MAX_HEIGHT_DELTA = config.get<number>("settings.maxHeightDelta");
const CACHE_STDTTL = config.get<number>("cache.stdTTL");
const CACHE_CHECKPERIOD = config.get<number>("cache.checkperiod");

const log = logger(LOGLEVEL);
const log = logger(LOGLEVEL, "server");

const middlewareLogger = (message: string, ...rest: string[]) => {
log.info({ msg: message, ...rest });
Expand Down

0 comments on commit 4fcb1c1

Please sign in to comment.