logger 选项在编译期间为 React 编译器事件提供自定义日志记录。
🌐 The logger option provides custom logging for React Compiler events during compilation.
{
logger: {
logEvent(filename, event) {
console.log(`[Compiler] ${event.kind}: ${filename}`);
}
}
}参考
🌐 Reference
logger
配置自定义日志记录以跟踪编译器行为和调试问题。
🌐 Configures custom logging to track compiler behavior and debug issues.
类型
🌐 Type
{
logEvent: (filename: string | null, event: LoggerEvent) => void;
} | null默认值
🌐 Default value
null
方法
🌐 Methods
logEvent:在每个编译器事件发生时调用,传入文件名和事件详情
事件类型
🌐 Event types
CompileSuccess: 函数编译成功CompileError:由于错误,功能被跳过CompileDiagnostic:非致命诊断信息CompileSkip:因其他原因跳过功能PipelineError: 意外的编译错误Timing:性能时序信息
注意事项
🌐 Caveats
- 事件结构可能会在不同版本之间发生变化
- 大型代码库会生成大量日志条目
用法
🌐 Usage
基本日志记录
🌐 Basic logging
跟踪编译成功和失败:
🌐 Track compilation success and failures:
{
logger: {
logEvent(filename, event) {
switch (event.kind) {
case 'CompileSuccess': {
console.log(`✅ Compiled: ${filename}`);
break;
}
case 'CompileError': {
console.log(`❌ Skipped: ${filename}`);
break;
}
default: {}
}
}
}
}详细的错误日志
🌐 Detailed error logging
获取有关编译失败的具体信息:
🌐 Get specific information about compilation failures:
{
logger: {
logEvent(filename, event) {
if (event.kind === 'CompileError') {
console.error(`\nCompilation failed: ${filename}`);
console.error(`Reason: ${event.detail.reason}`);
if (event.detail.description) {
console.error(`Details: ${event.detail.description}`);
}
if (event.detail.loc) {
const { line, column } = event.detail.loc.start;
console.error(`Location: Line ${line}, Column ${column}`);
}
if (event.detail.suggestions) {
console.error('Suggestions:', event.detail.suggestions);
}
}
}
}
}