Skip to content

Commit c0190be

Browse files
committed
fix(js/core): make sure genkit logger is a true global singleton
1 parent c61c37e commit c0190be

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

Diff for: js/core/src/logging.ts

+37-38
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,69 @@
1616

1717
const LOG_LEVELS = ['debug', 'info', 'warn', 'error'];
1818

19-
class Logger {
20-
logger: {
21-
debug(...args: any);
22-
info(...args: any);
23-
warn(...args: any);
24-
error(...args: any);
25-
level: string;
26-
};
19+
const loggerKey = '__genkit_logger';
2720

28-
defaultLogger = {
29-
shouldLog(targetLevel: string) {
30-
return LOG_LEVELS.indexOf(this.level) <= LOG_LEVELS.indexOf(targetLevel);
31-
},
32-
debug(...args: any) {
33-
this.shouldLog('debug') && console.debug(...args);
34-
},
35-
info(...args: any) {
36-
this.shouldLog('info') && console.info(...args);
37-
},
38-
warn(...args: any) {
39-
this.shouldLog('warn') && console.warn(...args);
40-
},
41-
error(...args: any) {
42-
this.shouldLog('error') && console.error(...args);
43-
},
44-
level: 'info',
45-
};
21+
const _defaultLogger = {
22+
shouldLog(targetLevel: string) {
23+
return LOG_LEVELS.indexOf(this.level) <= LOG_LEVELS.indexOf(targetLevel);
24+
},
25+
debug(...args: any) {
26+
this.shouldLog('debug') && console.debug(...args);
27+
},
28+
info(...args: any) {
29+
this.shouldLog('info') && console.info(...args);
30+
},
31+
warn(...args: any) {
32+
this.shouldLog('warn') && console.warn(...args);
33+
},
34+
error(...args: any) {
35+
this.shouldLog('error') && console.error(...args);
36+
},
37+
level: 'info',
38+
};
4639

47-
constructor() {
48-
this.logger = this.defaultLogger;
40+
function getLogger() {
41+
if (!global[loggerKey]) {
42+
global[loggerKey] = _defaultLogger;
4943
}
44+
return global[loggerKey];
45+
}
46+
47+
class Logger {
48+
readonly defaultLogger = _defaultLogger;
5049

51-
async init(fn: any) {
52-
this.logger = fn;
50+
init(fn: any) {
51+
global[loggerKey] = fn;
5352
}
5453

5554
info(...args: any) {
5655
// eslint-disable-next-line prefer-spread
57-
this.logger.info.apply(this.logger, args);
56+
getLogger().info.apply(getLogger(), args);
5857
}
5958
debug(...args: any) {
6059
// eslint-disable-next-line prefer-spread
61-
this.logger.debug.apply(this.logger, args);
60+
getLogger().debug.apply(getLogger(), args);
6261
}
6362
error(...args: any) {
6463
// eslint-disable-next-line prefer-spread
65-
this.logger.error.apply(this.logger, args);
64+
getLogger().error.apply(getLogger(), args);
6665
}
6766
warn(...args: any) {
6867
// eslint-disable-next-line prefer-spread
69-
this.logger.warn.apply(this.logger, args);
68+
getLogger().warn.apply(getLogger(), args);
7069
}
7170

7271
setLogLevel(level: 'error' | 'warn' | 'info' | 'debug') {
73-
this.logger.level = level;
72+
getLogger().level = level;
7473
}
7574

7675
logStructured(msg: string, metadata: any) {
77-
this.logger.info(msg, metadata);
76+
getLogger().info(msg, metadata);
7877
}
7978

8079
logStructuredError(msg: string, metadata: any) {
81-
this.logger.error(msg, metadata);
80+
getLogger().error(msg, metadata);
8281
}
8382
}
8483

85-
export const logger = new Logger();
84+
export const logger = new Logger();

0 commit comments

Comments
 (0)