Hi All! I’m trying to get stdout from my tasks to ...
# prefect-community
k
Hi All! I’m trying to get stdout from my tasks to go to the UI (with the docker agent) and not having any luck. I’m setting the
PREFECT__LOGGING__EXTRA_LOGGERS
environment variable both when launching my agent and injecting that variable into my docker container at runtime (with the
-env
arg). Any advice on how to proceed?
z
Hi @Kevin Weiler — what are you setting EXTRA_LOGGERS to?
k
Copy code
PREFECT__LOGGING__EXTRA_LOGGERS="['research']"
research
is the name of the logger
(and for completeness) - the docker agent is run with this argument:
Copy code
--env PREFECT__LOGGING__EXTRA_LOGGERS=${PREFECT__LOGGING__EXTRA_LOGGERS}
z
Can you show me a minimal flow that reproduces this?
k
yep - good exercise for me to do - will take me a few…
whew - ok, I have a nice self-contained example, and put it here: https://github.com/aquanauts/prefect_logger_repro
research/flow.py
is what you’re looking for
z
I have to go so I can only give you a brief answer for now — some quick thoughts: • Make sure you’ve attached a stream handler to your logger to write to stdout • Try adding the
log_stdout
flag to your task https://docs.prefect.io/core/concepts/logging.html#logging-stdout • Consider just using
<http://prefect.context.logger.info|prefect.context.logger.info>("…")
If those don’t work/fit your use-case I can take a look again tomorrow morning!
k
thanks for looking @Zanie • that logger writes to stdout when I run it outside of prefect - does something need to happen in addition? • I’ve added that - it doesn’t work and I’m not entirely sure what it does as I’ve tried that before • this wouldn’t fit our use-case unfortunately
z
Alright I'll take a more thorough look tomorrow then!
k
thanks again!
for tomorrow - I tried explicitly adding a stream handler and adding
log_stdout=True
to the task decorator … no dice: https://github.com/aquanauts/prefect_logger_repro/commit/4fd61a863c2bbeeb9f313278430c38f52a42a26a#
z
Here’s a minimal working example
Copy code
import os
import sys
import logging
import prefect
from prefect import task, Flow


def get_logger():
    logger = logging.getLogger("my-named-logger")
    logger.addHandler(logging.StreamHandler(sys.stdout))
    logger.setLevel(logging.DEBUG)
    return logger

@task()
def log_message():
    logger = get_logger()
    <http://logger.info|logger.info>("Hello world!")
    logger.error("Foo!")


with Flow('flow-with-extra-logger') as flow:
	log_message()
    
flow.register("first")
Agent run with:
prefect agent local start --env PREFECT__LOGGING__EXTRA_LOGGERS="['my-named-logger']"
Changing to docker storage e.g.
Copy code
flow.storage = Docker()
flow.register("first")
and using the docker agent works as well
k
thanks so much @Zanie. The pattern we’re going for here is actually a part of this thread: https://prefect-community.slack.com/archives/CL09KU1K7/p1605737921090000 which perhaps I should have continued … @Dolor Oculus is my colleague
so, I’m adding
research
because that’s the package name
lemme know if I should continue this in the prior thread
z
Yep this uses the logger for your package, I just named it
my-named-logger
in my example instead of
research
k
ahhhhhh - I see - I’ll give this a try - thanks!
z
This works fine as well
Copy code
import os
import sys
import logging
import prefect
from prefect import task, Flow
from prefect.environments.storage import Docker


LOGGER = logging.getLogger("my-named-logger")
LOGGER.addHandler(logging.StreamHandler(sys.stdout))
LOGGER.setLevel(logging.DEBUG)

@task()
def log_message():
    <http://LOGGER.info|LOGGER.info>("Hello world!")
    LOGGER.error("Foo!")


with Flow('flow-with-extra-logger') as flow:
	log_message()


flow.storage = Docker()
flow.register("first")
k
so indeed this works if
_LOGGER
is used in the task definition. But if I import a function that has
_LOGGER
defined in it’s file - I don’t get the output
z
I can’t test within your MRE
Copy code
❯ python research/flow.py
Traceback (most recent call last):
  File "research/flow.py", line 6, in <module>
    from research.example_pkg import things
ModuleNotFoundError: No module named 'research'
Are you adding it to your python_path? I noticed you’re not doing that in your docker storage. I’m surprised your flow would run at all?
Apologies —
PYTHONPATH
not
PYTHON_PATH
haha — I should be able to run your code
k
heh - cool cool - I’m not fond of our pattern of shoving stuff into a container and then setting
PYTHONPATH
but it’s what happened (with this repo anyway)
z
Figured it out 🙂
Sort of — I can reproduce your issue. This appears to be an issue with the
LocalDaskExecutor(scheduler="processes")
when I switch to using a
LocalExecutor
the logs work as expected!
Additionally
LocalDaskExecutor()
without using the processes scheduler works
k
IIIIIINteresting…..!
z
cc @Jim Crist-Harif — is extra_loggers expected to fail when using the process based scheduler like this?
j
Sorry, there's a lot going on in this thread, what example isn't working properly? Can I get a quick summary?
k
I think the (very) short story is that
extra_loggers
doesn’t work as expected when using
LocalDaskExecutor(scheduler="processes")
in fact - with a few small modifications to @Zanie’s most recent example, I have a self-contained example:
Copy code
import sys
import logging

from prefect import task, Flow
from prefect.engine.executors import LocalDaskExecutor
from prefect.environments import LocalEnvironment


LOGGER = logging.getLogger("my-named-logger")
LOGGER.addHandler(logging.StreamHandler(sys.stdout))
LOGGER.setLevel(logging.DEBUG)

@task()
def log_message():
    <http://LOGGER.info|LOGGER.info>("Hello world!")
    LOGGER.error("Foo!")


with Flow('flow-with-extra-logger') as flow:
    log_message()
flow.environment = LocalEnvironment(executor=LocalDaskExecutor(scheduler="processes"))

flow.run()
🙌 1
even with
PREFECT__LOGGING__EXTRA_LOGGERS="['my-named-logger']"
- this example does not log the expected output to the GUI
j
Ah, cool. I may not get to this today, but thanks for the reproducible example
k
cheers - thanks so much to you both for looking at this @Jim Crist-Harif - let me know if you’d like an issue in github with the repro
j
That'd be great actually, just to make sure this isn't lost.
k
all set guys - thanks again!
👍 1
Hey guys - just a gentle bump on this one as it’s a bit of a deal-breaker for us - any chance someone can take a look at this?
d
Hey @Kevin Weiler thanks for checking in! The fix here is planned, stay tuned 👍
k
thanks!
d
saw this PR went out today! https://github.com/PrefectHQ/prefect/pull/3989 🙏