Logger for VS Code extension

I implement VS Code extensions. How I can write to log file from my extensions? How I can configure a log level for a extension? How I can see the log? I hope you have some best practices.

[original thread by ira-gordin-sap]


I’m not sure if this applies to VS Code extensions, but there is a writeup on a logging extension here: https://www.theia-ide.org/doc/Json_Rpc.html

@florentbenoit maybe you can help or you know who to ping

There is no VS Code log API most of extensions are using custom output channels to have a log available very easily for end-users.
If you still want to log to a file, you may use any nodejs log library inside your extension. If you think it would be better if Theia could offer a log API then you need to file an enhancement for having theia namespace with logging facilities.

Right now anything logged with console already goes to Logger implementation.

You can provide your own and write output where you want. Logs from the plugin host have the same prefix always, something like [plugin-host

But it really boils down to who is a user for such logging. If it supposed to be an end user, then you should use output channels as Florent mentioned and preferences to control log level for them. If it is just for developers then all console logs already end up in backend logs.


This is exactly what I’m implementing now. The possibility to write messages to OutpuChannel. The messages which should be shown to end-users. I can’t write there debug/trace messages. I am searching for some way to have debug/trace messages in order to see easily what is the end-user issue in case he will have one.

@ira-gordin-sap, for your information, there is a PR for delegating the log messages to the output view. Note, this is for electron only. As we wanted to use ConnectionContainer and connection-scoped DI, I started a spin-off of the PR here: https://github.com/theia-ide/theia/compare/GH-4297?expand=1. It’s very much work in progress.


How I add preferences to my VS Code extension?


Where I can see it? How I can add timestamp to it?

All logs are redirected here at the end: https://github.com/theia-ide/theia/blob/3587c237cacc638dc54ec19dc3eaa755c2e69fb5/packages/core/src/node/console-logger-server.ts#L56 You can provide your custom implementation to intercept some logs and add timestamp or whatever you want.

You can see it the terminal of your backend by default. But you can also redirect logging to your infrastructure instead of console.

How can one customize the actual logging by the way? https://github.com/theia-ide/theia/blob/ccaa5d84c5718ce66e887b7bed620366fc859c11/packages/core/src/common/logger-protocol.ts#L109-L113

In Gitpod it is done in cusomt console-logger-server to wrap a message into JSON structure which can be handled by Google Cloud.

What do you want to customitze there?

Add the timestamp, for example. That was the original question from @ira-gordin-sap

then console-logger-server is better all logs from frontend and backend end up there, you can add timestamp ther

The console logger server calls the code I have referenced. I would like to know if it is possible to customize it or not. I was unable to add just the timestamp for example. Thanks!