Skip to content

Commit ba7372d

Browse files
Redirect logs to VSCode output console (#131)
1 parent fcffbea commit ba7372d

File tree

6 files changed

+89
-37
lines changed

6 files changed

+89
-37
lines changed

packages/databricks-sdk-js/src/logging/NamedLogger.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe(__filename, () => {
3232
const jsonValue = JSON.parse(value);
3333
delete jsonValue["timestamp"];
3434
assert.deepEqual(jsonValue, {
35+
logger: "TEST",
3536
level: "debug",
3637
message: "test",
3738
somethingElse: "value",
@@ -67,6 +68,7 @@ describe(__filename, () => {
6768
const jsonValue = JSON.parse(value);
6869
delete jsonValue["timestamp"];
6970
assert.deepEqual(jsonValue, {
71+
logger: "TEST",
7072
level: "debug",
7173
message: "test",
7274
headers: "header",

packages/databricks-sdk-js/src/logging/NamedLogger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class NamedLogger {
102102
this._loggerOpts?.maxFieldLength
103103
);
104104
this._logger?.log(level, message, {
105+
logger: this.name,
105106
operationId: this.opId,
106107
operationName: this.opName,
107108
timestamp: Date.now(),

packages/databricks-vscode/src/extension.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
commands,
33
debug,
44
ExtensionContext,
5+
OutputChannel,
56
tasks,
67
window,
78
workspace,
@@ -23,11 +24,7 @@ import {BricksTaskProvider} from "./cli/BricksTasks";
2324
import {ProjectConfigFileWatcher} from "./configuration/ProjectConfigFileWatcher";
2425
import {QuickstartCommands} from "./quickstart/QuickstartCommands";
2526
import {PublicApi} from "@databricks/databricks-vscode-types";
26-
import {
27-
ExposedLoggers,
28-
NamedLogger,
29-
} from "@databricks/databricks-sdk/dist/logging";
30-
import {format, loggers, transports} from "winston";
27+
import {initLoggers} from "./logger";
3128
import {UtilsCommands} from "./utils/UtilsCommands";
3229

3330
export function activate(context: ExtensionContext): PublicApi | undefined {
@@ -46,38 +43,7 @@ export function activate(context: ExtensionContext): PublicApi | undefined {
4643
*/
4744
return undefined;
4845
}
49-
NamedLogger.getOrCreate(
50-
ExposedLoggers.SDK,
51-
{
52-
factory: (name) => {
53-
return loggers.add(name, {
54-
level: "debug",
55-
format: format.json(),
56-
transports: [new transports.Console()],
57-
});
58-
},
59-
},
60-
true
61-
);
62-
63-
/**
64-
This logger collects all the logs in the extension.
65-
66-
TODO Make this logger log to a seperate (or common?) output console in vscode
67-
*/
68-
NamedLogger.getOrCreate(
69-
"Extension",
70-
{
71-
factory: (name) => {
72-
return loggers.add(name, {
73-
level: "error",
74-
format: format.json(),
75-
transports: [new transports.Console()],
76-
});
77-
},
78-
},
79-
true
80-
);
46+
initLoggers();
8147

8248
let cli = new CliWrapper(context);
8349
// Configuration group
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import internal, {Writable} from "stream";
2+
import {StringDecoder} from "string_decoder";
3+
import {OutputChannel} from "vscode";
4+
5+
export class OutputConsoleStream extends Writable {
6+
private readonly _decoder = new StringDecoder();
7+
constructor(
8+
private readonly _outputChannel: OutputChannel,
9+
opts?: internal.WritableOptions
10+
) {
11+
super(opts);
12+
}
13+
14+
_write(
15+
chunk: any,
16+
encoding: BufferEncoding,
17+
callback: (error?: Error | null | undefined) => void
18+
): void {
19+
const decoded = Buffer.isBuffer(chunk)
20+
? this._decoder.write(chunk)
21+
: chunk;
22+
this._outputChannel.append(decoded);
23+
callback();
24+
}
25+
26+
_final(callback: (error?: Error | null | undefined) => void): void {
27+
this._outputChannel.append(this._decoder.end());
28+
callback();
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./loggers";
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
NamedLogger,
3+
ExposedLoggers,
4+
} from "@databricks/databricks-sdk/dist/logging";
5+
import {OutputChannel, window} from "vscode";
6+
import {loggers, format, transports} from "winston";
7+
import {OutputConsoleStream} from "./OutputConsoleStream";
8+
9+
function getOutputConsoleTransport(outputChannel: OutputChannel) {
10+
return new transports.Stream({
11+
stream: new OutputConsoleStream(outputChannel, {
12+
defaultEncoding: "utf-8",
13+
}),
14+
});
15+
}
16+
export function initLoggers() {
17+
const outputChannel = window.createOutputChannel("Databricks Logs", "json");
18+
outputChannel.clear();
19+
20+
NamedLogger.getOrCreate(
21+
ExposedLoggers.SDK,
22+
{
23+
factory: (name) => {
24+
return loggers.add(name, {
25+
level: "debug",
26+
format: format.json(),
27+
transports: [getOutputConsoleTransport(outputChannel)],
28+
});
29+
},
30+
},
31+
true
32+
);
33+
34+
/**
35+
This logger collects all the logs in the extension.
36+
37+
TODO Make this logger log to a seperate (or common?) output console in vscode
38+
*/
39+
NamedLogger.getOrCreate(
40+
"Extension",
41+
{
42+
factory: (name) => {
43+
return loggers.add(name, {
44+
level: "error",
45+
format: format.json(),
46+
transports: [getOutputConsoleTransport(outputChannel)],
47+
});
48+
},
49+
},
50+
true
51+
);
52+
}

0 commit comments

Comments
 (0)