Logging from Scripts – Send Logs to Kestra
Send logs back to Kestra.
Log from scripts to Kestra
Your scripts can log to Kestra’s backend during flow execution. This logs events occurring during execution of a flow.
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 specify log messages using a JSON request payload.
Below is an example showing logs as list of dictionaries:
{ "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:
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:
id: logFromPythonnamespace: 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:
beforeCommands: - npm i @kestra-io/libsThen, simply use the require function to import the Kestra package and emit logs:
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:
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:
id: shell_scriptnamespace: 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?