easyctf-2017/server/api/logger.py

34 lines
841 B
Python
Raw Normal View History

2015-12-27 01:21:15 +00:00
import datetime
import logging
import logging.handlers
import os
2016-01-13 04:41:59 +00:00
import pkgutil
import api
2015-12-27 01:21:15 +00:00
NOTSET = 0
DEBUG = 10
INFO = 20
WARNING = 30
ERROR = 40
CRITICAL = 50
def initialize_logs():
2016-01-13 04:41:59 +00:00
def create_logger(name):
new_logger = logging.getLogger(name)
new_logger.setLevel(logging.INFO)
base = os.path.dirname(__file__).strip("api")
log_path = os.path.join(base, "logs")
if not os.path.exists(log_path):
os.mkdir(log_path)
log_handler = logging.handlers.RotatingFileHandler(os.path.join(log_path, name + ".log"))
new_logger.addHandler(log_handler)
for importer, modname, ispkg in pkgutil.walk_packages(path="../api"):
create_logger(modname)
def log(logname, level, message):
logger = logging.getLogger(logname)
message = "[%s] %s" % (datetime.datetime.now().strftime("%m/%d/%Y %X"), message)
logger.log(level, message)