Skip to content

Commit

Permalink
fix: lazily init logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed May 18, 2022
1 parent c224211 commit 91efdd9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ const logging = new Logging();
// min and max log levels numeric interface used there
const [MIN_LEVEL, MAX_LEVEL] = [0, 4];

logging.setProjectId().catch(err => {
console.error(`failed to set logging project id ${err}`);
});
logging.setDetectedResource().catch(err => {
console.error(`failed to discover resource metadata ${err}`);
});

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pjson = require('../../package.json');

Expand Down Expand Up @@ -80,6 +73,19 @@ export class Logger {
}
}

let didLoggingInit = false;

export function createLogger(level?: number): Logger {
if (!didLoggingInit) {
logging.setProjectId().catch(err => {
console.error(`failed to set logging project id ${err}`);
});
logging.setDetectedResource().catch(err => {
console.error(`failed to discover resource metadata ${err}`);
});

didLoggingInit = true;
}

return new Logger(level);
}

0 comments on commit 91efdd9

Please sign in to comment.