<@ULVA73B9P> when I'm developing, I often get this...
# ask-marvin
c
@Marvin when I'm developing, I often get this error from using the prefect logging. Is there a way to turn this error off? 134320.432 | INFO | flows.main - Initializing Log... Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2022.1.1\plugins\python\helpers\pydev\pydevd.py", line 1500, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files\JetBrains\PyCharm 2022.1.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:\wageup\prefect2\flows\routines\routines_main.py", line 767, in <module> self.run(client='aca', task='check_for_sftp_files') File "C:\wageup\prefect2\flows\routines\routines_main.py", line 63, in run self.u = Universal(client=self.client) File "C:\wageup\prefect2\flows\main.py", line 93, in init self.queryMachine = QueryGenerator(client=self.client, u=self) File "C:\wageup\prefect2\flows\etl_utilities\query_generator.py", line 154, in init self.logger = get_run_logger() File "C:\Users\Cody\anaconda3\envs\prefect2\lib\site-packages\prefect\logging\loggers.py", line 148, in get_run_logger raise MissingContextError("There is no active flow or task run context.") prefect.exceptions.MissingContextError: There is no active flow or task run context.
m
The error
MissingContextError: There is no active flow or task run context.
occurs when you try to use
prefect.get_run_logger()
outside of a Prefect flow or task run context. This can happen when you're testing task functions, using concurrent futures, or in an on_cancellation hook. Here are some related issues and their discussions: 1. prefect.get_run_logger() raises MissingContextError when testing task functions: This issue discusses the problem of getting a
MissingContextError
when testing task functions. The use of
prefect.get_run_logger()
within any task code will lead to a
MissingContextError
if the task function is called in a test. 2. No Prefect logger available by default in a vanilla Python function, even a callable on_cancellation hook: This issue discusses the problem of getting a
MissingContextError
when the on_cancellation hook is called. 3. prefect.get_run_logger() throws MissingContextError when using concurrent.futures.ThreadPoolExecutor: This issue discusses the problem of getting a
MissingContextError
when
prefect.get_run_logger()
is called in a method called by
concurrent.futures.ThreadPoolExecutor
. To avoid this error, ensure that you're using
prefect.get_run_logger()
within a Prefect flow or task run context. If you're testing, you might need to use a different approach for logging. If you're using concurrent futures or hooks, you might need to pass the logger as an argument or find another way to handle logging.
c
How can I make the logger think there is an active flow or task run context
@Marvin How can I make the logger think there is an active flow or task run context
m
To make the logger think there is an active flow or task run context, you can use Prefect's context management. You can manually set the context for your logger using the
prefect.context
module. Here's an example:
Copy code
from prefect import context

with context(run_id='test'):
    logger = prefect.get_run_logger()
In this example, the
run_id
is set to 'test', and then the logger is retrieved. This should prevent the
MissingContextError
from being raised. However, please note that this is a workaround and might not be the best practice. The Prefect logger is designed to be used within a flow or task run context, and using it outside of these contexts could lead to unexpected behavior. It's generally better to structure your code so that logging happens within the appropriate contexts.
n
if you're outside of a flow run, you can either disable the run logger or you can just use the regular logger