簡體   English   中英

Dagster 在從實體調用的標准 python 函數中使用 dagster 的自定義記錄器

[英]Dagster use dagster's custom loggers in standard python functions called from solids

我可以配置一個自定義記錄器(例如,一個文件記錄器),我可以從context.log.info的實體中成功使用它(例如)。 如何在標准 Python function / class 中使用相同的記錄器?

我正在使用標准的colored_console_logger ,以便我可以直接在控制台中看到正在發生的事情。 這個想法是將它(或與它一起使用)與另一個(自定義)記錄器交換。

可重現的例子:test_logging.py

from dagster import solid, pipeline, execute_pipeline, Field, ModeDefinition
from dagster.loggers import colored_console_logger

from random_func import random_func


@solid
def test_logs(context):
    context.log.info("Hello, world!")
    random_func()


@pipeline(mode_defs=[
    ModeDefinition(logger_defs={"console_logger": colored_console_logger})
])
def test_pipeline():
    test_logs()


if __name__ == "__main__":
    execute_pipeline(test_pipeline,
                     run_config={
                         'loggers': {
                             'console_logger': {
                                 'config': {
                                     'log_level': 'DEBUG',
                                     'name': 'console_logger',
                                 }
                             }
                         }
                     })

隨機函數.py

import logging

lgr = logging.getLogger('console_logger')


def random_func():
    lgr.info('in random func')
    print('\nhi\n')

Dagster 沒有提供任何全局方式來訪問實體上下文中的對象——您需要讓 function 接受參數並將記錄器從實體傳遞給它。

import logging

lgr = logging.getLogger('console_logger')


def random_func(logger=lgr):
    logger.info('in random func')
    print('\nhi\n')
@solid
def test_logs(context):
    context.log.info("Hello, world!")
    random_func(logger=context.log)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM