Home [Python] Logging Handler
Post
Cancel

[Python] Logging Handler

Multi Handler

  • error.log, info.log에 따로 파일에 저장
  • StreamHandlerRotatingFileHandler를 함께 사용해 화면, 파일에 함께 표시되도록
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import logging
from logging.handlers import RotatingFileHandler

def setup_logger():
  MAX_BYTES = 10000000 # Maximum size for a log file
  BACKUP_COUNT = 9 # Maximum number of old log files
  
  # The name should be unique, so you can get in in other places
  logger = logging.getLogger(__name__)
  logger.setLevel(logging.INFO) # the level should be the lowest level set in handlers

  log_format = logging.Formatter('[%(levelname)s] %(asctime)s - %(message)s')

  stream_handler = logging.StreamHandler()
  stream_handler.setFormatter(log_format)
  stream_handler.setLevel(logging.INFO)
  logger.addHandler(stream_handler)

  info_handler = RotatingFileHandler('info.log', maxBytes=MAX_BYTES, backupCount=BACKUP_COUNT)
  info_handler.setFormatter(log_format)
  info_handler.setLevel(logging.INFO)
  logger.addHandler(info_handler)

  error_handler = RotatingFileHandler('error.log', maxBytes=MAX_BYTES, backupCount=BACKUP_COUNT)
  error_handler.setFormatter(log_format)
  error_handler.setLevel(logging.ERROR)
  logger.addHandler(error_handler)
  
if __name__ == '__main__':
  setup_logger()
  for i in range(0, 1000):
    logger.info('This is a message {}'.format(i))
    if i % 5 == 0:
      logger.error('THis is a error {}'.format(i))
This post is licensed under CC BY 4.0 by the author.

Snippets in Visual Studio Code

[RaspberryPi] 애기 CCTV 만들기