Skip to content

Commit

Permalink
Merge pull request #53 from LN-Zap/feat/logger-scope
Browse files Browse the repository at this point in the history
Add log scopes
  • Loading branch information
mrfelton authored Mar 13, 2024
2 parents 07cfdc2 + 3b59852 commit ac9b7fb
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 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;
}
6 changes: 3 additions & 3 deletions 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 All @@ -22,7 +22,7 @@ export async function fetchWithTimeout(
url: string,
timeout: number = TIMEOUT,
): Promise<Response> {
log.debug({ message: `Starting fetch request to ${url}` });
log.debug({ message: `Starting fetch request to "${url}"` });
const fetchPromise = fetch(url);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function fetchData<T>(
: response.text());
return data as T;
} catch (error) {
log.error({ msg: `Error fetching data from ${url}:`, error });
log.error({ msg: `Error fetching data from "${url}":`, error });
throw error;
}
}
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 ac9b7fb

Please sign in to comment.