Is calling `get_dagster_logger()` outside of the b...
# ask-ai
s
Is calling
get_dagster_logger()
outside of the body of an op and passing it in the same as calling
context.log
?
s
No, calling
get_dagster_logger()
outside of the body of an op and passing it around is not the same as calling
context.log
directly within the op. While both methods allow you to log messages, they serve different purposes and have different implications. Using
get_dagster_logger()
allows you to obtain a Dagster-managed logger that can be used anywhere in your code, not just within the body of an op. This can be particularly useful when you have existing code that you are integrating with Dagster and you want to capture logs without having to pass the context object around. Messages produced using
get_dagster_logger()
will be treated identically to messages created using
context.log
, which means they will be captured by Dagster and linked to the relevant computations. On the other hand,
context.log
is available when you have a context object, which is typically within the body of an op. It is a convenient way to create log messages that are automatically associated with the current execution context.
context.log
is a subclass of the standard Python
logging.Logger
, so it behaves identically to any other Python logger. In summary, while both
get_dagster_logger()
and
context.log
can be used for logging within Dagster,
get_dagster_logger()
is particularly useful for cases where passing the context object is not feasible, and
context.log
is the standard way to log when you have access to the context within an op.