Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class BKTClientImpl implements Bucketeer {
this.eventEmitter.close();
this.featureFlagProcessor?.stop();
this.segementUsersCacheProcessor?.stop();
this.config.logger?.info('destroy finished', this.registerEventsScheduleID);
this.config.logger?.info('destroy finished');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was like

destroy finished Timeout {
      _idleTimeout: -1,
      _idlePrev: null,
      _idleNext: null,
      _idleStart: 5616,
      _onTimeout: null,
      _timerArgs: undefined,
      _repeat: 30000,
      _destroyed: true,
      [Symbol(refed)]: true,
      [Symbol(kHasPrimitive)]: false,
      [Symbol(asyncId)]: 644,
      [Symbol(triggerId)]: 0
    }

It made me feel like something bad had happened, but it hadn’t.

}

getBuildInfo(): BuildInfo {
Expand Down
51 changes: 20 additions & 31 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { format } from 'util';

export interface Logger {
error(...args: any[]): void;
warn(...args: any[]): void;
info(...args: any[]): void;
debug(...args: any[]): void;
}

const DEBUG = 'debug';
const INFO = 'info';
const WARN = 'warn';
const ERROR = 'error';
const NONE = 'none';
type ConsoleMethod = typeof console.error;

const logLevels = [DEBUG, INFO, WARN, ERROR, NONE];
const logLevelIndices: Record<string, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3,
none: 4,
};

/**
* A default logger that writes to stderr.
*/
export class DefaultLogger implements Logger {
prefix: string;
minLevel: number;

/**
Expand All @@ -30,36 +29,26 @@ export class DefaultLogger implements Logger {
*/
constructor(logLevel?: string) {
this.minLevel = 1;
for (let i = 0; i < logLevels.length; i++) {
if (logLevels[i] === logLevel) {
this.minLevel = i;
break;
}
if (logLevel && logLevel in logLevelIndices) {
this.minLevel = logLevelIndices[logLevel];
}
this.prefix = logLevel + ': [Bucketeer] ';
}

private log(level: string, consoleFn: ConsoleMethod, ...args: any[]) {
if (this.minLevel > logLevelIndices[level]) return;
consoleFn(`${level}: [Bucketeer]`, ...args);
}

error(...args: any[]): void {
this.write(args, ERROR);
this.log('error', console.error, ...args);
}
warn(...args: any[]): void {
this.write(args, WARN);
this.log('warn', console.warn, ...args);
}
info(...args: any[]): void {
this.write(args, INFO);
this.log('info', console.info, ...args);
}
debug(...args: any[]): void {
this.write(args, DEBUG);
}

write(args: any[], logLevel: (typeof logLevels)[number]): void {
if (this.minLevel > logLevels.indexOf(logLevel)) {
return;
}
if (args.length === 1) {
return;
}
args[0] = this.prefix + args[0];
console.error(format(...args));
this.log('debug', console.debug, ...args);
}
}
}