Esempio n. 1
0
	def test_parse(self):
		"""Check unit parsing"""

		for s in ['5 kilobytes', '5 KiloByte', '5kb', '5KB']:
			self.assertEqual((5000L, 'byte_decimal'), unitconvert.convert_from(s))

		for s in ['5 kibibytes', '5KiBIByte', '5kib', '5KiB']:
			self.assertEqual((5120L, 'byte_binary'), unitconvert.convert_from(s))
Esempio n. 2
0
def logger(subname, **kwargs):
    global LOGGER, HANDLER

    if not kwargs.get("init", False):
        return LOGGER

    if HANDLER != None:
        LOGGER.removeHandler(HANDLER)

    conf = config.read()
    log_type = kwargs.get("type", conf["logging"]["type"]) or "syslog"
    log_level = log_level_mapping[kwargs.get("level", conf["logging"]["level"]).lower()]

    if log_type == "file":
        name = kwargs.get("name", "billing")
        log_file = kwargs.get("log_file", conf["logging"]["logdir"]) + "/" + name + ".log"
        log_max_bytes = unitconvert.convert_from(conf["logging"]["logsize"])
        log_count = conf["logging"]["backcount"]

        HANDLER = logging.handlers.RotatingFileHandler(log_file, maxBytes=log_max_bytes, backupCount=log_count)

    elif log_type == "syslog":
        log_address = kwargs.get("address", str(conf["logging"]["address"]))
        log_facility = kwargs.get("facility", conf["logging"]["facility"])

        HANDLER = SysLogHandler(address=log_address, facility=SysLogHandler.facility_names[log_facility])

    elif log_type == "stderr":
        HANDLER = logging.StreamHandler(sys.stderr)

        # LOGGER = logging.getLogger()
    LOGGER.setLevel(log_level)

    HANDLER.setFormatter(
        logging.Formatter(kwargs.get("log_format", default_log_format), kwargs.get("date_format", default_date_format))
    )

    LOGGER.addHandler(HANDLER)

    return LOGGER