Skip to content

Commit

Permalink
feat(live-dmk): add LedgerLiveLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
fAnselmi-Ledger committed Feb 25, 2025
1 parent 3129e4c commit 54b36e2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-poems-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-dmk": minor
---

Add LL logger in live-dmk
5 changes: 3 additions & 2 deletions libs/live-dmk/src/hooks/useDeviceManagementKit.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { createContext, useContext } from "react";
import {
DeviceManagementKitBuilder,
ConsoleLogger,
DeviceManagementKit,
LogLevel,
} from "@ledgerhq/device-management-kit";
import { webHidTransportFactory } from "@ledgerhq/device-transport-kit-web-hid";
import { LedgerLiveLogger } from "../services/LedgerLiveLogger";

// TODO lower the level of the logger once useFeature("ldmkTransport") is removed and new transport goes to production
export const deviceManagementKit = new DeviceManagementKitBuilder()
.addTransport(webHidTransportFactory)
.addLogger(new ConsoleLogger(LogLevel.Debug))
.addLogger(new LedgerLiveLogger(LogLevel.Debug))
.build();

export const DeviceManagementKitContext = createContext<DeviceManagementKit>(deviceManagementKit);
Expand Down
55 changes: 55 additions & 0 deletions libs/live-dmk/src/services/LedgerLiveLogger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { log } from "@ledgerhq/logs";
import { LedgerLiveLogger } from "./LedgerLiveLogger";
import { LogLevel } from "@ledgerhq/device-management-kit";

vi.mock("@ledgerhq/logs", () => ({
log: vi.fn(),
}));

describe("LedgerLiveLogger", () => {
const options = { tag: "test", timestamp: Date.now(), data: { key: "value" } };

beforeEach(() => {
vi.clearAllMocks();
});

it("logs a message when the log level is less than or equal to maxLevel", () => {
//given
const logger = new LedgerLiveLogger(LogLevel.Debug);

//when
logger.log(LogLevel.Debug, "debug message", options);

//then
expect(log).toHaveBeenCalledWith("live-dmk-logger", "debug message", {
level: LogLevel.Debug,
...options,
});
});

it("does not log a message when the log level is above maxLevel", () => {
//given
const logger = new LedgerLiveLogger(LogLevel.Info);

//when
logger.log(LogLevel.Debug, "debug message", options);

//then
expect(log).not.toHaveBeenCalled();
});

it("logs a message when the level is null", () => {
//given
const logger = new LedgerLiveLogger(LogLevel.Error);

//when
logger.log(null, "null level message", options);

//then
expect(log).toHaveBeenCalledWith("live-dmk-logger", "null level message", {
level: null,
...options,
});
});
});
21 changes: 21 additions & 0 deletions libs/live-dmk/src/services/LedgerLiveLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { log } from "@ledgerhq/logs";
import {
LoggerSubscriberService,
LogLevel,
LogSubscriberOptions,
} from "@ledgerhq/device-management-kit";

export class LedgerLiveLogger implements LoggerSubscriberService {
private readonly maxLevel: LogLevel;

constructor(level: LogLevel = LogLevel.Debug) {
this.maxLevel = level;
}

log(level: LogLevel | null, message: string, options: LogSubscriberOptions): void {
if (level !== null && level > this.maxLevel) {
return;
}
log("live-dmk-logger", message, { level, ...options });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { deviceIdMap } from "../config/deviceIdMap";
import { activeDeviceSessionSubject } from "../config/activeDeviceSession";
import { deviceManagementKit } from "../hooks/useDeviceManagementKit";

const tracer = new LocalTracer("live-dmk", { function: "DeviceManagementKitTransport" });
const tracer = new LocalTracer("live-dmk-tracer", { function: "DeviceManagementKitTransport" });

export class DeviceManagementKitTransport extends Transport {
sessionId: string;
Expand Down
3 changes: 1 addition & 2 deletions libs/live-dmk/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineConfig } from "vitest/config";
import reactPlugin from "@vitejs/plugin-react";

export default defineConfig({
// @ts-expect-error typescript is being craycray
Expand All @@ -13,7 +12,7 @@ export default defineConfig({
printConsoleTrace: true,
coverage: {
enabled: true,
provider: "v8",
provider: "istanbul",
reporter: ["text", "html"],
include: ["src/**/*.ts", "src/**/*.tsx"],
exclude: ["node_modules", "lib-es", "src/hooks/index.ts", "src/index.ts", "src/**/*.test.*"],
Expand Down

0 comments on commit 54b36e2

Please # to comment.