My_Logger.py 3.49 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
import logging, logging.handlers
from itertools import count
import Utils

my_counter = count();

class ContextFilter(logging.Filter):
def filter(self, record):
record.count = next(my_counter)
return True

class My_Logger(object):
def __init__(self,properties_file):
self.utils = Utils.Utils(properties_file)
self.pid_file = self.utils.logs_folder_name.replace('logs_','sim_')

#measurements logger
self.meas_logger = logging.getLogger("measurement")
self.meas_logger.setLevel(logging.INFO)
self.meas_logger.addFilter(ContextFilter())
handler = logging.handlers.RotatingFileHandler(
self.utils.install_dir+'/'+ self.pid_file +"/"+self.utils.experiments_logs_folder_name+'/'+self.utils.experiments_logs_name+'/measurements.log', maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(count)s,\"%(asctime)s\",%(name)s,%(message)s")
handler.setFormatter(formatter)
self.meas_logger.addHandler(handler)

#actions logger
self.action_logger = logging.getLogger("action")
self.action_logger.setLevel(logging.INFO)
self.action_logger.addFilter(ContextFilter())
handler = logging.handlers.RotatingFileHandler(
self.utils.install_dir+'/'+ self.pid_file +"/"+self.utils.experiments_logs_folder_name+'/'+self.utils.experiments_logs_name+'/actions.log', maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(count)s,\"%(asctime)s\",%(name)s,%(message)s")
handler.setFormatter(formatter)
self.action_logger.addHandler(handler)

#discarded measurements logger
self.disc_meas_logger = logging.getLogger("discarded")
self.disc_meas_logger.setLevel(logging.INFO)
self.disc_meas_logger.addFilter(ContextFilter())
handler = logging.handlers.RotatingFileHandler(
self.utils.install_dir+'/'+ self.pid_file +"/"+self.utils.experiments_logs_folder_name+'/'+self.utils.experiments_logs_name+'/discarded_measurements.log', maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(count)s,\"%(asctime)s\",%(name)s,%(message)s")
handler.setFormatter(formatter)
self.disc_meas_logger.addHandler(handler)
#failures logger
self.fail_logger = logging.getLogger("failure")
self.fail_logger.setLevel(logging.INFO)
self.fail_logger.addFilter(ContextFilter())
handler = logging.handlers.RotatingFileHandler(
self.utils.install_dir+'/'+ self.pid_file +"/"+self.utils.experiments_logs_folder_name+'/'+self.utils.experiments_logs_name+'/failure.log', maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(count)s,\"%(asctime)s\",%(name)s,%(message)s")
handler.setFormatter(formatter)
self.fail_logger.addHandler(handler)

#cost logger
self.cost_logger = logging.getLogger("cost")
self.cost_logger.setLevel(logging.INFO)
self.cost_logger.addFilter(ContextFilter())
handler = logging.handlers.RotatingFileHandler(
self.utils.install_dir+'/'+ self.pid_file +"/"+self.utils.experiments_logs_folder_name+'/'+self.utils.experiments_logs_name+'/cost.log', maxBytes=10*1024*1024, backupCount=5)
formatter = logging.Formatter("%(count)s,\"%(asctime)s\",%(name)s, %(message)s")
handler.setFormatter(formatter)
self.cost_logger.addHandler(handler)


def log_measurement(self,message):
self.meas_logger.info(message);

def log_discarded_measurement(self,message):
self.disc_meas_logger.info(message);

def log_action(self,message):
self.action_logger.info(message);
def log_failure(self,message):
self.fail_logger.info(message);

def log_cost(self,message):
self.cost_logger.info(message);