https://dagster.io/ logo
#ask-ai
Title
# ask-ai
p

Peter Lim

03/04/2024, 6:45 PM
when running a job from an op, how do you emit the job’s logs into the op’s logs
s

Scout

03/04/2024, 6:45 PM
To emit logs from a job that is run from within an op into the op's logs, you can use the Python logging module to capture and redirect the logs accordingly. The documentation provides guidance on how to work with Python logging in the context of Dagster. Here's a general approach based on the documentation's guidance: 1. Configure the Python logging module to capture logs at the desired level (e.g., INFO, DEBUG). 2. Create a custom logging handler that will emit the captured logs to the op's context logger. 3. Attach the custom handler to the logger used within the job. The documentation provides an example of how to configure logging in Dagster, including how to output logs to a file. While the example is specific to file logging, the concept can be adapted to redirect logs to the op's context logger by using a custom handler that writes to
context.log
. Here's a simplified example based on the documentation's guidance:
Copy code
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.