Skip to content

Commit

Permalink
Refactor logger appId usage and stack-trace package usage
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Dec 16, 2023
1 parent 6f9da6f commit 526412e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions deno-runtime/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"astring": "npm:[email protected]",
"jsonrpc-lite": "npm:[email protected]",
"uuid": "npm:[email protected]",
"stack-trace": "npm:[email protected]",
}
}
1 change: 1 addition & 0 deletions deno-runtime/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 22 additions & 14 deletions deno-runtime/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import * as stackTrace from 'npm:stack-trace'
import { StackFrame } from 'npm:stack-trace'
import stackTrace from 'stack-trace';
import { AppObjectRegistry } from '../AppObjectRegistry.ts';

export interface StackFrame {
getTypeName(): string;
getFunctionName(): string;
getMethodName(): string;
getFileName(): string;
getLineNumber(): number;
getColumnNumber(): number;
isNative(): boolean;
isConstructor(): boolean;
}

enum LogMessageSeverity {
DEBUG = 'debug',
Expand All @@ -16,7 +27,7 @@ type Entry = {
method: string;
timestamp: Date;
args: Array<unknown>;
}
};

interface ILoggerStorageEntry {
appId: string;
Expand All @@ -29,40 +40,38 @@ interface ILoggerStorageEntry {
}

export class Logger {
private appId: string;
private entries: Array<Entry>;
private start: Date;
private method: string;

constructor(method: string, appId: string) {
this.appId = appId;
constructor(method: string) {
this.method = method;
this.entries = [];
this.start = new Date();
}

public debug(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.DEBUG, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.DEBUG, this.getStack(stackTrace.get()), ...args);
}

public info(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.INFORMATION, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.INFORMATION, this.getStack(stackTrace.get()), ...args);
}

public log(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.LOG, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.LOG, this.getStack(stackTrace.get()), ...args);
}

public warning(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.WARNING, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.WARNING, this.getStack(stackTrace.get()), ...args);
}

public error(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.ERROR, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.ERROR, this.getStack(stackTrace.get()), ...args);
}

public success(...args: Array<unknown>): void {
this.addEntry(LogMessageSeverity.SUCCESS, this.getStack(stackTrace.get()), ...args)
this.addEntry(LogMessageSeverity.SUCCESS, this.getStack(stackTrace.get()), ...args);
}

private addEntry(severity: LogMessageSeverity, caller: string, ...items: Array<unknown>): void {
Expand All @@ -78,7 +87,6 @@ export class Logger {
}
const str = JSON.stringify(args, null, 2);
return str ? JSON.parse(str) : str; // force call toJSON to prevent circular references

});

this.entries.push({
Expand Down Expand Up @@ -122,7 +130,7 @@ export class Logger {

public getLogs(): ILoggerStorageEntry {
return {
appId: this.appId,
appId: AppObjectRegistry.get('id')!,
method: this.method,
entries: this.entries,
startTime: this.start,
Expand Down
3 changes: 2 additions & 1 deletion deno-runtime/lib/tests/messenger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { Logger } from '../logger.ts';
describe('Messenger', () => {
beforeEach(() => {
AppObjectRegistry.clear();
AppObjectRegistry.set('logger', new Logger('test', 'test'));
AppObjectRegistry.set('logger', new Logger('test'));
AppObjectRegistry.set('id', 'test');
Messenger.Transport.selectTransport('noop');
});

Expand Down
12 changes: 5 additions & 7 deletions deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function wrapAppCode(code: string): (require: (module: string) => unknown) => Pr
}

async function handlInitializeApp(appPackage: IParseAppPackageResult): Promise<void> {
AppObjectRegistry.set('id', appPackage.info.id);
const source = sanitizeDeprecatedUsage(appPackage.files[appPackage.info.classFile]);

const require = buildRequire();
Expand Down Expand Up @@ -93,7 +94,6 @@ async function handlInitializeApp(appPackage: IParseAppPackageResult): Promise<v
}

AppObjectRegistry.set('app', app);
AppObjectRegistry.set('id', appPackage.info.id);
}

async function handleRequest({ type, payload }: Messenger.JsonRpcRequest): Promise<void> {
Expand All @@ -104,13 +104,11 @@ async function handleRequest({ type, payload }: Messenger.JsonRpcRequest): Promi

const { id, method, params } = payload;

const appId: string = method === 'construct' ? (params as Array<string>)[0] : AppObjectRegistry.get('id') as string;

const logger = new Logger(method, appId);
const logger = new Logger(method);
AppObjectRegistry.set('logger', logger);

switch (method) {
case 'app:construct': {
switch (true) {
case method.includes('app:construct'): {
const [appPackage] = params as [IParseAppPackageResult];

if (!appPackage?.info?.id || !appPackage?.info?.classFile || !appPackage?.files) {
Expand Down Expand Up @@ -162,7 +160,7 @@ async function main() {
JSONRPCMessage = Messenger.parseMessage(message);
} catch (error) {
if (Messenger.isErrorResponse(error)) {
await Messenger.send(error);
await Messenger.Transport.send(error);
} else {
await Messenger.sendParseError();
}
Expand Down

0 comments on commit 526412e

Please sign in to comment.