Skip to content

Commit

Permalink
fix: add default logger options
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-wave committed Aug 28, 2024
1 parent 99263e5 commit 1577391
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type Listener = () => void;

let instanceCounter = 0;

const defaultLoggerOptions = {
context: 'Onboarding',
};
const getDefaultBaseState = (): BaseState => ({
availablePresets: [],
activePresets: [],
Expand Down Expand Up @@ -88,7 +91,10 @@ export class Controller<HintParams, Presets extends string, Steps extends string
this.events = new EventEmitter(this);
this.hintStore = hintStore || new HintStore(this.events);
this.passStepListeners = new Set();
this.logger = createLogger(options.logger ?? {});
this.logger = createLogger({
...defaultLoggerOptions,
...this.options.logger,
});

if (this.options.debugMode) {
// @ts-ignore
Expand Down
6 changes: 1 addition & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ export type LoggerOptions = {
context?: string;
};
const noop = () => {};
export const createLogger = ({
level = 'error',
logger = console,
context = 'Onboarding',
}: LoggerOptions) => {
export const createLogger = ({level = 'error', logger = console, context}: LoggerOptions) => {
return {
debug:
level === 'debug'
Expand Down
13 changes: 8 additions & 5 deletions src/promo-manager/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const defaultInitOptions: InitPromoManagerOptions = {
timeout: 0,
};

const defaultLoggerOptions = {
context: 'Promo manager',
};

const delay = (timeout: number) =>
new Promise<void>((resolve) => {
setTimeout(resolve, timeout);
Expand Down Expand Up @@ -71,11 +75,10 @@ export class Controller {

this.status = 'idle';

this.logger = createLogger(
this.options.logger ?? {
context: 'Promo manager',
},
);
this.logger = createLogger({
...defaultLoggerOptions,
...this.options.logger,
});
this.logger.debug('Initialization started');

this.state = JSON.parse(
Expand Down

0 comments on commit 1577391

Please sign in to comment.