Skip to content
2 changes: 2 additions & 0 deletions packages/databricks-sdk-js/src/logging/NamedLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe(__filename, () => {
const jsonValue = JSON.parse(value);
delete jsonValue["timestamp"];
assert.deepEqual(jsonValue, {
logger: "TEST",
level: "debug",
message: "test",
somethingElse: "value",
Expand Down Expand Up @@ -67,6 +68,7 @@ describe(__filename, () => {
const jsonValue = JSON.parse(value);
delete jsonValue["timestamp"];
assert.deepEqual(jsonValue, {
logger: "TEST",
level: "debug",
message: "test",
headers: "header",
Expand Down
1 change: 1 addition & 0 deletions packages/databricks-sdk-js/src/logging/NamedLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class NamedLogger {
this._loggerOpts?.maxFieldLength
);
this._logger?.log(level, message, {
logger: this.name,
operationId: this.opId,
operationName: this.opName,
timestamp: Date.now(),
Expand Down
40 changes: 3 additions & 37 deletions packages/databricks-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
commands,
debug,
ExtensionContext,
OutputChannel,
tasks,
window,
workspace,
Expand All @@ -23,11 +24,7 @@ import {BricksTaskProvider} from "./cli/BricksTasks";
import {ProjectConfigFileWatcher} from "./configuration/ProjectConfigFileWatcher";
import {QuickstartCommands} from "./quickstart/QuickstartCommands";
import {PublicApi} from "@databricks/databricks-vscode-types";
import {
ExposedLoggers,
NamedLogger,
} from "@databricks/databricks-sdk/dist/logging";
import {format, loggers, transports} from "winston";
import {initLoggers} from "./logger";
import {UtilsCommands} from "./utils/UtilsCommands";

export function activate(context: ExtensionContext): PublicApi | undefined {
Expand All @@ -46,38 +43,7 @@ export function activate(context: ExtensionContext): PublicApi | undefined {
*/
return undefined;
}
NamedLogger.getOrCreate(
ExposedLoggers.SDK,
{
factory: (name) => {
return loggers.add(name, {
level: "debug",
format: format.json(),
transports: [new transports.Console()],
});
},
},
true
);

/**
This logger collects all the logs in the extension.

TODO Make this logger log to a seperate (or common?) output console in vscode
*/
NamedLogger.getOrCreate(
"Extension",
{
factory: (name) => {
return loggers.add(name, {
level: "error",
format: format.json(),
transports: [new transports.Console()],
});
},
},
true
);
initLoggers();

let cli = new CliWrapper(context);
// Configuration group
Expand Down
30 changes: 30 additions & 0 deletions packages/databricks-vscode/src/logger/OutputConsoleStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import internal, {Writable} from "stream";
import {StringDecoder} from "string_decoder";
import {OutputChannel} from "vscode";

export class OutputConsoleStream extends Writable {
private readonly _decoder = new StringDecoder();
constructor(
private readonly _outputChannel: OutputChannel,
opts?: internal.WritableOptions
) {
super(opts);
}

_write(
chunk: any,
encoding: BufferEncoding,
callback: (error?: Error | null | undefined) => void
): void {
const decoded = Buffer.isBuffer(chunk)
? this._decoder.write(chunk)
: chunk;
this._outputChannel.append(decoded);
callback();
}

_final(callback: (error?: Error | null | undefined) => void): void {
this._outputChannel.append(this._decoder.end());
callback();
}
}
1 change: 1 addition & 0 deletions packages/databricks-vscode/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./loggers";
52 changes: 52 additions & 0 deletions packages/databricks-vscode/src/logger/loggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
NamedLogger,
ExposedLoggers,
} from "@databricks/databricks-sdk/dist/logging";
import {OutputChannel, window} from "vscode";
import {loggers, format, transports} from "winston";
import {OutputConsoleStream} from "./OutputConsoleStream";

function getOutputConsoleTransport(outputChannel: OutputChannel) {
return new transports.Stream({
stream: new OutputConsoleStream(outputChannel, {
defaultEncoding: "utf-8",
}),
});
}
export function initLoggers() {
const outputChannel = window.createOutputChannel("Databricks Logs", "json");
outputChannel.clear();

NamedLogger.getOrCreate(
ExposedLoggers.SDK,
{
factory: (name) => {
return loggers.add(name, {
level: "debug",
format: format.json(),
transports: [getOutputConsoleTransport(outputChannel)],
});
},
},
true
);

/**
This logger collects all the logs in the extension.

TODO Make this logger log to a seperate (or common?) output console in vscode
*/
NamedLogger.getOrCreate(
"Extension",
{
factory: (name) => {
return loggers.add(name, {
level: "error",
format: format.json(),
transports: [getOutputConsoleTransport(outputChannel)],
});
},
},
true
);
}