본문 바로가기

Python_Beginer/Study

HRD 수업>파이썬을 이용한 자동화 스크립트 - logging 모듈

반응형

1. 로깅 출력

'''
로그 메시지를 디스플레이
코드 제일 위쪽에 로깅 설정 코드를 추가
'''
import logging
# 로깅 포맷팅 설정
logging.basicConfig(level=logging.DEBUG,
                    format=' %(asctime)s - %(levelname)s - %(message)s')

logging.debug('Start of program')

def factorial(n):
    logging.debug('Start of factorial(%s)' % (n))
    total = 1
    for i in range(n + 1):
        if not i:
            continue
        total *= i
        logging.debug('i is ' + str(i) + ', total is ' + str(total))
    logging.debug('End of factorial(%s)' % (n))
    return total

print(factorial(5))
logging.debug('End of program')

 

120
 2019-08-06 17:01:23,512 - DEBUG - Start of program
 2019-08-06 17:01:23,512 - DEBUG - Start of factorial(5)
 2019-08-06 17:01:23,512 - DEBUG - i is 1, total is 1
 2019-08-06 17:01:23,512 - DEBUG - i is 2, total is 2
 2019-08-06 17:01:23,512 - DEBUG - i is 3, total is 6
 2019-08-06 17:01:23,512 - DEBUG - i is 4, total is 24
 2019-08-06 17:01:23,512 - DEBUG - i is 5, total is 120
 2019-08-06 17:01:23,512 - DEBUG - End of factorial(5)
 2019-08-06 17:01:23,512 - DEBUG - End of program

Process finished with exit code 0

 

2. 로깅 파일 작성

'''
로그 메시지를 디스플레이
코드 제일 위쪽에 로깅 설정 코드를 추가
'''
import logging
# 로깅 포맷팅 설정
logging.basicConfig(level=logging.DEBUG, filename='log,txt',
                    format=' %(asctime)s - %(levelname)s - %(message)s')

logging.debug('Start of program')

def factorial(n):
    logging.debug('Start of factorial(%s)' % (n))
    total = 1
    for i in range(n + 1):
        if not i:
            continue
        total *= i
        logging.debug('i is ' + str(i) + ', total is ' + str(total))
    logging.debug('End of factorial(%s)' % (n))
    return total

print(factorial(5))
logging.debug('End of program')

 

log,txt
0.00MB

반응형