Skip to content
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

Improve logging system using separate output channels #659

Merged
merged 8 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
env:
Comment on lines -15 to 16
Copy link
Member Author

Choose a reason for hiding this comment

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

This is because the latest Ubuntu version doesn't provide Python 3.7

UV_SYSTEM_PYTHON: 1
steps:
Expand Down
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,47 @@ itself is compatible with Python 3.7 to 3.13.
## Troubleshooting

If you encounter any issues with the extension or the language server, please refer to the
logs in the output panel in VS Code. You can access the logs by running the `Ruff: Show logs`
command.
logs in the corresponding output channel in VS Code. The extension logs are in the "Ruff"
output channel and the language server logs are in the "Ruff Language Server" output channel.

By default, the output panel will only contain logs from the extension. To enable logs from the
language server, set the `trace.server` setting to `messages` in your `settings.json`:
To open the output panel, use the `Output: Show Output Channels` command in the command palette
(`Ctrl+Shift+P` or `Cmd+Shift+P`), then select "Ruff" or "Ruff Language Server". Alternatively,
you can use the `Ruff: Show logs` command to open the "Ruff" output channel directly.

The default log level for the extension is `info` which can be changed from the output panel using
the settings icon in the top right corner of the panel.

The default log level for the language server is `info` which can be changed using the `ruff.logLevel`
setting in your `settings.json`:

```json
{
"ruff.logLevel": "info"
}
```

The language server logs can be directed to a file by setting the `ruff.logFile` setting in
your `settings.json`:

```json
{
"ruff.logFile": "/path/to/ruff.log"
}
```

To fully capture the LSP messages between the editor and the server, set the `ruff.trace.server`
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
setting to either `messages` or `verbose` in your `settings.json`:

```json
{
"ruff.trace.server": "messages"
}
```

The trace value can also be set to `verbose` for more detailed logs. If you're using the Rust-based
language server, you can use the `ruff.logLevel` setting to control the log level of the server and
`ruff.logFile` to write logs to a file instead of the output panel.
This will be visible in the "Ruff Language Server Trace" output channel. The difference between
`messages` and `verbose` is that `messages` will only log the method name for both the request
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
and response, while `verbose` will also log the request parameters sent by the client and the
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
response result sent by the server.

The extension also displays certain information in the status bar. This can be pinned to the status
bar as a permanent item.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ruff",
"displayName": "Ruff",
"description": "A Visual Studio Code extension with support for the Ruff linter.",
"description": "A Visual Studio Code extension with support for the Ruff linter and formatter.",
"version": "2024.56.0",
"serverInfo": {
"name": "Ruff",
Expand All @@ -25,6 +25,7 @@
"keywords": [
"python",
"linting",
"formatting",
"ruff"
],
"engines": {
Expand Down
72 changes: 0 additions & 72 deletions src/common/log/logging.ts

This file was deleted.

96 changes: 96 additions & 0 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as util from "util";
import * as vscode from "vscode";

class ExtensionLogger {
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
readonly channel = vscode.window.createOutputChannel("Ruff", { log: true });
private readonly isCI = process.env.CI === "true";

error(...messages: unknown[]): void {
if (this.isCI) {
console.log(...messages);
}
this.channel.error(util.format(...messages));
}

dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
warn(...messages: unknown[]): void {
if (this.isCI) {
console.log(...messages);
}
this.channel.warn(util.format(...messages));
}

info(...messages: unknown[]): void {
if (this.isCI) {
console.log(...messages);
}
this.channel.info(util.format(...messages));
}

debug(...messages: unknown[]): void {
if (this.isCI) {
console.log(...messages);
}
this.channel.debug(util.format(...messages));
}

trace(...messages: unknown[]): void {
if (this.isCI) {
console.log(...messages);
}
this.channel.trace(util.format(...messages));
}
}

export const logger = new ExtensionLogger();

export class LazyOutputChannel implements vscode.OutputChannel {
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
name: string;
_channel: vscode.OutputChannel | undefined;

constructor(name: string) {
this.name = name;
}

get channel(): vscode.OutputChannel {
if (!this._channel) {
this._channel = vscode.window.createOutputChannel(this.name);
}
return this._channel;
}

append(value: string): void {
this.channel.append(value);
}

appendLine(value: string): void {
this.channel.appendLine(value);
}

replace(value: string): void {
this.channel.replace(value);
}

clear(): void {
if (this._channel) {
this._channel.clear();
}
}

show(preserveFocus?: boolean): void;
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
show(column?: any, preserveFocus?: any): void {
this.channel.show(column, preserveFocus);
}

hide(): void {
if (this._channel) {
this._channel.hide();
}
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
}

dispose(): void {
if (this._channel) {
this._channel.dispose();
}
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
}
}
12 changes: 6 additions & 6 deletions src/common/python.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { commands, Disposable, Event, EventEmitter, Uri } from "vscode";
import { traceError, traceLog } from "./log/logging";
import { logger } from "./logger";
import { PythonExtension, ResolvedEnvironment } from "@vscode/python-extension";

export interface IInterpreterDetails {
Expand Down Expand Up @@ -31,11 +31,11 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
}),
);

traceLog("Waiting for interpreter from python extension.");
logger.info("Waiting for interpreter from python extension.");
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
}
} catch (error) {
traceError("Error initializing python: ", error);
logger.error("Error initializing python: ", error);
}
}

Expand Down Expand Up @@ -72,8 +72,8 @@ export function checkVersion(resolved: ResolvedEnvironment): boolean {
if (version?.major === 3 && version?.minor >= 7) {
return true;
}
traceError(`Python version ${version?.major}.${version?.minor} is not supported.`);
traceError(`Selected python path: ${resolved.executable.uri?.fsPath}`);
traceError("Supported versions are 3.7 and above.");
logger.error(`Python version ${version?.major}.${version?.minor} is not supported.`);
logger.error(`Selected python path: ${resolved.executable.uri?.fsPath}`);
logger.error("Supported versions are 3.7 and above.");
return false;
}
Loading