Untitled
unknown
plain_text
20 days ago
1.2 kB
3
Indexable
Never
import logging import colorlog import sys def setup_logger(): """Set up the logger with colorlog formatter.""" logger = logging.getLogger() logger.setLevel(logging.DEBUG) # Create a ColoredFormatter formatter = colorlog.ColoredFormatter( "%(log_color)s%(asctime)s | %(levelname)-8s | %(name)s:%(funcName)s:%(lineno)d - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", log_colors={ 'DEBUG': 'cyan', 'INFO': 'green', 'WARNING': 'yellow', 'ERROR': 'red', 'CRITICAL': 'red,bg_white', }, secondary_log_colors={}, style='%' ) # Add a stream handler handler = logging.StreamHandler(sys.stdout) handler.setFormatter(formatter) logger.addHandler(handler) return logger # Set up the logger logger = setup_logger() # Example usage def main(): 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") if __name__ == "__main__": main()
Leave a Comment