Logging
Level | Numberic Value |
---|
NOTSET | 0 |
DEBUG | 10 |
INFO | 20 |
WARNING | 30 |
ERROR | 40 |
CRITICAL | 50 |
import logging
dir(logging)
logging.basicConfig(filename = "~/py.log")
logger = logging.getLogger()
logger.info("our first message.")
print(logger.level)
import logging
from rich.logging import RichHandler
logger = logging.getLogger(__name__)
shell_handler = RichHandler()
file_handler = logging.FileHandler("debug.log")
logger.setLevel(logging.DEBUG)
shell_handler.setLevel(logging.DEBUG)
file_handler.setLevel(logging.DEBUG)
fmt_shell = '%(message)s'
fmt_file = '%(levelname)s %(asctime)s [%(filename)s:%(funcName)s:%(lineno)d] %(message)s'
shell_formatter = logging.Formatter(fmt_shell)
file_formatter = logging.Formatter(fmt_file)
shell_handler.setFormatter(shell_formatter)
file_handler.setFormatter(file_formatter)
logger.addHandler(shell_handler)
logger.addHandler(file_handler)
- To have aesthetic logging output easily: