-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (sample): Possibility to export all logs to JSON
- Loading branch information
1 parent
2249dd7
commit ea615d7
Showing
6 changed files
with
188 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@ledgerhq/device-management-kit": patch | ||
"@ledgerhq/device-sdk-sample": patch | ||
--- | ||
|
||
Add possibility to export logs to JSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/core/src/api/logger-subscriber/service/WebLogsExporterLogger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ConnectionType } from "@api/discovery/ConnectionType"; | ||
import { deviceModelStubBuilder } from "@internal/device-model/model/DeviceModel.stub"; | ||
import { DeviceSession } from "@internal/device-session/model/DeviceSession"; | ||
import { ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; | ||
|
||
import { getJSONStringifyReplacer } from "./WebLogsExporterLogger"; | ||
|
||
describe("getJSONStringifyReplacer", () => { | ||
it("should return a function that replaces Uint8Array correctly", () => { | ||
const replacer = getJSONStringifyReplacer(); | ||
const value = new Uint8Array([1, 2, 3]); | ||
const result = replacer("key", value); | ||
expect(result).toEqual({ | ||
hex: "0x010203", | ||
readableHex: "01 02 03", | ||
value: "1,2,3", | ||
}); | ||
}); | ||
|
||
it("should return a function that replaces DeviceSession", () => { | ||
const stubDeviceModel = deviceModelStubBuilder(); | ||
const replacer = getJSONStringifyReplacer(); | ||
|
||
const connectedDevice = { | ||
deviceModel: deviceModelStubBuilder(), | ||
type: "USB" as ConnectionType, | ||
id: "mockedDeviceId", | ||
sendApdu: jest.fn(), | ||
}; | ||
|
||
const value = new DeviceSession( | ||
{ | ||
connectedDevice, | ||
id: "mockedSessionId", | ||
}, | ||
jest.fn(), | ||
{} as ManagerApiService, | ||
); | ||
const result = JSON.stringify(value, replacer); | ||
const expected = `{"id":"mockedSessionId","connectedDevice":{"deviceModel":${JSON.stringify( | ||
stubDeviceModel, | ||
)},"type":"USB","id":"mockedDeviceId"}}`; | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it("should return a function that replaces circular references", () => { | ||
interface CircularObject { | ||
name: string; | ||
self?: CircularObject; | ||
} | ||
|
||
const obj: CircularObject = { name: "Alice" }; | ||
obj.self = obj; | ||
|
||
const expected = '{"name":"Alice","self":"[Circular]"}'; | ||
const result = JSON.stringify(obj, getJSONStringifyReplacer()); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); |
100 changes: 100 additions & 0 deletions
100
packages/core/src/api/logger-subscriber/service/WebLogsExporterLogger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { LogLevel } from "@api/logger-subscriber/model/LogLevel"; | ||
import { LogSubscriberOptions } from "@api/types"; | ||
import { DeviceSession } from "@internal/device-session/model/DeviceSession"; | ||
|
||
import { LoggerSubscriberService } from "./LoggerSubscriberService"; | ||
|
||
/** | ||
* This function is used to format the logs to JSON format, | ||
* remove circular dependencies and do some extra formatting. | ||
* */ | ||
export function getJSONStringifyReplacer(): ( | ||
key: string, | ||
value: unknown, | ||
) => unknown { | ||
const ancestors: unknown[] = []; | ||
return function (_: string, value: unknown): unknown { | ||
// format Uint8Array values to more readable format | ||
if (value instanceof Uint8Array) { | ||
const bytesHex = Array.from(value).map((x) => | ||
x.toString(16).padStart(2, "0"), | ||
); | ||
return { | ||
hex: "0x" + bytesHex.join(""), | ||
readableHex: bytesHex.join(" "), | ||
value: value.toString(), | ||
}; | ||
} | ||
|
||
// format DeviceSession values to avoid huge object in logs | ||
if (value instanceof DeviceSession) { | ||
const { | ||
connectedDevice: { deviceModel, type, id }, | ||
} = value; | ||
return { | ||
id: value.id, | ||
connectedDevice: { | ||
deviceModel, | ||
type, | ||
id, | ||
}, | ||
}; | ||
} | ||
|
||
// format circular references to "[Circular]" | ||
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references | ||
if (typeof value !== "object" || value === null) { | ||
return value; | ||
} | ||
// `this` is the object that value is contained in, | ||
// i.e., its direct parent. | ||
// @ts-expect-error cf. comment above | ||
while (ancestors.length > 0 && ancestors.at(-1) !== (this as unknown)) { | ||
ancestors.pop(); | ||
} | ||
if (ancestors.includes(value)) { | ||
return "[Circular]"; | ||
} | ||
ancestors.push(value); | ||
return value; | ||
}; | ||
} | ||
|
||
export class WebLogsExporterLogger implements LoggerSubscriberService { | ||
private logs: Array< | ||
[level: LogLevel, message: string, options: LogSubscriberOptions] | ||
> = []; | ||
|
||
log(level: LogLevel, message: string, options: LogSubscriberOptions): void { | ||
this.logs.push([level, message, options]); | ||
} | ||
|
||
private formatLogsToJSON(): string { | ||
const remappedLogs = this.logs.map(([level, message, options]) => { | ||
const { timestamp, ...restOptions } = options; | ||
return { | ||
level: LogLevel[level], | ||
message, | ||
options: { | ||
...restOptions, | ||
date: new Date(options.timestamp), | ||
}, | ||
}; | ||
}); | ||
|
||
return JSON.stringify(remappedLogs, getJSONStringifyReplacer(), 2); | ||
} | ||
|
||
/** | ||
* Export logs to JSON file. | ||
*/ | ||
public exportLogsToJSON(): void { | ||
const logs = this.formatLogsToJSON(); | ||
const blob = new Blob([logs], { type: "application/json" }); | ||
const url = URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.href = url; | ||
a.download = `ledger-device-management-kit-logs-${new Date().toISOString()}.json`; | ||
a.click(); | ||
} | ||
} |