Hey, I’m developing my first Prefect 2 flow after ...
# prefect-community
d
Hey, I’m developing my first Prefect 2 flow after about a year with Prefect 1 (-: I saw that now to get Prefect’s logger I should run “get_run_logger()“. Now this works inside a flow or a task context - but what should I do when a task calls a regular python function that contains logs (logger.info for example)? For example - In prefect 1 I could write “logger = prefect.context.get(LOGGER_NAME)” at the top of the py file that contained both Prefect tasks and regular python functions (that were used from within tasks), and the logger was used by both. I tried using “get_run_logger()” at the top of the py file but got this error “There is no active flow or task run context.” Thanks @Itay vidal FYI
1
a
using logging at the module scope is not supported but we have an open PR from Michael converting simple print statements to logs which may help in your use case https://github.com/PrefectHQ/prefect/pull/7580 alternatively, try attaching your logger as extra logger
Copy code
prefect config set PREFECT_LOGGING_EXTRA_LOGGERS='logger1,logger2'
👀 1
d
This is a problem since we want to use the same logging syntax for all of our project’s code - something like a general Prefect logger that will work for “everything that runs inside a flow scope” (this was the functionality with “prefect.context.get(LOGGER_NAME)” apart from the flow level logs). Flows, tasks and regular python functions that get invoked by tasks. Do you know if anything like this is planned? If not, assuming I want to use a single logger with the same syntax for flows, tasks and functions - what would you do? (printing inside regular python functions seems like a bad practice) - Wrap a customize logger and use it everywhere? This way I won’t use “get_run_logger()” because it only gives me a part of the logs (flow or task scope) Thanks
a
you're right that implementing a custom logger and adding that as extra logger seems to be the right solution - we have some WIP to further enhance logging, but not in a way that would by default accept all logs from any logger unless configured to do so
d
So should I add Prefects logger as an extra handler to a Python custom logger? (will I be able to use such a “regular” python logger inside a flow and a task scope?) Or the opposite way is better? Adding an extra logger to Prefects logger? (will this logger work for regular function? how? I wouldn’t be able to run “get_run_logger()” in regular python functions), Thanks
a
you would need to add this extra logger to Prefect, not the other way around
d
But how will this work when logging within a regular python function? Logging directly using the extra logger from functions that are not tasks?
a
Copy code
logger = logging.getLogger("yourlogger")
in Prefect:
Copy code
prefect config set PREFECT_LOGGING_EXTRA_LOGGERS='yourlogger'
👀 1