1. 로깅을 사용해야 할때

logging.info(), logging.debug(), loggin.warning(), loggin.error(), logging.exception(), logging.critical()

2. 로깅 레벨

DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.

3. 로깅을 화면으로 보기

print()

또는 logging.info 만 화면으로 보여짐

4. 로깅을 파일로 저장해서 확인하기

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)

# command optoin 에서 로깅레벨 사용하기
--log=INFO
# 프로그램에서 사용하기
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
    raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level, ...)

# 로깅파일을 지우고 새로 작성하기
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)

# 변수 로깅하기
logging.warning('%s before you %s', 'Look', 'leap!')

# 포맷으로 로깅하기
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
  - DEBUG:This message should appear on the console
logging.basicConfig(format='%(asctime)s %(message)s')
  - 2010-12-12 11:41:42,612 is when this event was logged.
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
  - 12/12/2010 11:46:36 AM is when this event was logged

블로그 이미지

희망잡이

,