<@ULVA73B9P> instead of having get_run_logger() fo...
# ask-marvin
f
@Marvin instead of having get_run_logger() for each task. Can we have a generic logger?
m
thought for 39.6 seconds
Yes, you can definitely set up a generic logger in Prefect instead of using
get_run_logger()
for each task. Here's how you can do it:
Copy code
python
from prefect import flow, task
from prefect.logging import get_logger

# Create a generic logger
logger = get_logger()

@task
def task1():
    <http://logger.info|logger.info>("This is task 1")
    return "task1 result"

@task
def task2():
    <http://logger.info|logger.info>("This is task 2")
    return "task2 result"

@flow
def my_flow():
    <http://logger.info|logger.info>("Starting flow")
    result1 = task1()
    result2 = task2()
    <http://logger.info|logger.info>("Flow completed")
Key points about this approach: 1. Global Logger: Using
get_logger()
creates a logger that can be used anywhere in your code, not just within tasks. 2. Differences from get_run_logger(): -
get_run_logger()
is context-aware and automatically includes task/flow run information -
get_logger()
is more generic and behaves like a standard Python logger 3. Best Practices: - You can name your logger by passing a name:
get_logger("my_custom_logger")
- Consider using this approach when you want consistent logging across your entire application - The generic logger still integrates with Prefect's logging system 4. Customization: You can customize the logger further if needed:
Copy code
python
logger = get_logger("my_logger")
logger.setLevel("INFO")  # Set logging level
Remember that while using a generic logger is more convenient, you might lose some of the automatic context that
get_run_logger()
provides. The run-specific logger automatically includes information about the current task or flow run, which can be valuable for debugging and monitoring. If you need both convenience and context, you could create a custom logger that combines both approaches:
Copy code
python
from prefect import flow, task
from prefect.logging import get_logger
from contextlib import contextmanager
from typing import Optional

class PrefectCustomLogger:
    def __init__(self, name: Optional[str] = None):
        self.generic_logger = get_logger(name)
    
    def info(self, msg, *args, **kwargs):
        try:
            from prefect.logging import get_run_logger
            run_logger = get_run_logger()
            <http://run_logger.info|run_logger.info>(msg, *args, **kwargs)
        except Exception:
            # Fall back to generic logger if not in a task/flow context
            <http://self.generic_logger.info|self.generic_logger.info>(msg, *args, **kwargs)

# Usage
logger = PrefectCustomLogger("my_app")

@task
def my_task():
    <http://logger.info|logger.info>("This will use run logger if in task context, otherwise generic logger")
This hybrid approach gives you the best of both worlds: the convenience of a global logger with the context-awareness of
get_run_logger()
when available. For more details about logging customization in Prefect, you can refer to the logging customization documentation.