Peter Lim
03/04/2024, 6:45 PMScout
03/04/2024, 6:45 PMcontext.log
. Here's a simplified example based on the documentation's guidance: python import logging from dagster import job, op, OpExecutionContext class OpLogHandler(logging.Handler): def __init__(self, context): super().__init__() self.context = context def emit(self, record): log_entry = self.format(record) <http://self.context.log.info|self.context.log.info>(log_entry) @op def my_op(context: OpExecutionContext): # Set up the custom log handler to redirect logs to the op's context logger logger = logging.getLogger("my_job_logger") logger.setLevel(<http://logging.INFO|logging.INFO>) logger.addHandler(OpLogHandler(context)) # Here you would run the job and its logs would be captured by the custom handler # For example: # my_job.execute_in_process() @job def my_job(): # Define the job that will be run from the op pass # Execute the op, which in turn runs the job and captures its logs my_op()
In this example, OpLogHandler
is a custom logging handler that takes the OpExecutionContext
as an argument and uses it to write logs to the op's context logger. When the job is executed within the op, any logs emitted by the job's logger (my_job_logger
) are captured by the OpLogHandler
and redirected to the op's logs. Please note that this is a conceptual example and may require adjustments to fit your specific use case. The actual implementation details, such as how the job is run and how logs are emitted within the job, will depend on your application's architecture and requirements.