Function logs arrive in the OpenFaaS CLI

Vivek Kumar Singh
Jul 17 · 3 min read

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 0.8.0 .

As of today this feature is available if you are already using faas-netes, standby for support in other providers as well.

Installation

Follow the instructions here to install the latest version of OpenFaaS on Kubernetes using helm.

You should also have faas-cli installed with a version greater than or equal to 0.8.21. Follow instructions here to install the latest version of CLI.

Create and deploy a function

Now that we have everything set up. Let’s create, build and deploy a new function using faas-cli .

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>"

Rename astronaut-counter.yml to stack.yml

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.

Edit stack.yml file.

Astronaut Counter stack.yml

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 requirements.txt file.

./astronaut-counter/requirements.txt

requests

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.

./astronaut-counter/handler.py

Astronaut Counter handler.py

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 faas-cli up command.

Getting logs

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.

Streaming logs to the terminal

By default, 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 --follow flag.

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--tail=10.

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 --since flag.

faas-cli logs astronaut-counter --follow=false --since=10m

Wrapping up

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.

The astronaut-counter function used in this blog is available on Github.

What Next

Join the OpenFaaS community : https://docs.openfaas.com/community/

Try the self-paced workshop : https://github.com/openfaas/workshop

Vivek Kumar Singh

Written by

Contributor@OpenFaaS, Gopher, Pythonista. https://www.viveksyngh.me/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade