Application logs are essential to debugging production issues. In IBM Cloud Functions, all output written to stdout and stderr by actions is available in the activation records.
-
Create a new action named
logsfrom the following source files.function main(params) { console.log("function called with params", params) console.error("this is an error message") return { result: true } }
ibmcloud fn action create logs logs.js
ok: created action logs
-
Invoke the
logsaction to generate some logs.ibmcloud fn action invoke -r logs -p hello world
{ "result": true }
Retrieve activation record to verify logs have been recorded.
ibmcloud fn activation get --last
ok: got activation 9fc044881705479580448817053795bd
{
...
"logs": [
"20xx-11-14T09:49:03.021Z stdout: function called with params { hello: 'world' }",
"20xx-11-14T09:49:03.021Z stderr: this is an error message"
],
...
}Logs can also be retrieved without showing the whole activation record, using the activation logs command.
ibmcloud fn activation logs --last20xx-11-14T09:49:03.021404683Z stdout: function called with params { hello: 'world' }
20xx-11-14T09:49:03.021816473Z stderr: this is an error message
Activation logs can be monitored in real-time, rather than manually retrieving individual activation records.
-
In another terminal, run the following command to monitor logs from the
logsactions.ibmcloud fn activation poll
Enter Ctrl-c to exit. Polling for activation logs -
In your original terminal, run the following command multiple times.
ibmcloud fn action invoke logs -p hello world
ok: invoked /_/logs with id 0e8d715393504f628d715393503f6227 -
Check the output from the
pollcommand to see the activation logs.Activation: 'logs' (ae57d06630554ccb97d06630555ccb8b) [ "20xx-11-14T09:56:17.8322445Z stdout: function called with params { hello: 'world' }", "20xx-11-14T09:56:17.8324766Z stderr: this is an error message" ] Activation: 'logs' (0e8d715393504f628d715393503f6227) [ "20xx-11-14T09:56:20.8992704Z stdout: function called with params { hello: 'world' }", "20xx-11-14T09:56:20.8993178Z stderr: this is an error message" ] Activation: 'logs' (becbb9b0c37f45f98bb9b0c37fc5f9fc) [ "20xx-11-14T09:56:44.6961581Z stderr: this is an error message", "20xx-11-14T09:56:44.6964147Z stdout: function called with params { hello: 'world' }" ]