Commit deffe59dc571906f6325ef0e2610f318669fd6ee
1 parent
925b2e48
Exists in
master
Insere função log de eventos
Showing
2 changed files
with
89 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +[2015-04-17 02:57:36.024] INFO: 12 | |
| 2 | +[2015-04-17 02:59:01.672] INFO: 12 | |
| 3 | +[2015-04-17 02:59:04.960] WARNING: 12 | |
| 4 | +[2015-04-17 02:59:11.521] ERROR: 12 | |
| 5 | +[2015-04-17 02:59:15.105] CRITICAL: 12 | |
| 6 | +[2015-04-17 02:59:30.056] CRITICAL: 12 | |
| 7 | +[2015-04-17 02:59:35.104] ERROR: 12 | |
| 8 | +[2015-04-17 03:01:28.624] INFO: This is a messageInfo | |
| 9 | +[2015-04-17 03:01:28.624] WARNING: This is a messageWarn | |
| 10 | +[2015-04-17 03:01:28.625] ERROR: This is a messageError | |
| 11 | +[2015-04-17 03:01:28.625] CRITICAL: This is a messageCritical | |
| 12 | +[2015-04-17 03:02:18.703] INFO: This is a messageInfo | |
| 13 | +[2015-04-17 03:02:18.704] WARNING: This is a messageWarn | |
| 14 | +[2015-04-17 03:02:18.704] ERROR: This is a messageError | |
| 15 | +[2015-04-17 03:02:18.705] CRITICAL: This is a messageCritical | |
| 16 | +[2015-04-17 03:02:37.855] INFO: This is a messageInfo | |
| 17 | +[2015-04-17 03:02:37.856] WARNING: This is a messageWarn | |
| 18 | +[2015-04-17 03:02:37.856] ERROR: This is a messageError | |
| 19 | +[2015-04-17 03:02:37.856] CRITICAL: This is a messageCritical | |
| 20 | +[2015-04-17 03:02:56.543] INFO: This is a messageInfo | |
| 21 | +[2015-04-17 03:02:56.543] WARNING: This is a messageWarn | |
| 22 | +[2015-04-17 03:02:56.544] ERROR: This is a messageError | |
| 23 | +[2015-04-17 03:02:56.544] CRITICAL: This is a messageCritical | |
| 24 | +[2015-04-17 03:02:57.951] INFO: This is a messageInfo | |
| 25 | +[2015-04-17 03:02:57.952] WARNING: This is a messageWarn | |
| 26 | +[2015-04-17 03:02:57.952] ERROR: This is a messageError | |
| 27 | +[2015-04-17 03:02:57.952] CRITICAL: This is a messageCritical | ... | ... |
pyutil.py
| 1 | 1 | # -*- coding: UTF-8 -*- |
| 2 | 2 | |
| 3 | +# @def Função para obter data e hora atual do sistema | |
| 4 | +# @param string Formato de data e hora | |
| 5 | +# @return string Retorna data e hora do sistema no momento da chamada | |
| 6 | +def getTimeStamp(dtFrmt='%Y-%m-%d_%H.%M.%S.%f'): | |
| 7 | + from datetime import datetime | |
| 8 | + if (dtFrmt == '%Y-%m-%d_%H.%M.%S.%f'): | |
| 9 | + # [:-3] Remove 3 casas decimais dos milisegundos (ms) | |
| 10 | + return datetime.now().strftime(dtFrmt)[:-3] | |
| 11 | + else: | |
| 12 | + return datetime.now().strftime(dtFrmt) | |
| 13 | + | |
| 14 | +# @def Função para gravar log dos eventos em arquivo | |
| 15 | +# @param string Mensagem a ser salva | |
| 16 | +# @param int indice do tipo de log 1: Debug, 2: Info, 3: Warn, 4: Error, 5: Critical | |
| 17 | +# @param String Caminho completo do arquivo de logs | |
| 18 | +# @param String Formato de tempo utilizado | |
| 19 | +def log(logMsg="", logLevel=2, logFile="events.log", dtFrmt='%Y-%m-%d %H:%M:%S'): | |
| 20 | + import logging | |
| 21 | + | |
| 22 | + logLevelArray = ["", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" ] | |
| 23 | + logLevel %= len(logLevelArray) | |
| 24 | + print("[%s] log %s: %s" % (getTimeStamp(), logLevelArray[logLevel], logMsg)) | |
| 25 | + | |
| 26 | + #logFormat='[%(asctime)s.%(msecs).03d] %(levelname)s: <User: %(name)s> <Module: %(module)s> <Function: %(funcName)s>: %(message)s' | |
| 27 | + logFormat='[%(asctime)s.%(msecs).03d] %(levelname)s: %(message)s' | |
| 28 | + | |
| 29 | + if (logLevel == 1): | |
| 30 | + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.DEBUG) | |
| 31 | + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') | |
| 32 | + logging.debug(logMsg) | |
| 33 | + | |
| 34 | + elif (logLevel == 2): | |
| 35 | + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.INFO) | |
| 36 | + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') | |
| 37 | + logging.info(logMsg) | |
| 38 | + | |
| 39 | + elif (logLevel == 3): | |
| 40 | + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.WARN) | |
| 41 | + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') | |
| 42 | + logging.warn(logMsg) | |
| 43 | + | |
| 44 | + elif (logLevel == 4): | |
| 45 | + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.ERROR) | |
| 46 | + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') | |
| 47 | + logging.error(logMsg) | |
| 48 | + | |
| 49 | + elif (logLevel == 5): | |
| 50 | + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.CRITICAL) | |
| 51 | + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') | |
| 52 | + logging.critical(logMsg) | |
| 53 | + | |
| 54 | +def test_log(): | |
| 55 | + msg = " This is a message " | |
| 56 | + log(msg + "Debug", 1) | |
| 57 | + log(msg + "Info", 2) | |
| 58 | + log(msg + "Warn", 3) | |
| 59 | + log(msg + "Error", 4) | |
| 60 | + log(msg + "Critical", 5) | |
| 61 | + log(msg + "Critical", 6) | |
| 62 | + | |
| 63 | +# test_log() | |
| 64 | + | |
| 3 | 65 | def printStackTrace(filename): |
| 4 | 66 | from sys import exc_info |
| 5 | 67 | from os.path import basename | ... | ... |