Skip to content

Commit

Permalink
refine logging
Browse files Browse the repository at this point in the history
  • Loading branch information
garredow committed Apr 13, 2024
1 parent ed41ace commit 7d000b7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nothing-special/kaiware-lib",
"version": "0.2.0",
"version": "0.3.0",
"type": "module",
"author": {
"name": "Garrett Downs",
Expand Down
10 changes: 2 additions & 8 deletions src/lib/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';
import { LogLevel, MessageType } from '../enums';
import { Config, Log, Message } from '../types';
import { MessageType } from '../enums';
import { Config, Message } from '../types';
import { isJson } from '../utils';

export class Connection {
Expand All @@ -22,12 +22,6 @@ export class Connection {

this.socket.onopen = () => {
console.log('WebSocket connection established');
this.sendMessage<Omit<Log, 'id'>>(MessageType.NewLog, {
source: this.config.sourceId,
level: LogLevel.Info,
data: ['Connection established'],
timestamp: new Date().toISOString()
});
resolve();
};

Expand Down
29 changes: 6 additions & 23 deletions src/lib/kaiware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LogLevel, MessageType } from '../enums';
import { Config, Log } from '../types';
import { Config, LogMessageData } from '../types';
import { parseError } from '../utils';
import { Connection } from './connection';

export class Kaiware {
Expand Down Expand Up @@ -44,39 +45,21 @@ export class Kaiware {
const stringifiedData: string[] = data.map((a) => {
if (typeof a === 'string') {
return a;
} else if (a instanceof Error) {
return JSON.stringify(a, [
'message',
'type',
'name',
'stack',
'fileName',
'lineNumber',
'columnNumber'
]);
} else if (a instanceof ErrorEvent) {
return JSON.stringify(a, [
'message',
'type',
'name',
'stack',
'fileName',
'lineNumber',
'columnNumber'
]);
} else if (a instanceof Error || a instanceof ErrorEvent) {
return JSON.stringify(parseError(a));
} else {
return JSON.stringify(a);
}
});

const log: Omit<Log, 'id'> = {
const log: LogMessageData = {
source: this.config!.sourceId,
level: level,
data: stringifiedData,
timestamp: new Date().toISOString()
};

this.connection.sendMessage(MessageType.NewLog, log);
this.connection.sendMessage<LogMessageData>(MessageType.NewLog, log);
}

static log = {
Expand Down
2 changes: 1 addition & 1 deletion src/types/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export type Log = {
id: number;
source: string;
level: LogLevel;
data: string[];
data: unknown[];
timestamp: string;
};
8 changes: 8 additions & 0 deletions src/types/LogMessageData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LogLevel } from '../enums';

export type LogMessageData = {
source: string;
level: LogLevel;
data: string[];
timestamp: string;
};
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Config';
export * from './DeviceInfo';
export * from './Log';
export * from './LogMessageData';
export * from './Message';
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './isJson';
export * from './parseError';
4 changes: 2 additions & 2 deletions src/utils/isJson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function isJson(data: string): boolean {
export function isJson(data: unknown): boolean {
try {
JSON.parse(data);
JSON.parse(data as string);
} catch (error) {
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/parseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function parseError(err: Error | ErrorEvent): object {
return JSON.parse(
JSON.stringify(err, [
'message',
'type',
'name',
'stack',
'fileName',
'lineNumber',
'columnNumber'
])
);
}

0 comments on commit 7d000b7

Please sign in to comment.