Skip to content

Use VS Code's LogOutputChannel for logging #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

EhabY
Copy link

@EhabY EhabY commented Jul 15, 2025

closes #257

In this PR I create a LogOutputChannel instead of the old OutputChannel. By default, appendLine uses the info logging level so I used info to log most messages, except errors where the error level was used.

I think all of the info can be downgraded into debug level actually, and the errors could be downgraded into warn since they are handled or thrown right away. What do you think?

@matifali matifali requested review from code-asher and aslilac and removed request for code-asher July 15, 2025 21:02
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea this existed! Awesome.

@code-asher
Copy link
Member

Ah and I mentioned info already but to this point:

the errors could be downgraded into warn since they are handled or thrown right away. What do you think?

I think you are right, looking at the error calls, warn feels appropriate to me for these

src/storage.ts Outdated

switch (resp.status) {
case 200: {
const rawContentLength = resp.headers["content-length"];
const contentLength = Number.parseInt(rawContentLength);
if (Number.isNaN(contentLength)) {
this.output.appendLine(
this.output.error(
`Got invalid or missing content length: ${rawContentLength}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`Got invalid or missing content length: ${rawContentLength}`,
"Got invalid or missing content length",
rawContentLength,

The methods on LogOutputChannel all take ...args: any[], so we can take advantage of that instead of continuing to use string interpolation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I replace all messages that have a dynamic value at the end and remove the trailing : from the message?

The only issue here is that we cannot guarantee that VS Code is going to log those in the same way in future versions. Is it simply a message + args.join(" ")?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is how VS Code formats the args, primitives are kept as is, objects are stringified and errors made into human readable errors:

function format(args: any, verbose: boolean = false): string {
	let result = '';

	for (let i = 0; i < args.length; i++) {
		let a = args[i];

		if (a instanceof Error) {
			a = toErrorMessage(a, verbose);
		}

		if (typeof a === 'object') {
			try {
				a = JSON.stringify(a);
			} catch (e) { }
		}

		result += (i > 0 ? ' ' : '') + a;
	}

	return result;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's really seems like the "right" way to do it, I'm pretty convinced they won't change it majorly

@EhabY
Copy link
Author

EhabY commented Jul 16, 2025

Probably requires another review now that I replaced all the calls of storage.writeToCoderOutputChannel

@matifali matifali requested a review from code-asher July 16, 2025 17:12
Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@matifali
Copy link
Member

HI @EhabY. Thanks for your contribution. Could you also add the change to https://github.com/coder/vscode-coder/blob/main/CHANGELOG.md
I am ssuming that the usuall way to do it @code-asher

@EhabY
Copy link
Author

EhabY commented Jul 17, 2025

Added the change to the changlog

@code-asher
Copy link
Member

Ah right yeah, adding a line to the changelog in the PR is the best way yeah.

Comment on lines +7 to +8
- Use `LogOutputChannel` for the Coder output channel and ensure messages are logged
with the appropriate log level.
Copy link
Member

@code-asher code-asher Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would word it to be more user-centric, like how does this affect the user and what can/should they do about it?

Something like this maybe:

Respect log levels.  In the Coder output panel, you can now select the log level to filter messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include timestamps in Coder extension logs
4 participants