Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to Airflow Logs

Tags:

airflow

One way to write to the logs in Airflow is to return a string from a PythonOperator like on line 44 here.

Are there other ways that allow me to write to the airflow log files? I've found that print statements are not saved to the logs.

like image 748
thatkaiguy Avatar asked Oct 19 '16 01:10

thatkaiguy


People also ask

Where are the Airflow logs?

Users can specify the directory to place log files in airflow. cfg using base_log_folder . By default, logs are placed in the AIRFLOW_HOME directory.

Does Airflow use log4j?

Many common logging libraries, such as log4j, offer log rotation strategies to clear out older logs. However, Airflow does not utilize anything like it.


3 Answers

You can import the logging module into your code and write to logs that way

import logging

logging.info('Hello')

Here are some more options

import logging    

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
like image 196
J.Fratzke Avatar answered Oct 22 '22 09:10

J.Fratzke


You might use airflow logger

import logging


logger = logging.getLogger("airflow.task")
logger.error("Your custom error")
like image 31
Heedge Avatar answered Oct 22 '22 10:10

Heedge


There's a logger mixin class you can use:

https://github.com/apache/airflow/blob/main/airflow/utils/log/logging_mixin.py

from airflow.utils.log.logging_mixin import LoggingMixin

LoggingMixin().log.info("Hello")

Then in airflow logs you'll see:

[2021-07-07 15:55:42,370] {{logging_mixin.py:112}} INFO - Hello
like image 43
Alejandro Kaspar Avatar answered Oct 22 '22 10:10

Alejandro Kaspar