콘솔과 파일에 로그를 남기고 예외 처리가 포함된 기본 코드

import os
import logging, traceback
from logging.handlers import TimedRotatingFileHandler

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)-7s %(lineno)3s:%(funcName)20s %(message)s')
streamHander = logging.StreamHandler()
streamHander.setFormatter(formatter)
logger.addHandler(streamHander)

current_dir = os.path.dirname(os.path.abspath(__file__))
filename = __file__.split(".")[0] + ".log"
filename = current_dir + os.sep + "log" + os.sep + filename
fileHander = TimedRotatingFileHandler(filename=filename, when='midnight', backupCount=7)
fileHander.setFormatter(formatter)
logger.addHandler(fileHander)


def function_name():
    try:
        logger.debug("begin")

    except Exception as e:
        logger.error(traceback.format_exc())
    finally:
        logger.debug("end")


if __name__ == '__main__':
    function_name()