Python logging

Ref: https://docs.python.org/3/library/logging.html

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, ์›ํ•˜๋Š” ์ˆ˜์ค€์œผ๋กœ ์ง„ํ–‰์ƒํ™ฉ์„ ํ…์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•˜์—ฌ logํŒŒ์ผ์— ๊ธฐ๋กํ•˜๊ฑฐ๋‚˜ ํ„ฐ๋ฏธ๋„์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์“ฐ๋ ˆ๋“œ ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค.

๋กœ๊น… ์ˆ˜์ค€

์ˆ˜์ค€ : NOTSET -> DEBUG -> INFO -> WARNING -> ERROR -> CRITICAL

์ˆ˜์ค€๊ฐ’์‚ฌ์šฉ
NOTSET0
DEBUG10์ƒ์„ธํ•œ ์ •๋ณด, ๋ฌธ์ œ ์ง„๋‹จ
INFO20ํ”„๋กœ๊ทธ๋žจ ์ง„ํ–‰ ์ˆœ์„œ
WARNING30๊ฒฝ๊ณ , ํ”„๋กœ๊ทธ๋žจ์€ ์‹คํ–‰ ๋จ
ERROR40์—๋Ÿฌ, ํ”„๋กœ๊ทธ๋žจ ๊ธฐ๋Šฅ ์ค‘ ์ผ๋ถ€ ์‹คํ–‰ ์•ˆ๋จ
CRITICAL50ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜

ํฌ๋งท

Ref: https://docs.python.org/3/library/stdtypes.html#old-string-formatting

ํฌ๋งท์„ค๋ช…
%(created)ftime.time()
%(msecs)dLogRecord๊ฐ€ ์ƒ์„ฑ๋œ ์‹œ๊ฐ„์˜ ๋ฐ€๋ฆฌ์ดˆ
%(relativeCreated)dlogging ๋ชจ๋“ˆ์ด ๋กœ๋“œ๋œ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ LogRecord๊ฐ€ ์ƒ์„ฑ๋œ ์‹œ๊ฐ„์˜ ๋ฐ€๋ฆฌ์ดˆ
%(pathname)sํŒŒ์ผ ๊ฒฝ๋กœ
%(filename)sํŒŒ์ผ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ๋ช… ๋ถ€๋ถ„
%(module)sํŒŒ์ผ๋ช…์˜ ํ™•์žฅ์ž๋ฅผ ์ œ์™ธํ•œ ๋ถ€๋ถ„
%(asctime)s'yyyy-mm-dd hh:mm:ss,sss'
%(name)s๋กœ๊ฑฐ ์ด๋ฆ„
%(levelname)s๋กœ๊น… ์ˆ˜์ค€
%(levelno)s๋กœ๊น… ์ˆ˜์ค€ ๊ฐ’
%(lineno)d๋กœ๊น… ํ˜ธ์ถœ ํ–‰ ๋ฒˆํ˜ธ
%(funcName)sํ•จ์ˆ˜ ๋ช…
%(message)s๋ฉ”์‹œ์ง€
%(process)dํ”„๋กœ์„ธ์Šค ID
%(processName)sํ”„๋กœ์„ธ์Šค ์ด๋ฆ„
%(thread)d์“ฐ๋ ˆ๋“œ ID
%(threadName)s์“ฐ๋ ˆ๋“œ ์ด๋ฆ„

ํ•จ์ˆ˜

log = logging.getLogger(name=None)
  • @brief logging.Logger ๊ฐ์ฒด ๋ฐ˜ํ™˜
  • @param name ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ฉด __name__ ์„ค์ •
  • @return logging.Logger
