30 lines
957 B
Python
Raw Normal View History

2024-12-17 12:08:00 +00:00
import logging
# Create or get the root logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # Set the logging level
# Create a file handler
2024-12-17 12:10:02 +00:00
file_handler = logging.FileHandler("app.log")
2024-12-17 12:08:00 +00:00
file_handler.setLevel(logging.DEBUG) # Set the level for this handler
# Create a console handler (optional, for logging to the console too)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create a logging format
2024-12-17 12:10:02 +00:00
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2024-12-17 12:08:00 +00:00
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# Log some messages
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")