Hi all! Can I ask if there's a way to setup custom...
# ask-community
w
Hi all! Can I ask if there's a way to setup custom log handlers on the prefect logger? I haven't tested it yet, but I'm pretty sure I can attach a handler, or check if a handler is already attached, when I call
prefect.context.get("logger")
, but is there a better place where I can hook up my own log handlers? For example, we are going to write a log handler that can send agent/flow logs to a central location like kafka or s3 or datadog or whatever. We haven't exactly decided the target location yet, but we do know we want a central space, and I'm just trying to find the best place to configure and hook up our custom log handler.
z
Hi! This is a bit tricky since the logger can be shipped across different execution contexts. I'd recommend writing a wrapper for the
@task
decorator (or a
Task
subclass) that injects your custom log handler into the context for each task at runtime if it doesn't already exist.
Someone also used a custom runner class https://github.com/PrefectHQ/prefect/issues/2239
Here's a simpler example of attaching a handler (although I'm not sure it'd work in all environments) https://github.com/PrefectHQ/prefect/issues/3662
a
If you happen to use ECS, you can configure this as log driver on your ECS task definition to send logs to a different place e.g to Splunk.
w
@Zanie Again, similar to the other thread, sorry for not ever responding. I somehow missed the notifications that anyone responded, but this is good info. In fact, it's very helpful, and I'm going to share it with our team.
@Anna Geller Same to you... I'm sorry I didn't notice you had responded. We don't use ECS, but we'll use the info earlier in the thread to create something. I appreciate it!