Skip to content

Commit 07d22e7

Browse files
authored
enable to customize logger in the replayer (#1111)
1 parent 4ee86fe commit 07d22e7

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ The replayer accepts options as its constructor's second parameter, and it has t
311311
| logConfig | - | configuration of console output playback, refer to the [console recipe](./docs/recipes/console.md) |
312312
| plugins | [] | load plugins to provide extended replay functions. [What is plugins?](./docs/recipes/plugin.md) |
313313
| useVirtualDom | true | whether to use Virtual Dom optimization in the process of skipping to a new point of time |
314+
| logger | console | The logger object used by the replayer to print warnings or errors |
314315

315316
#### Use rrweb-player
316317

guide.zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ replayer.destroy();
306306
| unpackFn | - | 数据解压缩函数,详见[优化存储策略](./docs/recipes/optimize-storage.zh_CN.md) |
307307
| plugins | [] | 加载插件以获得额外的回放功能. [什么是插件?](./docs/recipes/plugin.zh_CN.md) |
308308
| useVirtualDom | true | 在播放器跳转到一个新的时间点的过程中,是否使用 Virtual Dom 优化 |
309+
| logger | console | 当播放器出现警告或错误时用来打印日志的对象 |
309310

310311
#### 使用 rrweb-player
311312

packages/rrweb/src/replay/index.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class Replayer {
184184
pauseAnimation: true,
185185
mouseTail: defaultMouseTailConfig,
186186
useVirtualDom: true, // Virtual-dom optimization is enabled by default.
187+
logger: console,
187188
};
188189
this.config = Object.assign({}, defaultConfig, config);
189190

@@ -269,9 +270,7 @@ export class Replayer {
269270
);
270271
value.node = realNode;
271272
} catch (error) {
272-
if (this.config.showWarning) {
273-
console.warn(error);
274-
}
273+
this.warn(error);
275274
}
276275
}
277276
}
@@ -495,7 +494,7 @@ export class Replayer {
495494
}
496495

497496
public resume(timeOffset = 0) {
498-
console.warn(
497+
this.warn(
499498
`The 'resume' was deprecated in 1.0. Please use 'play' method which has the same interface.`,
500499
);
501500
this.play(timeOffset);
@@ -753,10 +752,10 @@ export class Replayer {
753752
isSync = false,
754753
) {
755754
if (!this.iframe.contentDocument) {
756-
return console.warn('Looks like your replayer has been destroyed.');
755+
return this.warn('Looks like your replayer has been destroyed.');
757756
}
758757
if (Object.keys(this.legacy_missingNodeRetryMap).length) {
759-
console.warn(
758+
this.warn(
760759
'Found unresolved missing node map',
761760
this.legacy_missingNodeRetryMap,
762761
);
@@ -1252,12 +1251,10 @@ export class Replayer {
12521251
mediaEl.playbackRate = d.playbackRate;
12531252
}
12541253
} catch (error) {
1255-
if (this.config.showWarning) {
1256-
console.warn(
1257-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
1258-
`Failed to replay media interactions: ${error.message || error}`,
1259-
);
1260-
}
1254+
this.warn(
1255+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
1256+
`Failed to replay media interactions: ${error.message || error}`,
1257+
);
12611258
}
12621259
break;
12631260
}
@@ -1314,9 +1311,7 @@ export class Replayer {
13141311
);
13151312
this.iframe.contentDocument?.fonts.add(fontFace);
13161313
} catch (error) {
1317-
if (this.config.showWarning) {
1318-
console.warn(error);
1319-
}
1314+
this.warn(error);
13201315
}
13211316
break;
13221317
}
@@ -1354,9 +1349,7 @@ export class Replayer {
13541349
);
13551350
if (virtualNode) value.node = virtualNode;
13561351
} catch (error) {
1357-
if (this.config.showWarning) {
1358-
console.warn(error);
1359-
}
1352+
this.warn(error);
13601353
}
13611354
}
13621355
}
@@ -1437,7 +1430,7 @@ export class Replayer {
14371430

14381431
const appendNode = (mutation: addedNodeMutation) => {
14391432
if (!this.iframe.contentDocument) {
1440-
return console.warn('Looks like your replayer has been destroyed.');
1433+
return this.warn('Looks like your replayer has been destroyed.');
14411434
}
14421435
let parent: Node | null | ShadowRoot | RRNode = mirror.getNode(
14431436
mutation.parentId,
@@ -1718,12 +1711,10 @@ export class Replayer {
17181711
value,
17191712
);
17201713
} catch (error) {
1721-
if (this.config.showWarning) {
1722-
console.warn(
1723-
'An error occurred may due to the checkout feature.',
1724-
error,
1725-
);
1726-
}
1714+
this.warn(
1715+
'An error occurred may due to the checkout feature.',
1716+
error,
1717+
);
17271718
}
17281719
} else if (attributeName === 'style') {
17291720
const styleValues = value;
@@ -2136,20 +2127,20 @@ export class Replayer {
21362127
* is microtask, so events fired on a removed DOM may emit
21372128
* snapshots in the reverse order.
21382129
*/
2139-
this.debug(REPLAY_CONSOLE_PREFIX, `Node with id '${id}' not found. `, d);
2130+
this.debug(`Node with id '${id}' not found. `, d);
21402131
}
21412132

21422133
private warn(...args: Parameters<typeof console.warn>) {
21432134
if (!this.config.showWarning) {
21442135
return;
21452136
}
2146-
console.warn(REPLAY_CONSOLE_PREFIX, ...args);
2137+
this.config.logger.warn(REPLAY_CONSOLE_PREFIX, ...args);
21472138
}
21482139

21492140
private debug(...args: Parameters<typeof console.log>) {
21502141
if (!this.config.showDebug) {
21512142
return;
21522143
}
2153-
console.log(REPLAY_CONSOLE_PREFIX, ...args);
2144+
this.config.logger.log(REPLAY_CONSOLE_PREFIX, ...args);
21542145
}
21552146
}

packages/rrweb/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ export type playerConfig = {
180180
};
181181
unpackFn?: UnpackFn;
182182
useVirtualDom: boolean;
183+
logger: {
184+
log: (...args: Parameters<typeof console.log>) => void;
185+
warn: (...args: Parameters<typeof console.warn>) => void;
186+
};
183187
plugins?: ReplayPlugin[];
184188
};
185189

0 commit comments

Comments
 (0)