""" Small classes to assist with timezone calculations, LOGGER configuration, and other common operations. """ import datetime import pytz import logging LOGGER = logging.getLogger('QSimLogger') def parse_date(dt_str): """parse strings according to the same format as generated by format_date""" if(dt_str == None): return None parts = dt_str.split(".") dt = datetime.datetime.strptime(parts[0], '%Y/%m/%d-%H:%M:%S').replace(microsecond=int(parts[1]+"000"), tzinfo = pytz.utc) return dt def format_date(dt): """Format the date into a date with millesecond resolution and string/alphabetical sorting that is equivalent to datetime sorting""" if(dt == None): return None dt_str = dt.strftime('%Y/%m/%d-%H:%M:%S') + "." + str(dt.microsecond / 1000) return dt_str def configure_logging(loglevel=logging.DEBUG): """configures qsim.util.LOGGER to write a rotating file (10M per file, 5 files) to /var/log/qsim.log""" LOGGER.setLevel(loglevel) handler = logging.handlers.RotatingFileHandler( "/var/log/qsim/{lfn}.log".format(lfn="qsim"), maxBytes=10*1024*1024, backupCount=5) handler.setFormatter(logging.Formatter( "%(asctime)s %(levelname)s %(filename)s %(funcName)s - %(message)s","%Y-%m-%d %H:%M:%S %Z")) LOGGER.addHandler(handler) LOGGER.info("logging started...")