Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazily init logging #801

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@
}
}

let didLoggingInit = false;

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

Check warning on line 81 in src/logger.ts

View check run for this annotation

Codecov / codecov/patch

src/logger.ts#L81

Added line #L81 was not covered by tests
});
logging.setDetectedResource().catch(err => {
console.error(`failed to discover resource metadata ${err}`);

Check warning on line 84 in src/logger.ts

View check run for this annotation

Codecov / codecov/patch

src/logger.ts#L84

Added line #L84 was not covered by tests
});

didLoggingInit = true;
}

return new Logger(level);
}
Loading