Skip to content

Commit

Permalink
fix: lazily init logging (#801)
Browse files Browse the repository at this point in the history
Co-authored-by: Punya Biswal <punya@google.com>
Co-authored-by: Aaron Abbott <aaronabbott@google.com>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent 2411b71 commit 04853a9
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 04853a9

Please sign in to comment.