Mitch
07/29/2024, 8:32 PMimport logger
to log messages to the prefect dashboard or do I need to use prefect's implementation of the logger tool?Marvin
07/29/2024, 8:32 PMimport logger
from Python's standard logging module won't automatically capture logs to Prefect's backend.
Here’s how you can use Prefect's logger:
python
from prefect import flow, get_run_logger
@flow
def my_flow():
logger = get_run_logger()
<http://logger.info|logger.info>("This is a log message that will appear in the Prefect dashboard.")
my_flow()
Using get_run_logger()
ensures that logs are captured and displayed in the Prefect UI.
For more information, check the Prefect docs.
If you have any further questions, feel free to ask!Mitch
07/29/2024, 8:34 PMMarvin
07/29/2024, 8:34 PMlogging.yml
file with the desired configuration.
2. Set the Logging Configuration Path:
Use the PREFECT_LOGGING_SETTINGS_PATH
environment variable to point to your custom logging configuration file.
3. Forward Logs to Prefect:
Ensure your configuration forwards logs to Prefect's API. You can do this by using the APILogHandler
provided by Prefect.
Here is a basic example of what your logging.yml
file might look like:
yaml
version: 1
disable_existing_loggers: False
formatters:
standard:
format: "%(asctime)s.%(msecs)03d | %(levelname)-7s | %(name)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
formatter: standard
level: INFO
api:
class: prefect.logging.handlers.APILogHandler
level: INFO
loggers:
prefect:
handlers: [console, api]
level: INFO
propagate: True
my_custom_logger:
handlers: [console, api]
level: INFO
propagate: False
root:
handlers: [console, api]
level: INFO
4. Example Usage:
Here’s how you can use the standard logger in your flow:
python
import logging
from prefect import flow, task
@task
def my_task():
logger = logging.getLogger("my_custom_logger")
<http://logger.info|logger.info>("This is a log message from a task.")
@flow
def my_flow():
logger = logging.getLogger("my_custom_logger")
<http://logger.info|logger.info>("This is a log message from the flow.")
my_task()
my_flow()
5. Set the Environment Variable:
Make sure to set the PREFECT_LOGGING_SETTINGS_PATH
to point to your logging.yml
file:
sh
export PREFECT_LOGGING_SETTINGS_PATH=/path/to/logging.yml
By following these steps, your logs from the standard Python logging module should be captured and displayed in the Prefect dashboard.
For more detailed logs configuration, refer to the Prefect logging guide.