Skip to content

Commit 4f30922

Browse files
rberrellezaclaude
andcommitted
Migrate to VS Code LogOutputChannel for structured logging
Replace all console.log/error calls with VS Code's LogOutputChannel API for better debugging and user experience. ## Changes **New Files:** - src/logger.ts: Centralized logger module with initializeLogger() and getLogger() functions **Modified Files:** - src/extension.ts: Initialize logger, replace 10 console.* calls - src/okteto.ts: Replace 28 console.* calls with appropriate levels - src/download.ts: Replace 3 console.* calls - src/manifest.ts: Replace 1 console.error call - src/machineid.ts: Replace 1 console.log call - src/telemetry.ts: Replace 2 console.error calls - src/test/mock/vscode.ts: Add createOutputChannel mock ## Benefits - Users can now access logs via Output panel > "Okteto" - Structured logging with severity levels (info, debug, error) - Better debugging for CLI issues, manifest problems, SSH failures - Logs are properly stored and rotated by VS Code ## Log Level Mapping - info: User-facing operations (okteto up, deploy, down completed) - debug: Internal diagnostics (user selections, config info) - error: Critical errors (download failures, CLI errors) ## Verification ✅ 52 unit tests passing ✅ 10 E2E tests passing ✅ VSIX builds successfully (1.59 MB) ✅ ESLint shows expected 11 warnings (no new issues) ✅ All console.* calls replaced in source files (43 instances) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5cf6030 commit 4f30922

File tree

8 files changed

+87
-47
lines changed

8 files changed

+87
-47
lines changed

src/download.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import got from'got';
66
import { pipeline } from 'stream/promises';
77
import path from 'path';
88
import * as vscode from 'vscode';
9+
import { getLogger } from './logger';
910

1011
export const minimum = '3.16.0';
1112

@@ -62,15 +63,15 @@ export async function binary(source: string, destination: string, progress: vsco
6263
progress.report({increment: reportedProgress, message: ''});
6364
})
6465
.on("error", (error) => {
65-
console.error(`Download failed: ${error.message}`);
66+
getLogger().error(`Download failed: ${error.message}`);
6667
});
67-
68+
6869
fileWriterStream
6970
.on("error", (error) => {
70-
console.error(`Could not write file to system: ${error.message}`);
71+
getLogger().error(`Could not write file to system: ${error.message}`);
7172
})
7273
.on("finish", () => {
73-
console.log(`File downloaded to ${destination}`);
74+
getLogger().info(`File downloaded to ${destination}`);
7475
});
7576

7677
await pipeline(downloadStream, fileWriterStream);

src/extension.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {sortFilePaths} from './paths';
88
import * as okteto from './okteto';
99
import {Reporter, events} from './telemetry';
1010
import { minimum } from './download';
11+
import { initializeLogger, getLogger } from './logger';
1112

