|
| 1 | +--- |
| 2 | +keywords: [beginner, motoko, canister logs, logging] |
| 3 | +--- |
| 4 | + |
| 5 | +# Canister logs |
| 6 | + |
| 7 | +[View this sample's code on GitHub](https://github.com/dfinity/examples/tree/master/motoko/canister_logs) |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | +This example requires an installation of: |
| 11 | + |
| 12 | +- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/). |
| 13 | +- [x] Download the following project files from GitHub: `git clone https://github.com/dfinity/examples/` |
| 14 | + |
| 15 | +You will need to have 3 terminal windows: |
| 16 | +- Terminal A: Running a DFX instance and separating its output from anything else |
| 17 | +- Terminal B: Deploying a canister and seeing its output |
| 18 | +- Terminal C: Reading logs interactively |
| 19 | + |
| 20 | +### Step 1: Navigate into the folder containing the project's files and start a local instance of the replica with the command: |
| 21 | + |
| 22 | +```shell |
| 23 | +# Terminal A -- for running DFX and separating its output from anything else. |
| 24 | +$ cd examples/motoko/canister_logs |
| 25 | +$ dfx start --clean |
| 26 | + |
| 27 | +# Terminal B -- for deploying the canister and calling its methods. |
| 28 | +$ cd examples/motoko/canister_logs |
| 29 | + |
| 30 | +# Terminal C -- for polling logs. |
| 31 | +$ cd examples/motoko/canister_logs |
| 32 | +``` |
| 33 | + |
| 34 | +### Step 2: Deploy the canister: |
| 35 | + |
| 36 | +```shell |
| 37 | +# Terminal B |
| 38 | +$ dfx deploy |
| 39 | +``` |
| 40 | + |
| 41 | +### Step 3: Check canister logs: |
| 42 | + |
| 43 | +Expect to see logs from timer traps. |
| 44 | + |
| 45 | +```shell |
| 46 | +# Terminal B |
| 47 | +$ dfx canister logs CanisterLogs |
| 48 | +[0. 2024-05-23T08:32:26.203980235Z]: right before timer trap |
| 49 | +[1. 2024-05-23T08:32:26.203980235Z]: [TRAP]: timer trap |
| 50 | +[2. 2024-05-23T08:32:31.836721763Z]: right before timer trap |
| 51 | +[3. 2024-05-23T08:32:31.836721763Z]: [TRAP]: timer trap |
| 52 | +``` |
| 53 | + |
| 54 | +### Step 4: Call `print` method and check the logs: |
| 55 | + |
| 56 | +```shell |
| 57 | +# Terminal B |
| 58 | +$ dfx canister call CanisterLogs print hi |
| 59 | +() |
| 60 | + |
| 61 | +# Expect to see new log entry. |
| 62 | +$ dfx canister logs CanisterLogs |
| 63 | +... |
| 64 | +[8. 2024-05-23T08:32:46.598972616Z]: right before timer trap |
| 65 | +[9. 2024-05-23T08:32:46.598972616Z]: [TRAP]: timer trap |
| 66 | +[10. 2024-05-23T08:32:48.713755238Z]: hi |
| 67 | +[11. 2024-05-23T08:32:51.623988313Z]: right before timer trap |
| 68 | +[12. 2024-05-23T08:32:51.623988313Z]: [TRAP]: timer trap |
| 69 | +... |
| 70 | +``` |
| 71 | + |
| 72 | +### Step 5: Start constantly polling logs: |
| 73 | + |
| 74 | +In order not to call `dfx canister logs CanisterLogs` after every canister call in a separate terminal window/pane C start a script that will constantly poll logs: |
| 75 | + |
| 76 | +```shell |
| 77 | +# Terminal C |
| 78 | +$ ./poll_logs.sh |
| 79 | +... |
| 80 | +[8. 2024-05-23T08:32:46.598972616Z]: right before timer trap |
| 81 | +[9. 2024-05-23T08:32:46.598972616Z]: [TRAP]: timer trap |
| 82 | +[10. 2024-05-23T08:32:48.713755238Z]: hi |
| 83 | +[11. 2024-05-23T08:32:51.623988313Z]: right before timer trap |
| 84 | +[12. 2024-05-23T08:32:51.623988313Z]: [TRAP]: timer trap |
| 85 | +... |
| 86 | +``` |
| 87 | + |
| 88 | +### Step 6: Call `print`, `trap` and other canister methods: |
| 89 | + |
| 90 | +```shell |
| 91 | +# Terminal B |
| 92 | +$ dfx canister call CanisterLogs print hi! |
| 93 | +() |
| 94 | + |
| 95 | +$ dfx canister call CanisterLogs print hello! |
| 96 | +() |
| 97 | + |
| 98 | +$ dfx canister call CanisterLogs print yey! |
| 99 | +() |
| 100 | + |
| 101 | +$ dfx canister call CanisterLogs trap oops! |
| 102 | +Error: Failed update call. |
| 103 | +Caused by: Failed update call. |
| 104 | + The replica returned a rejection error: reject code CanisterError, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai trapped explicitly: oops!, error code None |
| 105 | + |
| 106 | +$ dfx canister call CanisterLogs memory_oob |
| 107 | +Error: Failed update call. |
| 108 | +Caused by: Failed update call. |
| 109 | + The replica returned a rejection error: reject code CanisterError, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai trapped explicitly: StableMemory range out of bounds, error code None |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +Observe recorded logs that might look similar to this: |
| 114 | + |
| 115 | +```shell |
| 116 | +# Terminal C |
| 117 | +... |
| 118 | +[19. 2024-05-23T08:33:11.319493785Z]: right before timer trap |
| 119 | +[20. 2024-05-23T08:33:11.319493785Z]: [TRAP]: timer trap |
| 120 | +[21. 2024-05-23T08:33:14.229855179Z]: hi! |
| 121 | +[22. 2024-05-23T08:33:16.413512126Z]: right before timer trap |
| 122 | +[23. 2024-05-23T08:33:16.413512126Z]: [TRAP]: timer trap |
| 123 | +[24. 2024-05-23T08:33:18.622686552Z]: hello! |
| 124 | +[25. 2024-05-23T08:33:21.519088681Z]: right before timer trap |
| 125 | +[26. 2024-05-23T08:33:21.519088681Z]: [TRAP]: timer trap |
| 126 | +[27. 2024-05-23T08:33:22.96101893Z]: yey! |
| 127 | +[28. 2024-05-23T08:33:26.601860526Z]: right before timer trap |
| 128 | +[29. 2024-05-23T08:33:26.601860526Z]: [TRAP]: timer trap |
| 129 | +[30. 2024-05-23T08:33:28.039227914Z]: right before trap |
| 130 | +[31. 2024-05-23T08:33:28.039227914Z]: [TRAP]: oops! |
| 131 | +[32. 2024-05-23T08:33:31.634215234Z]: right before timer trap |
| 132 | +[33. 2024-05-23T08:33:31.634215234Z]: [TRAP]: timer trap |
| 133 | +[34. 2024-05-23T08:33:35.96761902Z]: right before memory out of bounds |
| 134 | +[35. 2024-05-23T08:33:35.96761902Z]: [TRAP]: StableMemory range out of bounds |
| 135 | +[36. 2024-05-23T08:33:36.712223153Z]: right before timer trap |
| 136 | +[37. 2024-05-23T08:33:36.712223153Z]: [TRAP]: timer trap |
| 137 | +... |
| 138 | + |
| 139 | +``` |
0 commit comments