日志记录、错误报告与诊断
构建日志记录和错误报告层,以便诊断运行在真实用户设备上的扩展所出现的问题。
日志记录、错误报告与诊断 是 CoddyKit 上的免费 Browser Extensions Development (Chrome & Edge) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Browser Extensions Development (Chrome & Edge) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
You Cannot Watch Every Console
Once your extension ships, you cannot open dev tools on every user's browser. A deliberate logging and error-reporting strategy is how you learn what is going wrong in the wild.
A Central Log Function
Wrap logging in one function so you can change behavior in a single place, add timestamps, and silence output in production.
function log(level, msg) {
console[level]('[MyExt] ' + new Date().toISOString() + ' ' + msg)
}Log Levels
Use distinct levels so you can filter noise. Common ones: debug, info, warn, and error.
log('warn', 'Quota nearly full')
log('error', 'Sync failed')Catching Uncaught Errors
Add a global error handler in each context to capture exceptions you did not anticipate.
self.addEventListener('error', (e) => {
log('error', e.message + ' @ ' + e.filename)
})Catching Rejected Promises
Unhandled promise rejections are a common silent failure. Listen for unhandledrejection too.
self.addEventListener('unhandledrejection', (e) => {
log('error', 'Unhandled: ' + e.reason)
})Persisting a Log Ring Buffer
Keep the last N log entries in storage so users can export them when reporting a bug. Cap the size to avoid filling the quota.
async function persist(entry) {
const { logs = [] } = await chrome.storage.local.get('logs')
logs.push(entry)
if (logs.length > 200) logs.shift()
await chrome.storage.local.set({ logs })
}Letting Users Export Logs
Add an options-page button that dumps stored logs to a downloadable file, making support requests far more useful.
const blob = new Blob([JSON.stringify(logs, null, 2)], { type: 'application/json' })
const url = URL.createObjectURL(blob)
chrome.downloads.download({ url, filename: 'myext-logs.json' })Remote Error Reporting
For aggregate insight, POST anonymized error reports to your own endpoint. Always make this opt-in and strip personal data.
fetch('https://errors.example.com', {
method: 'POST',
body: JSON.stringify({ msg, version })
})Respecting Privacy
Never log page contents, URLs with tokens, or anything sensitive. Disclose any remote reporting in your privacy policy and store listing.
Silencing Logs in Production
Gate verbose logging behind a debug flag so release builds stay quiet and fast, while you can flip it on when investigating.
const DEBUG = false
function debug(msg) { if (DEBUG) log('debug', msg) }Correlating Across Contexts
Tag each log with its source (popup, content, worker) so you can trace a bug that spans contexts and messaging.
log('info', '[worker] received PING')Quick Check
Test your diagnostics knowledge.
Recap
You built a diagnostics layer:
- Central log function with levels
- Global
errorandunhandledrejectionhandlers - A capped log buffer in storage with export
- Opt-in, privacy-safe remote reporting
- A debug flag and per-context tagging
Good diagnostics turn mysterious field bugs into fixable reports.
常见问题解答
「日志记录、错误报告与诊断」课时是免费的吗?
是的 — 「日志记录、错误报告与诊断」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Browser Extensions Development (Chrome & Edge) 课程的其余内容,请升级到 CoddyKit PRO。 Browser Extensions Development (Chrome & Edge) 课程共包含 4 节课。
「日志记录、错误报告与诊断」这节课中我会学到什么?
构建日志记录和错误报告层,以便诊断运行在真实用户设备上的扩展所出现的问题。 你通过在浏览器中直接运行的动手代码来练习 Browser Extensions Development (Chrome & Edge),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Browser Extensions Development (Chrome & Edge) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Browser Extensions Development (Chrome & Edge) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「日志记录、错误报告与诊断」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Browser Extensions Development (Chrome & Edge) 课中编写并运行代码吗?
能。每节 Browser Extensions Development (Chrome & Edge) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。