1213
const activeManifest = new Map<string, vscode.Uri>();
1314
let reporter: Reporter;
@@ -41,7 +42,10 @@ function getExtensionVersion() : string {
4142
export function activate(context: vscode.ExtensionContext) {
4243
const version = getExtensionVersion();
4344

44-
console.log(`okteto.remote-kubernetes ${version} activated`);
45+
const logger = initializeLogger();
46+
context.subscriptions.push(logger);
47+
48+
logger.info(`okteto.remote-kubernetes ${version} activated`);
4549

4650
const ctx = okteto.getContext();
4751
const machineId = okteto.getMachineId();
@@ -93,16 +97,16 @@ async function installCmd(upgrade: boolean, handleErr: boolean) {
9397
success = `Okteto was successfully upgraded`;
9498
}
9599

96-
console.log('installing okteto');
100+
getLogger().info('installing okteto');
97101
reporter.track(events.install);
98102
await vscode.window.withProgress(
99103
{location: vscode.ProgressLocation.Notification, title: title},
100104
async (progress) => {
101105
try {
102-
106+
103107
await okteto.install(progress);
104108
} catch(err: unknown) {
105-
console.error(err)
109+
getLogger().error(`${getErrorMessage(err)}`)
106110
reporter.track(events.oktetoInstallFailed);
107111
reporter.captureError(`okteto install failed: ${getErrorMessage(err)}`, err);
108112
if (handleErr) {
@@ -136,7 +140,7 @@ async function upCmd() {
136140

137141
reporter.track(events.manifestSelected);
138142
const manifestPath = manifestUri;
139-
console.log(`user selected: ${manifestPath.fsPath}`);
143+
getLogger().debug(`user selected: ${manifestPath.fsPath}`);
140144

141145
let m: manifest.Manifest;
142146

@@ -201,7 +205,7 @@ async function waitForUp(namespace: string, name: string, port: number) {
201205
const result = await waitForFinalState(namespace, name, progress);
202206
if (!result.result) {
203207
reporter.track(events.oktetoUpFailed);
204-
console.error(result.message);
208+
getLogger().error(result.message);
205209
throw new Error(`Okteto: Up command failed: ${result.message}`);
206210
}
207211

@@ -231,7 +235,7 @@ async function waitForFinalState(namespace: string, name:string, progress: vscod
231235
const res = await okteto.getState(namespace, name);
232236
if (!seen.has(res.state)) {
233237
progress.report({ message: messages.get(res.state) });
234-
console.log(`okteto is ${res.state}`);
238+
getLogger().debug(`okteto is ${res.state}`);
235239
}
236240

237241
seen.set(res.state, true);
@@ -354,7 +358,7 @@ async function deployCmd() {
354358

355359
reporter.track(events.manifestSelected);
356360
const manifestPath = manifestUri.fsPath;
357-
console.log(`user selected: ${manifestPath}`);
361+
getLogger().debug(`user selected: ${manifestPath}`);
358362

359363
try {
360364
const namespace = await getNamespace();
@@ -382,7 +386,7 @@ async function testCmd() {
382386

383387
reporter.track(events.manifestSelected);
384388
const manifestPath = manifestUri;
385-
console.log(`user selected: ${manifestPath}`);
389+
getLogger().debug(`user selected: ${manifestPath}`);
386390

387391
let m: manifest.Manifest;
388392

@@ -424,7 +428,7 @@ async function destroyCmd() {
424428
}
425429

426430
reporter.track(events.manifestSelected);
427-
console.log(`user selected: ${manifestUri.fsPath}`);
431+
getLogger().debug(`user selected: ${manifestUri.fsPath}`);
428432

429433
reporter.track(events.destroy);
430434
try {
@@ -468,7 +472,7 @@ export function getDefaultLocationManifest(): vscode.Uri | undefined{
468472

469473
const p = path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, 'okteto.yml');
470474
const loc = vscode.Uri.file(p);
471-
console.log(`default location: ${loc.fsPath.toString()}`);
475+
getLogger().debug(`default location: ${loc.fsPath.toString()}`);
472476
return loc;
473477
}
474478

src/logger.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as vscode from 'vscode';
2+
3+
let outputChannel: vscode.LogOutputChannel | undefined;
4+
5+
export function initializeLogger(): vscode.LogOutputChannel {
6+
if (!outputChannel) {
7+
outputChannel = vscode.window.createOutputChannel('Okteto', { log: true });
8+
}
9+
return outputChannel;
10+
}
11+
12+
export function getLogger(): vscode.LogOutputChannel {
13+
if (!outputChannel) {
14+
// Auto-initialize if not already done (useful for tests and standalone module usage)
15+
return initializeLogger();
16+
}
17+
return outputChannel;
18+
}

src/machineid.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {execaSync} from 'execa';
44
import {createHmac} from 'crypto';
55
import * as os from 'os';
6+
import { getLogger } from './logger';
67

78
function getCommand(): string {
89
switch (os.platform()) {
@@ -43,7 +44,7 @@ export function protect(): string {
4344
const id = expose(result.stdout);
4445
return hash(id);
4546
}catch(err: unknown){
46-
console.log(`failed to generate machineid: ${err}`);
47+
getLogger().debug(`failed to generate machineid: ${err}`);
4748
return 'na';
4849
}
4950
}

src/manifest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {promises} from 'fs';
22
import * as yaml from 'yaml';
33
import * as vscode from 'vscode';
4+
import { getLogger } from './logger';
45

56
export class Service {
67
constructor(public name: string, public workdir: string, public port: number) {}
@@ -152,7 +153,7 @@ export async function get(manifestPath: vscode.Uri): Promise<Manifest> {
152153

153154
const parsed = yaml.parseDocument(data);
154155
if (parsed.errors && parsed.errors.length > 0) {
155-
console.error(`${manifestPath} is not a valid yaml file: ${parsed.errors.join(", ")}`);
156+
getLogger().error(`${manifestPath} is not a valid yaml file: ${parsed.errors.join(", ")}`);
156157
throw new Error(`${manifestPath} is not a valid yaml file`);
157158
}
158159

0 commit comments

Comments
 (0)