Send logs back to Kestra.

Your scripts can log to Kestra's backend during flow execution. This allows you to log events occuring during execution of a flow.

How to log from script tasks

Logging from Script and Commands tasks

The Scripts Plugin provides convenient methods to log to the Kestra backend during flow Execution. Under the hood, Kestra tracks logs from script tasks by searching standard output and standard error for ::{}:: patterns that allow you to specify log messages using a JSON request payload.

Below is an example showing logs as list of dictionaries:

json
{
    "logs": [
        {
            "level": "DEBUG",
            "message": "Hello World from logs!"
        },
        {
            "level": "INFO",
            "message": "Hello World!"
        }
    ]
}

Python

The example below shows how you can log from your Python script to Kestra's backend at runtime:

python
from kestra import Kestra

logger = Kestra.logger()
logger.debug("Hello World from logs!")
logger.info("Hello World!")

Here is a more comprehensive example in a flow:

yaml
id: logFromPython
namespace: company.team

tasks:
  - id: py
    type: io.kestra.plugin.scripts.python.Script
    script: |
      from kestra import Kestra

      logger = Kestra.logger()
      logger.info("Py task is alive!")

Node.js

Node.js follows the same syntax for sending logs as in Python. Here is an example:

You need to install the npm package, that can be done with a beforeCommands:

yaml
beforeCommands:
 - npm i @kestra-io/libs

Then, simply use the require function to import the Kestra package and emit logs:

js
const Kestra = require("@kestra-io/libs");

const logger = Kestra.logger();
logger.debug("Hello World from logs!");
logger.info("Hello World!");

Shell

To log from a Shell task, wrap the JSON payload with double colons '::{"logs": [{"level":"DEBUG","message":"Hello World!"}]}::' as shown in the following examples:

bash
echo '::{"logs": [{"level":"DEBUG","message":"Hello World from logs!"},{"level":"INFO","message":"Hello World!"}]}::'

The JSON payload should be provided without any spaces.

Here is a comprehensive example in a flow:

yaml
id: shell_script
namespace: company.team

tasks:
  - id: shell_script
    type: io.kestra.plugin.scripts.shell.Script
    containerImage: ubuntu
    script: |
      echo '::{"logs": [{"level":"INFO","message":"Shell task is alive!"}]}::'

Was this page helpful?