Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log inside Python function in PythonOperator in Airflow

Tags:

airflow

I am using a PythonOperator in my Airflow DAG and I need to print something inside the operator's Python function. I tried to print but apparently it didn't work out. Not so sure that's going to work. Next I tried to pass self.log in PythonOperator but I am not sure how to pass that reference.

task = PythonOperator(
    task_id='task1',
    python_callable=my_func,
    params={ ... },
    provide_context=True,
    dag=dag
)

...

def my_func(**context):
    ...
    print(some_message) # this didn't work.
like image 439
kee Avatar asked Aug 26 '18 00:08

kee


People also ask

How do you add a log in Airflow?

To enable custom logging, you need to create the configuration file ~/airflow/config/log_config.py and specify your modifications to DEFAULT_LOGGING_CONFIG . You might need to do this to add a custom handler.

What is Op_kwargs in Airflow?

op_kwargs (dict) – A dict of keyword arguments to pass to python_callable. provide_context (bool) – if set to true, Airflow will pass a set of keyword arguments that can be used in your function. This set of kwargs correspond exactly to what you can use in your jinja templates.


1 Answers

The way you should be logging in Airflow (and in Python in general) is through the logging module, that is,

import logging

on top of the DAG definition and

logging.info(some_message)

in place of the print statement in your my_func function. Other functions you can use besides info() (with different logging level/criticality) can be found in the Python docs: https://docs.python.org/3/howto/logging.html#logging-basic-tutorial

like image 75
Alessandro Cosentino Avatar answered Sep 24 '22 10:09

Alessandro Cosentino