From deffe59dc571906f6325ef0e2610f318669fd6ee Mon Sep 17 00:00:00 2001 From: André Araújo Date: Fri, 17 Apr 2015 03:05:10 +0000 Subject: [PATCH] Insere função log de eventos --- events.log | 27 +++++++++++++++++++++++++++ pyutil.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 0 deletions(-) create mode 100644 events.log diff --git a/events.log b/events.log new file mode 100644 index 0000000..a7ae6e2 --- /dev/null +++ b/events.log @@ -0,0 +1,27 @@ +[2015-04-17 02:57:36.024] INFO: 12 +[2015-04-17 02:59:01.672] INFO: 12 +[2015-04-17 02:59:04.960] WARNING: 12 +[2015-04-17 02:59:11.521] ERROR: 12 +[2015-04-17 02:59:15.105] CRITICAL: 12 +[2015-04-17 02:59:30.056] CRITICAL: 12 +[2015-04-17 02:59:35.104] ERROR: 12 +[2015-04-17 03:01:28.624] INFO: This is a messageInfo +[2015-04-17 03:01:28.624] WARNING: This is a messageWarn +[2015-04-17 03:01:28.625] ERROR: This is a messageError +[2015-04-17 03:01:28.625] CRITICAL: This is a messageCritical +[2015-04-17 03:02:18.703] INFO: This is a messageInfo +[2015-04-17 03:02:18.704] WARNING: This is a messageWarn +[2015-04-17 03:02:18.704] ERROR: This is a messageError +[2015-04-17 03:02:18.705] CRITICAL: This is a messageCritical +[2015-04-17 03:02:37.855] INFO: This is a messageInfo +[2015-04-17 03:02:37.856] WARNING: This is a messageWarn +[2015-04-17 03:02:37.856] ERROR: This is a messageError +[2015-04-17 03:02:37.856] CRITICAL: This is a messageCritical +[2015-04-17 03:02:56.543] INFO: This is a messageInfo +[2015-04-17 03:02:56.543] WARNING: This is a messageWarn +[2015-04-17 03:02:56.544] ERROR: This is a messageError +[2015-04-17 03:02:56.544] CRITICAL: This is a messageCritical +[2015-04-17 03:02:57.951] INFO: This is a messageInfo +[2015-04-17 03:02:57.952] WARNING: This is a messageWarn +[2015-04-17 03:02:57.952] ERROR: This is a messageError +[2015-04-17 03:02:57.952] CRITICAL: This is a messageCritical diff --git a/pyutil.py b/pyutil.py index 7d6f469..18ad9ee 100644 --- a/pyutil.py +++ b/pyutil.py @@ -1,5 +1,67 @@ # -*- coding: UTF-8 -*- +# @def Função para obter data e hora atual do sistema +# @param string Formato de data e hora +# @return string Retorna data e hora do sistema no momento da chamada +def getTimeStamp(dtFrmt='%Y-%m-%d_%H.%M.%S.%f'): + from datetime import datetime + if (dtFrmt == '%Y-%m-%d_%H.%M.%S.%f'): + # [:-3] Remove 3 casas decimais dos milisegundos (ms) + return datetime.now().strftime(dtFrmt)[:-3] + else: + return datetime.now().strftime(dtFrmt) + +# @def Função para gravar log dos eventos em arquivo +# @param string Mensagem a ser salva +# @param int indice do tipo de log 1: Debug, 2: Info, 3: Warn, 4: Error, 5: Critical +# @param String Caminho completo do arquivo de logs +# @param String Formato de tempo utilizado +def log(logMsg="", logLevel=2, logFile="events.log", dtFrmt='%Y-%m-%d %H:%M:%S'): + import logging + + logLevelArray = ["", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL" ] + logLevel %= len(logLevelArray) + print("[%s] log %s: %s" % (getTimeStamp(), logLevelArray[logLevel], logMsg)) + + #logFormat='[%(asctime)s.%(msecs).03d] %(levelname)s: : %(message)s' + logFormat='[%(asctime)s.%(msecs).03d] %(levelname)s: %(message)s' + + if (logLevel == 1): + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.DEBUG) + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') + logging.debug(logMsg) + + elif (logLevel == 2): + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.INFO) + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') + logging.info(logMsg) + + elif (logLevel == 3): + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.WARN) + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') + logging.warn(logMsg) + + elif (logLevel == 4): + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.ERROR) + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') + logging.error(logMsg) + + elif (logLevel == 5): + logging.basicConfig(filename=logFile, datefmt=dtFrmt, format=logFormat, level=logging.CRITICAL) + logging.Formatter(fmt='%(asctime)s',datefmt='%Y/%m/%d,%H:%M:%S.%f') + logging.critical(logMsg) + +def test_log(): + msg = " This is a message " + log(msg + "Debug", 1) + log(msg + "Info", 2) + log(msg + "Warn", 3) + log(msg + "Error", 4) + log(msg + "Critical", 5) + log(msg + "Critical", 6) + +# test_log() + def printStackTrace(filename): from sys import exc_info from os.path import basename -- libgit2 0.21.2