In this blog post, I will show how you can access OpenFaaS function logs from the OpenFaaS CLI.
At the time of writing this blog, logs are available for only Kubernetes provider for OpenFaaS which is also called faas-netes. Log support was added to faas-netes version
0.8.0, please make sure you are running
faas-netes version greater than equal to
As of today this feature is available if you are already using faas-netes, standby for support in other providers as well.
Follow the instructions here to install the latest version of OpenFaaS on Kubernetes using helm.
Create and deploy a function
Now that we have everything set up. Let’s create, build and deploy a new function using
We’ll create a function called
astronaut-counter that counts the number of astronauts present in the International Space Station (ISS).
faas-cli new --lang python3 astronaut-counter --prefix="<username>"
mv astronaut-counter.yml stack.yml
Enable verbose logs for the function using
write_debug environment variable. Also, separate logs from the function response using
combine_output environment variable.
Wherever you see <username>, make sure you replace that with your docker hub user name.
We’ll be pulling in data from: http://api.open-notify.org/astros.json. To make HTTP request and pull the data, we will be using python
requests library. Let’s add that to function’s
Now let’s update the
handler code to pull data from the open notify API. We will also add some debug log statement for debugging purpose.
Build, push and deploy the function using one command.
faas-cli up -g http://<gateway-ip>:<port>
Make sure you have already logged into your registry before running
To get logs for a function, you can use
faas-cli logs <function-name> command which will stream logs to terminal.
faas-cli logs astronaut-counter -g http://<gateway-ip>:<port>
This is how logs looks like for one of the function call.
If you call the function several times with
faas-cli logs command running in the terminal, it should stream the logs as below.
faas-cli will print all logs and continue to stream the logs for 30 seconds. If you want to just print logs, you can use
faas-cli logs astronaut-counter --follow=false
To tail last
n lines of logs you can use
--tail flag. For example, tail last 10 lines
faas-cli logs astronaut-counter --tail=10
If you want to print all logs for a function relative to some duration e.g. get all logs for last 10 minutes, you can use
faas-cli logs astronaut-counter --follow=false --since=10m
The current implementation provides logs out of the box with the Kubernetes provider, but also allows us to plugin alternative log systems. If you are interested in adding support for other log systems or open-source contribution, please join the OpenFaas Community.
Join the OpenFaaS community : https://docs.openfaas.com/community/
Try the self-paced workshop : https://github.com/openfaas/workshop