简体   繁体   中英

Creating a log file for a VSCode extension

What is the standard way to have logging statements in a VSCode extension write to a file ?

I feel like there is some paradigm at work that I am missing... I can find plenty of ways to make the console log more useful for debugging my extension (Channels, libraries like typescript-logging ), but I don't see any methods for creating a log file for my extension.

Is this not a common practice? If a user of my extension experiences an issue is the easiest way for them to send me diagnostics to save the console log they can see by opening the developer tools?

Thanks for any suggestions

For those trying to solve the same problem, and looking for suggestions, I ended up going with winston.js which provides types for typescript.

I defined a logger like this:

const logger: winston.Logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(
    winston.format.simple(),
    winston.format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss'
    }),
    winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
    new winston.transports.File({
        level: 'info',
        dirname: logFileDir,
        filename: logFileName
    }),
    new winston.transports.File({
        level: 'debug',
        dirname: logFileDir,
        filename: debugLogFileName
    }),
]
});
export logger;

And then am able to call logger.info("whatever") from elsewhere in my code, and have it write to the specified log files.

You can have the logs be written to the console as well, by adding a Console transport, but be careful of this bug which prevents the logs from showing up in VSCode's "Debug Console". This workaround solved that issue for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM