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);