← Notes

December 8, 2020

Advanced logging in Python

Advanced logging in Python


Tags: logging python
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
fh = logging.FileHandler("file.log")

ch.setLevel(logging.INFO)
fh.setLevel(logging.DEBUG)

formatter = logging.Formatter("[%(asctime)s] %(levelname)s - %(funcName)s(): %(message)s", "%H:%M:%S")
ch.setFormatter(formatter)
formatter = logging.Formatter("[%(asctime)s] %(levelname)s - %(funcName)s(): %(message)s")
fh.setFormatter(formatter)

logger.addHandler(ch)
logger.addHandler(fh)