惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
小众软件
小众软件
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - 聂微东
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
云风的 BLOG
云风的 BLOG
量子位
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
博客园 - 司徒正美
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享

Pierce Freeman

A browser for agents | Pierce Freeman The grey market of podcast appearances The way I travel | Pierce Freeman Fixing slow AWS uploads | Pierce Freeman Local tools should still use vaults We solved scratch content first Starting a podcast in 2025 Being late but still being early Automating our home video imports Adding my parents to tailscale A deep dive on agent sandboxes Language servers for AI | Pierce Freeman My simple home podcast studio We need centralized infrastructure | Pierce Freeman Coercing agents to follow conventions using AST validation My unified theory of social selling My personal backup strategy | Pierce Freeman July updates to the homelab How the KV Cache works httpx is the right way to do web requests in Python Reputation is becoming everything | Pierce Freeman Building a (kind of) invisible mac app Updated knowledge in language models Making an ascii animation | Pierce Freeman How speculative decoding works | Pierce Freeman Under the hood of Claude Code Doing things because they're easy, not hard Speeding up sideeffects with JIT in mountaineer Firehot for hot reloading in Python Misadventures in Python hot reloading
Debugging chrome extensions with system-level logging
2023-12-19 · via Pierce Freeman

I've been working on a Chrome extension lately that's getting closer to a public release. That shifts my workflow from blue sky design to the basement - fighting every last bug.

Extensions are basically mini web applications these days, just with access to a chrome global variable that can interact with some browser-level functionality. Aside from that - it's all familiar. That extends to the debugging experience. Since extensions run in the regular V8 Chrome runtime, Chrome exposes the same debugging tools that you're used to on the web: profiling, stack tracing, code mapping, etc.

Unlike a regular website, however, the potential edge cases of an extension are practically infinite. They need to tolerate the whole universe of pages where you're applying them. I've found one of the best ways to catch these edge cases and diagnose them after the fact is to capture verbose logging to disk. You can browse the web as you test your extension and then review the workflow session logs afterward. The Inspector console of the background process is fleeting and often crashes if your logging volume is too high.

Enabling Verbosity

Per the Chromium docs, you can add verbosity on application startup. This logic works on Chrome as well:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-logging --v=1

This logging file will be accessible during your run and after. It gets cleared the next time you launch Chrome with this logging command, however, so make sure to copy over what you need to a new file. You can inspect it (or pipe it) in realtime by running tail:

tail -f ~/Library/Application\ Support/Google/Chrome/chrome_debug.log

This should produce extension logs whether you're actively inspecting your service worker or just have it running in the background. From there you can open it in a log utility, duckdb, or just a simple grep.

grep "extension-error" ./chrome_debug.log

Logging Utility

This strategy is most useful if you log aggressively. If your logs aren't precise enough to pin down the bug, this approach loses much of the merit. I suggest a tunable logging class where your development builds will log every page URL, primary function call, expected value, etc. Your build pipeline can then clear these out before production.

Internally, we have a logging class a lot like this. It extends the default javascript console with a bit of syntax sugar to let flags determine whether messages actually make it to the console or are dropped.

The process.env variable default can be set by your build pipeline so you increase verbosity during development and scale it back for production. es-build makes this pretty easy.

export enum LOG_LEVELS {
    NONE = 0,
    ERROR = 1,
    WARN = 2,
    INFO = 3,
    DEBUG = 4,
}

const logToLevel = {
    NONE: LOG_LEVELS.NONE,
    ERROR: LOG_LEVELS.ERROR,
    WARN: LOG_LEVELS.WARN,
    INFO: LOG_LEVELS.INFO,
    DEBUG: LOG_LEVELS.DEBUG,
};

interface CustomConsole extends Console {
    logLevel?: string;
}

class CustomLogger {
    systemLogLevel: LOG_LEVELS;

    constructor() {
        const envDefault = process.env.PUBLIC_ENV_LOG_LEVEL! || 'INFO';
        let enumValue = logToLevel[envDefault as keyof typeof LOG_LEVELS];
        if (enumValue === undefined) {
            console.log(`Invalid log level ${envDefault}, defaulting to INFO`);
            enumValue = LOG_LEVELS.INFO;
        }

        this.systemLogLevel = enumValue;
    }

    setLogLevel(level: LOG_LEVELS) {
        this.systemLogLevel = level;
    }

    error(...args: any[]) {
        if (this.systemLogLevel >= LOG_LEVELS.ERROR) {
            console.error(...args);
        }
    }

    warn(...args: any[]) {
        if (this.systemLogLevel >= LOG_LEVELS.WARN) {
            console.warn(...args);
        }
    }

    info(...args: any[]) {
        if (this.systemLogLevel >= LOG_LEVELS.INFO) {
            console.info(...args);
        }
    }

    debug(...args: any[]) {
        if (this.systemLogLevel >= LOG_LEVELS.DEBUG) {
            console.log(...args);
        }
    }
}
export const defaultLogger = new CustomLogger();

Additional Notes

  • Make sure to format any important console.log values as JSON strings instead of the raw objects themselves. When Chrome saves the logs to disk it will format everything as strings which means that your rich objects will be serialized to [object Object], which doesn't make for the easiest debugging.
  • A heavy day of Chrome use will result in a log file of around 2GB. Maybe more maybe less depending on your logging verbosity. I periodically copy this over to another scratch location on disk so I don't lose any intermediate logs, but I haven't had an issue with data loss yet from the core logging file.