Airflow + Python Logging模块未写入日志文件

13

尝试将“hello world”写入Airflow日志(Airflow 1.10.3)。根据这里这里给出的解决方案,我应该只需要import logginglogging.info('hello world')。但是这对我似乎不起作用。

import logging
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator

default_args = {
    'owner': 'benten',
    'depends_on_past': False,
    'start_date': datetime(2019, 7, 25),
    'email_on_failure': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
    }

def logging_is_fun():
    logging.debug("hellow world")
    logging.info("hello world")
    logging.critical("hello world")
    return None

with DAG('fun_logs', schedule_interval='45 * * * *', default_args=default_args) as dag:
    log_task = PythonOperator(python_callable=logging_is_fun, task_id='log_test_task')

我手动触发了DAG,任务成功执行。但是当我检查日志时,只看到以下内容:

*** Reading local file: /home/ubuntu/airflow/logs/fun_logs/log_test_task/2019-08-31T19:22:49.653712+00:00/1.log

我的那些"hello world"语句去哪了? 我不指望能看到所有的语句,因为我的日志级别设置。但我希望能看到关键信息。

我的airflow.cfg文件中包含以下内容(据我所知,所有设置均为默认设置):

# The folder where airflow should store its log files
# This path must be absolute
base_log_folder = /home/ubuntu/airflow/logs

# Airflow can store logs remotely in AWS S3, Google Cloud Storage or Elastic Search.
# Users must supply an Airflow connection id that provides access to the storage
# location. If remote_logging is set to true, see UPDATING.md for additional
# configuration requirements.
remote_logging = False
remote_log_conn_id =
remote_base_log_folder =
encrypt_s3_logs = False

# Logging level
logging_level = WARN
fab_logging_level = WARN

# Logging class
# Specify the class that will specify the logging configuration
# This class has to be on the python classpath
# logging_config_class = my.path.default_local_settings.LOGGING_CONFIG
logging_config_class =

# Log format
log_format = [%%(asctime)s] {%%(filename)s:%%(lineno)d} %%(levelname)s - %%(message)s
simple_log_format = %%(asctime)s %%(levelname)s - %%(message)s

# Log filename format
log_filename_template = {{ ti.dag_id }}/{{ ti.task_id }}/{{ ts }}/{{ try_number }}.log
log_processor_filename_template = {{ filename }}.log
dag_processor_manager_log_location = /home/ubuntu/airflow/logs/dag_processor_manager/dag_processor_manager.log
4个回答

8

airflow.cfg中将logging_level = INFO设置为WARN,您就可以看到日志。

原因

logging_level记录当airflow事件达到那些日志级别时的情况。例如,一个airflow操作被弃用会生成一个作为WARN记录的airflow事件。

就您的代码而言,它们只是要记录的正常Python语句。因此,它们实际上属于airflow中INFO log_level。因此,如果将logging_level设置为INFO,您应该能够看到日志记录语句。


成功了!我曾经为此苦苦思索,直到抓狂。之前我的日志文件一直不断膨胀,所以我将其设置为警告级别。但是我没有意识到脚本中的记录级别会被覆盖。 - benten
@Amit Singh我有相同的代码,只是日志级别为INFO。我无法获取调试输出,即logging.debug("hello world")。我需要启用其他什么设置吗? - pm1359
@benten 我有相同的代码,只是日志级别是INFO。我没有得到调试信息,logging.debug("hello world")。我需要启用其他什么吗? - pm1359
不太确定,Maryam。我很久没看过这个了。 - benten

1
你只需要运行这个任务。只需添加log_task.run(),你的日志就会为你呈现出来。
只需测试它,它就可以工作,以下是输出的样子:
[2019-09-03 02:19:15,990] {main.py:18} CRITICAL - hello world
[2019-09-03 02:19:16,024] {main.py:17} INFO - hello world
[2019-09-03 02:19:16,024] {main.py:18} CRITICAL - hello world
[2019-09-03 02:19:16,060] {main.py:17} INFO - hello world
[2019-09-03 02:19:16,061] {main.py:18} CRITICAL - hello world
[2019-09-03 02:19:16,100] {main.py:17} INFO - hello world
[2019-09-03 02:19:16,100] {main.py:18} CRITICAL - hello world
...
...

希望它有所帮助。

很好。你是从UI触发作业的吗?即使我运行这段代码时使用了log_task.run(),我仍然没有在我的日志中看到任何东西(我已经在with子句内外都运行了该行)。 - benten
似乎无论是命令行还是其他方式都没有关系。我猜测我的 airflow.cfg 文件中有些问题。 - benten
你尝试使用默认配置了吗? - Haroun Mohammedi
@Haroun Mohammedi 我有相同的代码,只是日志级别为INFO。我没有得到调试信息,logging.debug("hello world")。我需要启用其他什么吗? - pm1359
1
我很困惑。log_run.task()是从哪里来的?这是一个需要导入的Python函数吗? - BertC

0

免责声明:我也还在摸索中。但这是我目前的理解。

Airflow有多种记录日志的方式。任务记录到它们自己的文件中,而不是像airflow服务本身一样记录到stdout。

您可能会通过Web UI找到日志:

  1. 进入DAG
  2. 点击任务实例
  3. 点击[查看日志]

(在我的机器上使用自己的配置验证了您的示例DAG,上述步骤显示“hello world”,但终端上的stdout没有)

据我所见,这是唯一受logging_level配置影响的日志类型,默认情况下为INFO

文档中可以看到,这些日志存储在{dag_id}/{task_id}/{execution_date}/{try_number}.log中。

Astronomer也有指南。但我还没有尝试过。


0

我在这里顶起了一个旧的线程,但如果有人仍在苦苦挣扎,我有另一个解决方案。 在我的情况下,logging_level 已经设置为 INFO 并且使用 "airflow.task" 初始化记录器没有任何作用。 没有日志显示出来。 相反,最终有效的方法是从 airflow.cfg 清除 "default_impersonation" 设置。 如果您无法使用 'run_as_user' 选项,则只需要此设置。 参考链接 https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#default-impersonation

清除该设置后,日志可以很好地显示。


这是在Composer中吗?我已经将我的日志记录设置为INFO在Cloud Composer中,但消息仍然没有显示出来。 - CClarke
1
不,这个案例是在本地独立模式下运行的。如果有帮助的话,您也可以尝试覆盖Cloud Composer中的“default_impersonation”选项,因为它不是一个被阻止的配置选项:https://cloud.google.com/composer/docs/composer-2/composer-features#airflow_configuration - LTJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接