PACKAGE

import logging
import os
from logging.handlers import TimedRotatingFileHandler

VARIABLE

# terminal logging
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
  '%(asctime)s %(levelname)-7s %(lineno)3s:%(funcName)-15s %(message)s'
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

# directory logging
current_dir = os.path.dirname(os.path.abspath(__file__))
filename = current_dir + os.sep + "log" + os.sep + os.path.splitext(
  os.path.basename(__file__)
)[0] + ".log"
handler = TimedRotatingFileHandler(
  filename=filename, when='midnight', backupCount=7, encoding='utf8'
)
handler.suffix = '%Y%m%d'
formatter = logging.Formatter(
  '%(asctime)s %(levelname)-7s %(lineno)3s:%(funcName)-15s %(message)s'
)
handler.setFormatter(formatter)
logger.addHandler(handler)

FUNCTION

def functionName():
    logger.debug('logging')

REFERENCE

  1. dxample.py와 같은 위치에 log 폴더를 만들고, 그 안에 example.log 생성