enable to customize logger in the replayer (#1111)

This commit is contained in:
Yun Feng
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 12c2671644
commit 306da78607
4 changed files with 25 additions and 28 deletions

View File

@@ -311,6 +311,7 @@ The replayer accepts options as its constructor's second parameter, and it has t
| logConfig | - | configuration of console output playback, refer to the [console recipe](./docs/recipes/console.md) |
| plugins | [] | load plugins to provide extended replay functions. [What is plugins?](./docs/recipes/plugin.md) |
| useVirtualDom | true | whether to use Virtual Dom optimization in the process of skipping to a new point of time |
| logger | console | The logger object used by the replayer to print warnings or errors |
#### Use rrweb-player

View File

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

View File

@@ -184,6 +184,7 @@ export class Replayer {
pauseAnimation: true,
mouseTail: defaultMouseTailConfig,
useVirtualDom: true, // Virtual-dom optimization is enabled by default.
logger: console,
};
this.config = Object.assign({}, defaultConfig, config);
@@ -269,9 +270,7 @@ export class Replayer {
);
value.node = realNode;
} catch (error) {
if (this.config.showWarning) {
console.warn(error);
}
this.warn(error);
}
}
}
@@ -495,7 +494,7 @@ export class Replayer {
}
public resume(timeOffset = 0) {
console.warn(
this.warn(
`The 'resume' was deprecated in 1.0. Please use 'play' method which has the same interface.`,
);
this.play(timeOffset);
@@ -753,10 +752,10 @@ export class Replayer {
isSync = false,
) {
if (!this.iframe.contentDocument) {
return console.warn('Looks like your replayer has been destroyed.');
return this.warn('Looks like your replayer has been destroyed.');
}
if (Object.keys(this.legacy_missingNodeRetryMap).length) {
console.warn(
this.warn(
'Found unresolved missing node map',
this.legacy_missingNodeRetryMap,
);
@@ -1252,12 +1251,10 @@ export class Replayer {
mediaEl.playbackRate = d.playbackRate;
}
} catch (error) {
if (this.config.showWarning) {
console.warn(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
`Failed to replay media interactions: ${error.message || error}`,
);
}
this.warn(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
`Failed to replay media interactions: ${error.message || error}`,
);
}
break;
}
@@ -1314,9 +1311,7 @@ export class Replayer {
);
this.iframe.contentDocument?.fonts.add(fontFace);
} catch (error) {
if (this.config.showWarning) {
console.warn(error);
}
this.warn(error);
}
break;
}
@@ -1354,9 +1349,7 @@ export class Replayer {
);
if (virtualNode) value.node = virtualNode;
} catch (error) {
if (this.config.showWarning) {
console.warn(error);
}
this.warn(error);
}
}
}
@@ -1437,7 +1430,7 @@ export class Replayer {
const appendNode = (mutation: addedNodeMutation) => {
if (!this.iframe.contentDocument) {
return console.warn('Looks like your replayer has been destroyed.');
return this.warn('Looks like your replayer has been destroyed.');
}
let parent: Node | null | ShadowRoot | RRNode = mirror.getNode(
mutation.parentId,
@@ -1718,12 +1711,10 @@ export class Replayer {
value,
);
} catch (error) {
if (this.config.showWarning) {
console.warn(
'An error occurred may due to the checkout feature.',
error,
);
}
this.warn(
'An error occurred may due to the checkout feature.',
error,
);
}
} else if (attributeName === 'style') {
const styleValues = value;
@@ -2136,20 +2127,20 @@ export class Replayer {
* is microtask, so events fired on a removed DOM may emit
* snapshots in the reverse order.
*/
this.debug(REPLAY_CONSOLE_PREFIX, `Node with id '${id}' not found. `, d);
this.debug(`Node with id '${id}' not found. `, d);
}
private warn(...args: Parameters<typeof console.warn>) {
if (!this.config.showWarning) {
return;
}
console.warn(REPLAY_CONSOLE_PREFIX, ...args);
this.config.logger.warn(REPLAY_CONSOLE_PREFIX, ...args);
}
private debug(...args: Parameters<typeof console.log>) {
if (!this.config.showDebug) {
return;
}
console.log(REPLAY_CONSOLE_PREFIX, ...args);
this.config.logger.log(REPLAY_CONSOLE_PREFIX, ...args);
}
}

View File

@@ -180,6 +180,10 @@ export type playerConfig = {
};
unpackFn?: UnpackFn;
useVirtualDom: boolean;
logger: {
log: (...args: Parameters<typeof console.log>) => void;
warn: (...args: Parameters<typeof console.warn>) => void;
};
plugins?: ReplayPlugin[];
};