Via an extensible API, OpenFaaS Provides access to function logs via the Gateway API and the CLI. This provides a simple and consistent way to access function logs regardless of your orchestration provider.
The log provider for Kubernetes faas-netes fetches records directly from the Kubernetes API, so it will give similar output to kubectl logs -n openfaas-fn deploy/function.
The faas-cli logs NAME command will stream the logs for the named function. By default, it will attempt to follow the logs, but you can control the behavior of the stream using these flags
The CLI writes logs to stdout, so it can easily be chained with any of your favorite CLI tools: grep, sed, fzf etc.
For example, when you set the write_debug environment variable in the Sentiment Analysis function from the Store, it will print the function output to the logs, we can then search that output using grep. For example,
faas-clistoredeploySentimentAnalysis--envwrite_debug=trueecho"i like code"|faas-cliinvokesentimentanalysis
echo"i like functions"|faas-cliinvokesentimentanalysis
echo"i like containers"|faas-cliinvokesentimentanalysis
faas-clilogssentimentanalysis|grepsentence_count
2019-07-2212:44:08.202436478+0000UTCsentimentanalysis(sentimentanalysis-7887c5d8c5-5rnb5){"polarity":0.0,"sentence_count":1,"subjectivity":0.0}2019-07-2212:44:10.11422064+0000UTCsentimentanalysis(sentimentanalysis-7887c5d8c5-5rnb5){"polarity":0.0,"sentence_count":1,"subjectivity":0.0}2019-07-2212:44:11.882708263+0000UTCsentimentanalysis(sentimentanalysis-7887c5d8c5-5rnb5){"polarity":0.0,"sentence_count":1,"subjectivity":0.0}
Log retention and history will be determined by your cluster configuration and the log provider installed. The default configuration in the function provider for Kubernetes streams logs directly from the cluster. This means that you will only see logs from running function containers, and no long term history is not available. If you delete a function, you remove access to its logs.
With faasd, logs are stored in the journal, so the retention period lasts beyond the life of a function, however it may be limited by time or the number of lines.
If you want to retain logs over time, consider using a log aggregator like Elastic Search, Logz.io or Humio as an external data-store.
Grafana Loki can also provide fast access to logs without much additional overhead on the cluster. A log provider exists to enable "faas-cli" and the OpenFaaS API to query an external data-store, instead of Kubernetes itself.
The log system is designed to be extended with alternative providers, this means that logs could instead be supplied by a persistent storage, e.g. Loki or ElasticSearch. See the logs provider overview for more details about how providers work and available alternatives.
Structured logs are a form of machine-readable logs that treats logs as data sets rather than text which allows logs to be more easily searched and analyzed. Typically, these will be in the form of a JSON object on a single line and will allow you to achieve a high level of granular detail.
Introduced in of-watchdog v0.8.2, you can set the prefix_logs environment variable to false. This will remove the log prefix for all messages received via stdout/stderr, meaning that only the <msg> will be sent to the terminal.