Skip to content

Commit 85cd3a6

Browse files
authored
Merge pull request #871 from maksymar/maksym/ic-272-logs
add `canister_logs` examples for rust and motoko
2 parents 1281143 + 79c9671 commit 85cd3a6

File tree

13 files changed

+1146
-0
lines changed

13 files changed

+1146
-0
lines changed

motoko/canister_logs/README.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
```

motoko/canister_logs/dfx.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"canisters": {
4+
"CanisterLogs": {
5+
"type": "motoko",
6+
"main": "src/Main.mo"
7+
}
8+
},
9+
"defaults": {
10+
"build": {
11+
"packtool": ""
12+
}
13+
}
14+
}

motoko/canister_logs/poll_logs.sh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Function to fetch logs and filter out new lines
4+
fetch_and_filter_logs() {
5+
# Fetch logs
6+
new_logs=$(dfx canister logs CanisterLogs)
7+
8+
# Compare with previous logs to find new ones
9+
while IFS= read -r line; do
10+
if [[ ! "${previous_logs[*]}" =~ "$line" ]]; then
11+
echo "$line"
12+
fi
13+
done <<< "$new_logs"
14+
15+
# Update previous logs
16+
previous_logs=("$new_logs")
17+
}
18+
19+
# Initial fetch and filter
20+
fetch_and_filter_logs
21+
22+
# Infinite loop to continuously fetch and filter logs
23+
while true; do
24+
fetch_and_filter_logs
25+
sleep 1
26+
done

motoko/canister_logs/src/Main.mo

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Debug "mo:base/Debug";
2+
import { abs } = "mo:base/Int";
3+
import { now } = "mo:base/Time";
4+
import { setTimer; recurringTimer } = "mo:base/Timer";
5+
import StableMemory "mo:base/ExperimentalStableMemory";
6+
7+
actor CanisterLogs {
8+
9+
let timerDelaySeconds = 5;
10+
let second = 1_000_000_000;
11+
12+
private func execute_timer() : async () {
13+
Debug.print("right before timer trap");
14+
Debug.trap("timer trap");
15+
};
16+
17+
ignore setTimer<system>(#seconds (timerDelaySeconds - abs(now() / second) % timerDelaySeconds),
18+
func () : async () {
19+
ignore recurringTimer<system>(#seconds timerDelaySeconds, execute_timer);
20+
await execute_timer();
21+
});
22+
23+
public func print(text : Text) : async () {
24+
Debug.print(text);
25+
};
26+
27+
public func trap(text : Text) : async () {
28+
Debug.print("right before trap");
29+
Debug.trap(text);
30+
};
31+
32+
public func memory_oob() : async () {
33+
Debug.print("right before memory out of bounds");
34+
let offset : Nat64 = 10;
35+
let value : Nat = 20;
36+
let _blob = StableMemory.loadBlob(offset, value); // Expect reading outside of memory bounds to trap.
37+
};
38+
39+
};

0 commit comments

Comments
 (0)