Logs

Understand Dapr logging

Dapr produces structured logs to stdout either as a plain text or JSON formatted. By default, all Dapr processes (runtime and system services) write to console out in plain text. To enable JSON formatted logs, you need to add the --log-as-json command flag when running Dapr processes.

If you want to use a search engine such as Elastic Search or Azure Monitor to search the logs, it is recommended to use JSON-formatted logs which the log collector and search engine can parse using the built-in JSON parser.

Log schema

Dapr produces logs based on the following schema.

FieldDescriptionExample
timeISO8601 Timestamp2011-10-05T14:48:00.000Z
levelLog Level (info/warn/debug/error)info
typeLog Typelog
msgLog Messagehello dapr!
scopeLogging Scopedapr.runtime
instanceContainer Namedapr-pod-xxxxx
app_idDapr App IDdapr-app
verDapr Runtime Version0.5.0

Plain text and JSON formatted logs

  • Plain text log examples
  1. time="2020-03-11T17:08:48.303776-07:00" level=info msg="starting Dapr Runtime -- version 0.5.0-rc.3 -- commit v0.3.0-rc.0-155-g5dfcf2e" instance=dapr-pod-xxxx scope=dapr.runtime type=log ver=0.5.0-rc.3
  2. time="2020-03-11T17:08:48.303913-07:00" level=info msg="log level set to: info" instance=dapr-pod-xxxx scope=dapr.runtime type=log ver=0.5.0-rc.3
  • JSON formatted log examples
  1. {"instance":"dapr-pod-xxxx","level":"info","msg":"starting Dapr Runtime -- version 0.5.0-rc.3 -- commit v0.3.0-rc.0-155-g5dfcf2e","scope":"dapr.runtime","time":"2020-03-11T17:09:45.788005Z","type":"log","ver":"0.5.0-rc.3"}
  2. {"instance":"dapr-pod-xxxx","level":"info","msg":"log level set to: info","scope":"dapr.runtime","time":"2020-03-11T17:09:45.788075Z","type":"log","ver":"0.5.0-rc.3"}

Configurating plain text or JSON formatted logs

Dapr supports both plain text and JSON formatted logs. The default format is plain-text. If you want to use plain text with a search engine, you will not need to change any configuring options.

To use JSON formatted logs, you need to add additional configuration when you install Dapr and deploy your app. The recommendation is to use JSONformatted logs because most log collectors and search engines can parse JSON more easily with built-in parsers.

Configuring log format in Kubernetes

The following steps describe how to configure JSON formatted logs for Kubernetes

Install Dapr to your cluster using the Helm chart

You can enable JSON formatted logs for Dapr system services by adding --set global.logAsJson=true option to Helm command.

  1. helm install dapr dapr/dapr --namespace dapr-system --set global.logAsJson=true

Enable JSON formatted log for Dapr sidecars

You can enable JSON-formatted logs in Dapr sidecars activated by the Dapr sidecar-injector service by adding the dapr.io/log-as-json: "true" annotation to the deployment.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pythonapp
  5. namespace: default
  6. labels:
  7. app: python
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: python
  13. template:
  14. metadata:
  15. labels:
  16. app: python
  17. annotations:
  18. dapr.io/enabled: "true"
  19. dapr.io/app-id: "pythonapp"
  20. dapr.io/log-as-json: "true"
  21. ...

Log collectors

If you run Dapr in a Kubernetes cluster, Fluentd is a popular container log collector. You can use Fluentd with a json parser plugin to parse Dapr JSON formatted logs. This how-to shows how to configure the Fluentd in your cluster.

If you are using the Azure Kubernetes Service, you can use the default OMS Agent to collect logs with Azure Monitor without needing to install Fluentd.

Search engines

If you use Fluentd, we recommend to using Elastic Search and Kibana. This how-to shows how to set up Elastic Search and Kibana in your Kubernetes cluster.

If you are using the Azure Kubernetes Service, you can use Azure monitor for containers without indstalling any additional monitoring tools. Also read How to enable Azure Monitor for containers

References

Last modified August 2, 2021 : Fix Java SDK link (#1695) (2c67fd1)