Open vikramsubramanian opened 2 months ago
To efficiently manage logging across your entire application and ensure that all errors are captured and logged consistently, follow these steps:
# logger_config.py
from loguru import logger
# Define the log format
fmt = "[{time}] [{level}] - {extra} {name}:{function}:{line} - {message}"
# Remove default logger configuration
logger.remove()
# Add custom configuration
logger.add("path/to/file", format=fmt)
# Bind custom parameters
custom_logger = logger.bind(param_1="value1", param_2="value2")
def get_logger():
return custom_logger
logger_config.py
in your submodules.# submodule_1.py
from logger_config import get_logger
# Get the custom logger
custom_logger = get_logger()
@custom_logger.catch
def run():
# Your code here
pass
# main.py
from submodule_1 import run
if __name__ == "__main__":
run()
By following these steps, you can efficiently manage logging across your entire application, ensuring that all errors are captured and logged consistently.
💡 To rerun Mayil, comment mayil-ai rerun
. Mayil will incorporate any new context added to the ticket. Include details in your rerun comment to guide Mayil!
Hello,
I have a programme with a bunch of modules. In the main module a have a custom logger:
I want to use my custom logger to catch all errors within all modules. For base logger I could just import logger in all modules and then use decorarator
for all functions. What would be the best practice for custom logger? Should I pass it as the argument to submodules' functions and then explicitly call
custom_logger.error()within
except `blocks?)