logging.basicConfig(**kwargs)
  • @brief ๋ฃจํŠธ ๋กœ๊ฑฐ ์„ค์ •, ๋กœ๊น…์ด ์‹œ์ž‘๋˜๊ธฐ์ „ ํ˜ธ์ถœํ•ด์•ผ ์œ ํšจํ•จ
  • @param filename ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ FileHandler๋ฅผ ์ƒ์„ฑ, ์„ค์ • ์•ˆํ•˜๋ฉด StreamHandler ์ƒ์„ฑ
  • @param format ํฌ๋งท ex) fmt='[%(levelname)-8s] %(filename)-10s %(lineno) 4d ํ–‰ : %(message)s'
  • @param datefmt time.strftime() ์„ค์ • ๋ฐฉ๋ฒ• Ref
  • @param level ์ˆ˜์ค€
  • @param handlers ํ•ธ๋“ค๋Ÿฌ ์„ค์ •, filename์ด๋‚˜ stream์„ ์„ค์ •ํ•˜๋ฉด ์•ˆ๋จ, ํฌ๋งคํ„ฐ ์„ค์ • ์•ˆํ•˜๋ฉด format์ด ์„ค์ •๋จ

์ž์‹ ๋กœ๊ฑฐ๋Š” ์ž์‹ ์—๊ฒŒ ์„ค์ •๋œ ์ˆ˜์ค€์— ๋งž์ถฐ ๋ถ€๋ชจ ๋กœ๊ฑฐ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

logging.Logger

logging.Logger.setLevel(level)
  • @brief ์ˆ˜์ค€ ์„ค์ •
logging.Logger.addHandler(hdlr)
  • @brief ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋ก

์ž์‹ ๋กœ๊ฑฐ์— ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ธ๋“ค๋Ÿฌ์™€ ๋ถ€๋ชจ ๋กœ๊ฑฐ ๋ชจ๋‘์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ logging.Logger.propagate์˜ ๊ฐ’์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

logging.Logger.debug(msg, *args, **kwargs)
logging.Logger.info(msg, *args, **kwargs)
logging.Logger.warning(msg, *args, **kwargs)
logging.Logger.error(msg, *args, **kwargs)
logging.Logger.critical(msg, *args, **kwargs)

logging.Handler

stream_handler = logging.StreamHandler()
file_handler = logging.FileHandler()
import logging.handlers
http_handler = logging.handlers.HTTPHandler()
queue_handler = logging.handlers.QueueHandler()
logging.Handler.setLevel(level)
  • @brief ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์‹œ์ง€ ์ˆ˜์ค€ ์„ค์ •
logging.Handler.setFormatter(fmt)
  • @brief ์ถœ๋ ฅ๋˜๋Š” ๋ฉ”์‹œ์ง€ ํฌ๋งท ์„ค์ •
  • @param fmt ํฌ๋งทํ„ฐ

logging.Formatter

formatter = logging.Formatter(fmt=None, datefmt=None, style='%')
  • @brief ์ถœ๋ ฅ ํ˜•์‹์„ ์„ค์ •
  • @param fmt ํฌ๋งท
  • @param datefmt time.strftime() ์„ค์ • ๋ฐฉ๋ฒ• Ref

Examples

child.py

import logging
log = logging.getLogger(__name__)
log.setLevel(logging.ERROR)
def child_function():
log.debug("debug")
log.info("info")
log.warning("warning")
log.error("error")
log.critical("critical")

main.py

import child
import logging
'''
๋ฃจํŠธ ๋กœ๊ฑฐ ์„ค์ •, format๋งŒ ์„ค์ •ํ–ˆ์œผ๋ฏ€๋กœ StreamHandler ์ƒ์„ฑ
'''
logging.basicConfig(
format='[%(levelname)-8s] %(filename)-10s %(lineno) 4d ํ–‰ : %(message)s',
level=logging.CRITICAL)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
def main_function():
log.debug("debug")
log.info("info")
log.warning("warning")
log.error("error")
log.critical("critical")
if __name__ == '__main__':
# __main__ ๋กœ๊ฑฐ
main_function()
# child ๋กœ๊ฑฐ
child.child_function()
# ๋ฃจํŠธ ๋กœ๊ฑฐ
logging.debug("debug")
logging.critical("critical")

๊ฒฐ๊ณผ

[INFO ] main.py 16 ํ–‰ : info
[WARNING ] main.py 17 ํ–‰ : warning
[ERROR ] main.py 18 ํ–‰ : error
[CRITICAL] main.py 19 ํ–‰ : critical
[ERROR ] child.py 12 ํ–‰ : error
[CRITICAL] child.py 13 ํ–‰ : critical
[CRITICAL] main.py 29 ํ–‰ : critical
Last updated on