Skip to content

feat: add fetchCanisterLogs method to PocketIc - #235

Open
yasumorishima wants to merge 2 commits into
dfinity:mainfrom
yasumorishima:feat/fetch-canister-logs
Open

feat: add fetchCanisterLogs method to PocketIc#235
yasumorishima wants to merge 2 commits into
dfinity:mainfrom
yasumorishima:feat/fetch-canister-logs

Conversation

@yasumorishima

Copy link
Copy Markdown

Summary

Adds a fetchCanisterLogs() method to the PocketIc class, implementing the fetch_canister_logs management canister query call.

This allows users to programmatically retrieve canister log records, which is especially useful for debugging traps in timers and heartbeats that are otherwise invisible.

Closes #68

Changes

  • management-canister.ts: Add Candid IDL types, TypeScript interfaces, and encode/decode functions for fetch_canister_logs
  • pocket-ic-types.ts: Add FetchCanisterLogsOptions and CanisterLogRecord public types
  • pocket-ic.ts: Add fetchCanisterLogs() method that makes a query call to the management canister with effectivePrincipal set to the target canister
  • main.mo: Add print_log method to test canister for testing
  • fetchCanisterLogs.spec.ts: Add 2 test cases (fetch logs after Debug.print + empty logs for fresh canister)

Implementation notes

  • Follows the same pattern as the Rust library implementation
  • Uses effectivePrincipal: { canisterId } as required by the IC spec for management canister queries
  • The caller must be a controller of the target canister (anonymous principal is rejected)
  • Maps timestamp_nanos to timestampNanos in the public type to follow the project's camelCase convention

Test plan

  • pnpm run build passes
  • pnpm run test:pic — all 13 tests pass (11 existing + 2 new)
  • pnpm run format:check passes
  • e2e tests pass (Node.js + Bun, ubuntu + macos)

@yasumorishima
yasumorishima requested a review from a team as a code owner February 13, 2026 14:23
Copilot AI review requested due to automatic review settings July 29, 2026 03:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class support in the PocketIc client for retrieving canister log records via the management canister fetch_canister_logs query, enabling programmatic access to logs useful for debugging timers/heartbeats.

Changes:

  • Added Candid IDL encode/decode and TypeScript types for the management canister fetch_canister_logs API.
  • Introduced public FetchCanisterLogsOptions and CanisterLogRecord types and a new PocketIc.fetchCanisterLogs() method using effectivePrincipal: { canisterId }.
  • Added a test-canister print_log method and new Jest tests validating log retrieval and the empty-logs case.

Reviewed changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/pic/src/management-canister.ts Adds IDL record types and encode/decode helpers for fetch_canister_logs.
packages/pic/src/pocket-ic-types.ts Exposes new public option/record types for the feature.
packages/pic/src/pocket-ic.ts Implements PocketIc.fetchCanisterLogs() and maps response fields to public types.
packages/pic/tests/src/fetchCanisterLogs.spec.ts Adds coverage for fetching logs and handling empty logs.
packages/pic/tests/test-canister/main.mo Adds print_log method to emit log output from the test canister.
packages/pic/tests/test-canister/test_canister.did Updates DID to include the new print_log method.
packages/pic/tests/test-canister/declarations/test_canister.did.js Regenerates JS DID bindings including print_log.
packages/pic/tests/test-canister/declarations/test_canister.did.d.ts Regenerates TS DID typings including print_log.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +874 to +877
public async fetchCanisterLogs({
canisterId,
sender = Principal.anonymous(),
}: FetchCanisterLogsOptions): Promise<CanisterLogRecordPublic[]> {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error messages from trap in timers not showing

2 participants