Skip to content

Commit 01fd64f

Browse files
jonahsniderpunyaaabmass
authored
fix: lazily init logging (#801)
Co-authored-by: Punya Biswal <punya@google.com> Co-authored-by: Aaron Abbott <aaronabbott@google.com>
1 parent 72ef061 commit 01fd64f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

handwritten/cloud-profiler/src/logger.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ const logging = new Logging();
2323
// min and max log levels numeric interface used there
2424
const [MIN_LEVEL, MAX_LEVEL] = [0, 4];
2525

26-
logging.setProjectId().catch(err => {
27-
console.error(`failed to set logging project id ${err}`);
28-
});
29-
logging.setDetectedResource().catch(err => {
30-
console.error(`failed to discover resource metadata ${err}`);
31-
});
32-
3326
// eslint-disable-next-line @typescript-eslint/no-var-requires
3427
const pjson = require('../../package.json');
3528

@@ -80,6 +73,19 @@ export class Logger {
8073
}
8174
}
8275

76+
let didLoggingInit = false;
77+
8378
export function createLogger(level?: number): Logger {
79+
if (!didLoggingInit) {
80+
logging.setProjectId().catch(err => {
81+
console.error(`failed to set logging project id ${err}`);
82+
});
83+
logging.setDetectedResource().catch(err => {
84+
console.error(`failed to discover resource metadata ${err}`);
85+
});
86+
87+
didLoggingInit = true;
88+
}
89+
8490
return new Logger(level);
8591
}

0 commit comments

Comments
 (0)