Skip to content

Commit

Permalink
fix(js/core): make sure genkit logger is a true global singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj committed Dec 10, 2024
1 parent c61c37e commit c0190be
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions js/core/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,69 @@

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

class Logger {
logger: {
debug(...args: any);
info(...args: any);
warn(...args: any);
error(...args: any);
level: string;
};
const loggerKey = '__genkit_logger';

defaultLogger = {
shouldLog(targetLevel: string) {
return LOG_LEVELS.indexOf(this.level) <= LOG_LEVELS.indexOf(targetLevel);
},
debug(...args: any) {
this.shouldLog('debug') && console.debug(...args);
},
info(...args: any) {
this.shouldLog('info') && console.info(...args);
},
warn(...args: any) {
this.shouldLog('warn') && console.warn(...args);
},
error(...args: any) {
this.shouldLog('error') && console.error(...args);
},
level: 'info',
};
const _defaultLogger = {
shouldLog(targetLevel: string) {
return LOG_LEVELS.indexOf(this.level) <= LOG_LEVELS.indexOf(targetLevel);
},
debug(...args: any) {
this.shouldLog('debug') && console.debug(...args);
},
info(...args: any) {
this.shouldLog('info') && console.info(...args);
},
warn(...args: any) {
this.shouldLog('warn') && console.warn(...args);
},
error(...args: any) {
this.shouldLog('error') && console.error(...args);
},
level: 'info',
};

constructor() {
this.logger = this.defaultLogger;
function getLogger() {
if (!global[loggerKey]) {
global[loggerKey] = _defaultLogger;
}
return global[loggerKey];
}

class Logger {
readonly defaultLogger = _defaultLogger;

async init(fn: any) {
this.logger = fn;
init(fn: any) {
global[loggerKey] = fn;
}

info(...args: any) {
// eslint-disable-next-line prefer-spread
this.logger.info.apply(this.logger, args);
getLogger().info.apply(getLogger(), args);
}
debug(...args: any) {
// eslint-disable-next-line prefer-spread
this.logger.debug.apply(this.logger, args);
getLogger().debug.apply(getLogger(), args);
}
error(...args: any) {
// eslint-disable-next-line prefer-spread
this.logger.error.apply(this.logger, args);
getLogger().error.apply(getLogger(), args);
}
warn(...args: any) {
// eslint-disable-next-line prefer-spread
this.logger.warn.apply(this.logger, args);
getLogger().warn.apply(getLogger(), args);
}

setLogLevel(level: 'error' | 'warn' | 'info' | 'debug') {
this.logger.level = level;
getLogger().level = level;
}

logStructured(msg: string, metadata: any) {
this.logger.info(msg, metadata);
getLogger().info(msg, metadata);
}

logStructuredError(msg: string, metadata: any) {
this.logger.error(msg, metadata);
getLogger().error(msg, metadata);
}
}

export const logger = new Logger();
export const logger = new Logger();

0 comments on commit c0190be

Please sign in to comment.