티스토리 뷰

Python

logging

techbard 2016.05.10 09:51

# logging


import logging


logging.basicConfig(level=logging.INFO)


logging.debug('This message will be ignored')

logging.info('This should be logged')

logging.warning('And this, too')


# 결과

INFO:root:This should be logged

WARNING:root:And this, too


# 상수


print(logging.DEBUG)

print(logging.INFO)

print(logging.WARNING)


# 결과

10

20

30


# 파일 저장


import logging


logging.basicConfig(level=logging.INFO, filename='logs.txt', filemode='w')


logging.debug('This message will be ignored')

logging.info('This should be logged')

logging.warning('And this, too')



import logging


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# logging.disable(logging.CRITICAL)


def buggy_factorial(n):

logging.debug('Start of factorial (%s)' % (n))


total = 1

for i in range(n+1):

total *= i

logging.debug('i is %s, total is %s' % (i, total))


logging.debug('Return value is %s' % (total))

return total


print(buggy_factorial(5))


# 결과

2016-06-10 15:32:53,441 - DEBUG - Start of factorial (5)

2016-06-10 15:32:53,441 - DEBUG - i is 0, total is 0

2016-06-10 15:32:53,441 - DEBUG - i is 1, total is 0

2016-06-10 15:32:53,441 - DEBUG - i is 2, total is 0

2016-06-10 15:32:53,441 - DEBUG - i is 3, total is 0

2016-06-10 15:32:53,441 - DEBUG - i is 4, total is 0

2016-06-10 15:32:53,441 - DEBUG - i is 5, total is 0

2016-06-10 15:32:53,441 - DEBUG - Return value is 0

0


댓글
댓글쓰기 폼
공지사항
Total
407,535
Today
9
Yesterday
19
«   2019/09   »
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          
글 보관함