|
| 1 | +Title: Debugging - logging |
| 2 | +Date: 2016-11-27 17:48 |
| 3 | +Tags: python, debugging, logging |
| 4 | +Category: Python |
| 5 | +Slug: debugging-logging |
| 6 | +Author: Bruno Santana |
| 7 | +Email: santanasta@gmail.com |
| 8 | +Github: BrunoLSA |
| 9 | + |
| 10 | + |
| 11 | +Achei algo interessante no livro que estou lendo (Automatize tarefas maçantes com Python) e resolvi compartilhar. |
| 12 | + |
| 13 | +Trata-se do Logging, que ajuda no debug do programa. |
| 14 | + |
| 15 | +Vejam o exemplo nesse programa, com falha: |
| 16 | + |
| 17 | + :::python |
| 18 | + import logging |
| 19 | + logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') |
| 20 | + |
| 21 | + |
| 22 | + logging.debug('Start of program') |
| 23 | + |
| 24 | + def factorial(n): |
| 25 | + logging.debug('Start of factorial(%s%%)' % (n)) |
| 26 | + total = 1 |
| 27 | + for i in range(n+1): |
| 28 | + total *= i |
| 29 | + logging.debug('i is ' + str(i) + ', total is ' + str(total)) |
| 30 | + logging.debug('End of factorial(%s%%)' % (n)) |
| 31 | + return total |
| 32 | + |
| 33 | + print(factorial(5)) |
| 34 | + logging.debug('End of program') |
| 35 | + |
| 36 | + |
| 37 | +O programa retorna: |
| 38 | + |
| 39 | + :::python |
| 40 | + 2016-11-15 16:17:30,339 - DEBUG - Start of program |
| 41 | + 2016-11-15 16:17:30,340 - DEBUG - Start of factorial(5%) |
| 42 | + 2016-11-15 16:17:30,340 - DEBUG - i is 0, total is 0 |
| 43 | + 2016-11-15 16:17:30,340 - DEBUG - i is 1, total is 0 |
| 44 | + 2016-11-15 16:17:30,340 - DEBUG - i is 2, total is 0 |
| 45 | + 2016-11-15 16:17:30,340 - DEBUG - i is 3, total is 0 |
| 46 | + 2016-11-15 16:17:30,340 - DEBUG - i is 4, total is 0 |
| 47 | + 2016-11-15 16:17:30,340 - DEBUG - i is 5, total is 0 |
| 48 | + 2016-11-15 16:17:30,340 - DEBUG - End of factorial(5%) |
| 49 | + 2016-11-15 16:17:30,340 - DEBUG - End of program |
| 50 | + 0 |
| 51 | + |
| 52 | +Dessa forma, podemos ver o passo a passo que o programa está realizando e identificar onde está o erro. No caso, vemos que para corrigir o problema, devemos alterar o **for i in range(n+1):** para **for i in range(1, n+1):**. |
| 53 | +Quando o desenvolvedor não quiser mais visualizar as mensagens de logging, basta chamar **logging.disable(logging.CRITICAL)** logo embaixo do **import logging**. Essa função faz com que não seja necessário alterar o programa removendo todas as chamadas de logging manualmente. |
| 54 | + |
| 55 | +Também é possível gravar as mensagens de log num arquivo, ao invés de mostrá-las na tela. A função aceita o argumento **filename**. |
| 56 | + |
| 57 | + :::python |
| 58 | + import logging |
| 59 | + logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') |
| 60 | + |
| 61 | + |
| 62 | +Lado negativo do uso dessa função: a leitura do código fica difícil, por causa desse monte de logging.debug no meio do código. Para evitar isso, pode-se usar um decorator. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments