diff --git a/content/docs/15.how-to-guides/python.md b/content/docs/15.how-to-guides/python.md index da54b6e708..24606f8151 100644 --- a/content/docs/15.how-to-guides/python.md +++ b/content/docs/15.how-to-guides/python.md @@ -52,42 +52,6 @@ tasks: downloads = get_docker_image_downloads() ``` -## Logs - -If your Python code needs to log something to the console, we recommend using the `Kestra.logger()` method from the [Kestra pip package](https://github.com/kestra-io/libs) to instantiate a `logger` object — this logger is configured to correctly capture all Python log levels and send them to the Kestra backend. - -```yaml -id: python_logs -namespace: company.team - -tasks: - - id: python_logger - type: io.kestra.plugin.scripts.python.Script - allowFailure: true - warningOnStdErr: false - script: | - import time - from kestra import Kestra - - logger = Kestra.logger() - - logger.debug("DEBUG is used for diagnostic info.") - time.sleep(0.5) - - logger.info("INFO confirms normal operation.") - time.sleep(0.5) - - logger.warning("WARNING signals something unexpected.") - time.sleep(0.5) - - logger.error("ERROR indicates a serious issue.") - time.sleep(0.5) - - logger.critical("CRITICAL means a severe failure.") -``` - -You can read more about the Python Script task in the [Plugin documentation](/plugins/plugin-script-python/tasks/io.kestra.plugin.scripts.python.script) - ## Commands If you would prefer to put your Python code in a `.py` file (e.g. your code is much longer or spread across multiple files), you can run the previous example using the `io.kestra.plugin.scripts.python.Commands` type: @@ -232,6 +196,46 @@ tasks: _This example works for both `io.kestra.plugin.scripts.python.Script` and `io.kestra.plugin.scripts.python.Commands`._ +## Capture Logs + +If your Python code needs to log something to the console, we recommend using the `Kestra.logger()` method from the [Kestra pip package](https://github.com/kestra-io/libs) to instantiate a `logger` object — this logger is configured to correctly capture all Python log levels and send them to the Kestra backend. + +```yaml +id: python_logs +namespace: company.team + +tasks: + - id: python_logger + type: io.kestra.plugin.scripts.python.Script + allowFailure: true + warningOnStdErr: false + script: | + import time + from kestra import Kestra + + logger = Kestra.logger() + + logger.debug("DEBUG is used for diagnostic info.") + time.sleep(0.5) + + logger.info("INFO confirms normal operation.") + time.sleep(0.5) + + logger.warning("WARNING signals something unexpected.") + time.sleep(0.5) + + logger.error("ERROR indicates a serious issue.") + time.sleep(0.5) + + logger.critical("CRITICAL means a severe failure.") +``` + +When we execute the above example, we can see Kestra correctly captures the log levels in the Logs view: + +![logs](/docs/how-to-guides/python/logs.png) + +You can read more about the Python Script task in the [Plugin documentation](/plugins/plugin-script-python/tasks/io.kestra.plugin.scripts.python.script) + ## Handling Metrics You can also get [metrics](../04.workflow-components/01.tasks/02.scripts/06.outputs-metrics.md#outputs-and-metrics-in-script-and-commands-tasks) from your Python code. In this example, we can use the `time` module to time the execution time of the function and then pass this to Kestra so it can be viewed in the Metrics tab. You don't need to modify your flow in order for this to work. diff --git a/public/docs/how-to-guides/python/logs.png b/public/docs/how-to-guides/python/logs.png new file mode 100644 index 0000000000..8a76bf43a7 Binary files /dev/null and b/public/docs/how-to-guides/python/logs.png differ