Source
yaml
id: loguru
namespace: company.team
inputs:
- id: nr_logs
type: INT
defaults: 100
tasks:
- id: reproducer
type: io.kestra.plugin.scripts.python.Script
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: ghcr.io/kestra-io/pydata:latest
script: |
from loguru import logger
from faker import Faker
import time
import sys
logger.remove()
logger.add(sys.stdout, level="INFO")
logger.add(sys.stderr, level="WARNING")
def generate_logs(fake, num_logs):
logger.debug("This message will not show up as the log level is set to INFO")
logger.warning("Starting to generate log messages")
for _ in range(num_logs):
log_message = fake.sentence()
logger.info(log_message)
time.sleep(0.01)
logger.warning("Finished generating log messages")
if __name__ == "__main__":
faker_ = Faker()
generate_logs(faker_, int("{{ inputs.nr_logs }}"))
About this blueprint
Python
This flow demonstrates how to configure logging in a Python script using
Loguru. The Script task will generate,
by default, 100 random log messages, but this number of logs can be changed
at runtime using the input parameter nr_logs.
- The
containerImageproperty is set toghcr.io/kestra-io/pydata:latestto use thepydataDocker image that contains theloguruandfakerlibraries. - The
scriptproperty contains the Python code that will be executed by theScripttask.
The log level is set to INFO in the Script task. Therefore, the
logger.debug message will NOT show up in the logs. The logger.warning
messages will be translated to WARN-level logs in Kestra. The logger.info
messages will be translated to INFO-level logs in Kestra.
More Related Blueprints