Skip to content

Commit

Permalink
feat: improve logging and tracing (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus-Keller authored Jun 29, 2023
1 parent 8af930c commit b0bce7f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/ide-extension/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ export function logString(message: string): void {
}
channel.appendLine(message);
}

/**
* Add a message to trace.
*
* @param message - trace message
*/
export function traceString(message: string): void {
console.log(message);
}
4 changes: 2 additions & 2 deletions packages/ide-extension/src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TelemetryClient } from 'applicationinsights';
import type { Contracts } from 'applicationinsights';
import type { IDE, SendTelemetry } from '@sap/guided-answers-extension-types';
import { v4 as uuidv4 } from 'uuid';
import { logString } from '../logger/logger';
import { logString, traceString } from '../logger/logger';
import packageJson from '../../package.json';
import type { TelemetryEvent, TelemetryReporter } from '../types';
import { actionMap } from './action-map';
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function trackEvent(event: TelemetryEvent): Promise<void> {
const name = `${packageJson.name}/${event.name}`;
const properties = propertyValuesToString({ ...event.properties, ...(reporter.commonProperties ?? {}) });
reporter.client.trackEvent({ name, properties });
logString(`Telemetry event '${event.name}': ${JSON.stringify(properties)}`);
traceString(`Telemetry event '${event.name}': ${JSON.stringify(properties)}`);
} catch (error) {
logString(`Error sending telemetry event '${event.name}': ${(error as Error).message}`);
}
Expand Down
15 changes: 14 additions & 1 deletion packages/ide-extension/test/logger/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OutputChannel, window } from 'vscode';
import { logString } from '../../src/logger/logger';
import { logString, traceString } from '../../src/logger/logger';

describe('Tests for logging', () => {
test('Test for logString()', () => {
Expand All @@ -15,4 +15,17 @@ describe('Tests for logging', () => {
// Result check
expect(channelMock.appendLine).toBeCalledWith('LOG_MESSAGE');
});

test('Test for traceString()', () => {
// Mock setup
const originalLogFn = console.log;
console.log = jest.fn();

// Test execution
traceString('TRACE_MESSAGE');

// Result check
expect(console.log).toBeCalledWith('TRACE_MESSAGE');
console.log = originalLogFn;
});
});
2 changes: 2 additions & 0 deletions packages/ide-extension/test/telemetry/telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Telemetry trackEvent() tests', () => {
jest.clearAllMocks();
jest.spyOn(commands, 'registerCommand');
jest.spyOn(logger, 'logString').mockImplementation(() => null);
jest.spyOn(logger, 'traceString').mockImplementation(() => null);
jest.spyOn(workspace, 'getConfiguration').mockReturnValue({ get: () => true } as any);

const context = {
Expand Down Expand Up @@ -145,6 +146,7 @@ describe('Telemetry trackAction() tests', () => {
treeTitle: 'Title'
}
});
expect(logger.traceString).toBeCalledWith(expect.stringContaining('OPEN_TREE'));
});

test('send UPDATE_ACTIVE_NODE action', () => {
Expand Down

0 comments on commit b0bce7f

Please sign in to comment.