2014-03-03 01:18:51 +01:00
|
|
|
import logging
|
|
|
|
from logging.handlers import RotatingFileHandler
|
2014-03-03 19:03:06 +01:00
|
|
|
|
|
|
|
class bcolors:
|
|
|
|
DEBUG = '\033[94m'
|
|
|
|
INFO = '\033[95m'
|
|
|
|
SUCCESS = '\033[92m'
|
|
|
|
WARNING = '\033[93m'
|
|
|
|
FAIL = '\033[91m'
|
|
|
|
ENDC = '\033[0m'
|
|
|
|
|
|
|
|
class lvl:
|
|
|
|
NOTSET = 0
|
|
|
|
DEBUG = 10
|
|
|
|
INFO = 20
|
2014-03-03 20:46:54 +01:00
|
|
|
SUCCESS = 25
|
2014-03-03 19:03:06 +01:00
|
|
|
WARNING = 30
|
|
|
|
FAIL = 40
|
2014-03-02 22:57:48 +01:00
|
|
|
|
2014-03-03 19:16:07 +01:00
|
|
|
class SingleLevelFilter(logging.Filter):
|
|
|
|
def __init__(self, passlevel, reject):
|
|
|
|
self.passlevel = passlevel
|
|
|
|
self.reject = reject
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
if self.reject:
|
|
|
|
return (record.levelno != self.passlevel)
|
|
|
|
else:
|
|
|
|
return (record.levelno == self.passlevel)
|
|
|
|
|
2014-03-02 22:57:48 +01:00
|
|
|
class Log(object):
|
2014-03-03 12:10:06 +01:00
|
|
|
def __init__(self) :
|
2014-03-03 13:26:40 +01:00
|
|
|
|
2014-03-03 19:05:14 +01:00
|
|
|
logging.addLevelName(lvl.SUCCESS, "SUCCESS")
|
2014-03-03 13:23:52 +01:00
|
|
|
|
2014-03-03 13:39:07 +01:00
|
|
|
self.logger = logging.getLogger()
|
2014-03-03 12:10:06 +01:00
|
|
|
self.logger.setLevel(logging.DEBUG)
|
2014-03-03 13:13:00 +01:00
|
|
|
formatter = logging.Formatter('%(asctime)-15s :: %(levelname)s :: %(message)s')
|
2014-03-03 13:23:52 +01:00
|
|
|
|
2014-03-03 12:10:06 +01:00
|
|
|
file_handler = RotatingFileHandler('activity.log', 'a', 1000000, 1)
|
|
|
|
file_handler.setLevel(logging.DEBUG)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(file_handler)
|
|
|
|
|
2014-03-03 19:19:42 +01:00
|
|
|
file_handler_warning = RotatingFileHandler('illegal.log', 'a', 1000000, 1)
|
2014-03-03 20:46:54 +01:00
|
|
|
f1 = SingleLevelFilter(logging.WARNING, False)
|
2014-03-03 19:16:07 +01:00
|
|
|
file_handler_warning.addFilter(f1)
|
2014-03-03 20:46:54 +01:00
|
|
|
file_handler_warning.setFormatter(formatter)
|
2014-03-03 19:16:07 +01:00
|
|
|
self.logger.addHandler(file_handler_warning)
|
|
|
|
|
2014-03-03 13:13:00 +01:00
|
|
|
file_handler_error = RotatingFileHandler('error.log', 'a', 1000000, 1)
|
|
|
|
file_handler_error.setLevel(logging.ERROR)
|
|
|
|
file_handler_error.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(file_handler_error)
|
|
|
|
|
2014-03-03 14:09:06 +01:00
|
|
|
steam_handler = logging.StreamHandler()
|
2014-03-03 14:43:59 +01:00
|
|
|
steam_handler.setLevel(logging.NOTSET)
|
2014-03-03 14:12:46 +01:00
|
|
|
self.logger.addHandler(steam_handler)
|
2014-03-03 14:09:06 +01:00
|
|
|
|
2014-03-03 14:43:59 +01:00
|
|
|
|
2014-03-03 12:11:19 +01:00
|
|
|
def printL(self,pMsg,pLvl):
|
2014-03-03 19:03:06 +01:00
|
|
|
if pLvl == lvl.DEBUG :
|
2014-03-03 14:19:26 +01:00
|
|
|
pMsg = bcolors.DEBUG + pMsg + bcolors.ENDC
|
2014-03-03 19:03:06 +01:00
|
|
|
elif pLvl == lvl.INFO :
|
2014-03-03 14:23:29 +01:00
|
|
|
pMsg = bcolors.INFO + pMsg + bcolors.ENDC
|
2014-03-03 19:06:21 +01:00
|
|
|
elif pLvl == lvl.SUCCESS :
|
2014-03-03 14:23:29 +01:00
|
|
|
pMsg = bcolors.SUCCESS + pMsg + bcolors.ENDC
|
2014-03-03 19:03:06 +01:00
|
|
|
elif pLvl == lvl.WARNING :
|
2014-03-03 14:23:29 +01:00
|
|
|
pMsg = bcolors.WARNING + pMsg + bcolors.ENDC
|
2014-03-03 19:03:06 +01:00
|
|
|
elif pLvl == lvl.FAIL :
|
2014-03-03 14:23:29 +01:00
|
|
|
pMsg = bcolors.FAIL + pMsg + bcolors.ENDC
|
2014-03-03 14:09:06 +01:00
|
|
|
self.logger.log(pLvl,pMsg)
|
2014-03-03 14:17:10 +01:00
|
|
|
|
2014-03-03 12:10:06 +01:00
|
|
|
|
2014-03-03 19:03:06 +01:00
|
|
|
|
2014-03-03 12:10:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|