#!/usr/bin/python -u ''' -------------------------------------------------------------------------------- Type: Python 3.x module Author: Milan Toman (milan.v.toman@gmail.com) Description: Logger module, writes debug, logs and warning into ./log dir Used many times, doesn't change, let us create a module. Module is not ready, needs work. TODO: -------------------------------------------------------------------------------- Import libraries -------------------------------------------------------------------------------- ''' import os import datetime import logging class ventilLogger(object): def __init__(self): __name__ = 'Zi logger! By Ventil!!!' def all_other_shit(self): # Check log directory and create if non-existent if os.path.isdir(_LOG_DIR): # print "INFO: Log directory \"{}\" exists.".format(_LOG_DIR) files = os.listdir(_LOG_DIR) logfile_dict = {} for file in files: if _LOG_FILE_ROOT in file: file_path = os.path.join(_LOG_DIR, file) file_stats = os.stat(file_path) file_mtime = file_stats.st_mtime """ if datetime.datetime.now() - \ file_stats.st_mtime > datetime.timedelta(hours=24) """ try: logfile_dict.update({file_path: file_mtime}) except: logfile_dict = {file_path: file_mtime} else: pass sorted_list_keys = sorted(logfile_dict, key=logfile_dict.get) # select the last 30 log files to keep, delete the rest. files_to_keep = sorted_list_keys[-30:] for filename in sorted_list_keys: if filename not in files_to_keep: #print("Deleting {}".format(filename)) os.remove(filename) else: #print("Not deleting {}".format(filename)) pass else: try: os.mkdir(_LOG_DIR) # print "INFO: Created logging directory \"{}\"".format(_LOG_DIR) except () as error: print(u"FATAL: Unable to create " +\ u"logging directory \"{}\"".format(_LOG_DIR)) raise SystemError(u"Unable to create log directory %s", error) # Check for previous logs and rename if any if os.path.isfile(_LOG_FILE): timestapmp_logfile = os.path.getmtime(_LOG_FILE) date_logfile = datetime.datetime.fromtimestamp(timestapmp_logfile) _LOG_RENAME = _LOG_FILE + "." + date_logfile.strftime("%Y%m%d%H%M%S") os.rename(_LOG_FILE, _LOG_RENAME) if os.path.isfile(_DEBUG_FILE): timestapmp_logfile = os.path.getmtime(_DEBUG_FILE) date_logfile = datetime.datetime.fromtimestamp(timestapmp_logfile) _DEBUG_RENAME = _DEBUG_FILE + "." + date_logfile.strftime("%Y%m%d%H%M%S") os.rename(_DEBUG_FILE, _DEBUG_RENAME) # Cleanup if more than _MAX_LOGS / _MAX_LOGS_SIZE logs are present # TODO # Setup formatting _basic_format = "%(asctime)s %(name)s %(levelname)s %(message)s" _basic_formatter = logging.Formatter(_basic_format) _debug_format = "%(asctime)s %(name)s[%(process)d] \ (%(funcName)s) %(levelname)s %(message)s" _debug_formatter = logging.Formatter(_debug_format) _console_format = "%(name)s %(levelname)s: %(message)s" _console_formatter = logging.Formatter(_console_format) # Make logging readable with module hierarchy logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # Setting up handlers for stdout / file logging and debug # Logfile basic_handler = logging.FileHandler(_LOG_FILE) basic_handler.setLevel(logging.ERROR) basic_handler.setFormatter(_basic_formatter) logger.addHandler(basic_handler) # Debug file debug_handler = logging.FileHandler(_DEBUG_FILE) debug_handler.setLevel(logging.DEBUG) debug_handler.setFormatter(_debug_formatter) logger.addHandler(debug_handler) # Console console_handler = logging.StreamHandler() console_handler.setLevel(logging.CRITICAL) console_handler.setFormatter(_console_formatter) logger.addHandler(console_handler) # Just for debugging # print _LOG_FILE, _DEBUG_FILE # logger.debug(u'debug message') # logger.info(u'info message') # logger.warn(u'warn message') # logger.error(u'error message') # logger.critical(u'critical message')