def _checkConfig(self): if not super(CFileRotatHandlerConfig, self)._checkConfig(): return False if len(self._filename) == 0: printerr("file name empty") return False return True
def _checkConfig(self): if not super(CRotatingFileHandlerConfig, self)._checkConfig(): return False if self._maxbytes <= 0: printerr("maxbytes should above zero") return False return True
def _checkConfig(self): if not super(CNatureTimedRotatingFileHandlerConfig, self)._checkConfig(): return False if not self._when.upper() in set(['S', 'M', 'H', 'D', 'MIDNIGHT']): printerr("invalid rotat log param %s" % self._when) return False return True
def initLog(config, logmodule="sys"): "init a log for use" if not isinstance(config, CLogConfig): printerr( "InitLog Invalid parameter, need parameter CLogConfig buf now %s" % type(config)) return None if not config.checkConfig(): printerr("config.checkConfig() failed") return None logger = logging.getLogger(logmodule) logger.setLevel(config._level) handler = RotatingFileHandler(config._path, maxBytes=4 * 1024 * 1024 * 1024, backupCount=100) if 'file' != config._handler: handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter(config._formatter)) logger.addHandler(handler) return logger
def _checkConfig(self): if not self._level in LEVELS.values(): printerr("level %s illegal" % self._level) return False if not self._formatter in LOGFORMATTER.values(): printerr("log formatter:%s not support" % self._formatter) return False return True
def demo(filename, sep): f = open(filename, 'r') title = f.readline() fields_map = ParseTitle(title, sep) if None == fields_map: commfunc.printerr("parsetitle failed") sys.exit(-1) for l in f: fields = ParseLine(l, fields_map, sep) if None == fields: commfunc.printerr("parseline fields failed") sys.exit(-1) print fields sys.exit(0)
def ParseLine(line, fields_map={}, sep=""): if len(line) == 0: return None if line[-1] == os.linesep: line_array = line[:-1].split(sep) else: line_array = line.split(sep) array_len = len(line_array) if len(line_array) == 0: commfunc.printerr("line:%s, sep:%s split failed" % (line, sep)) return None if len(fields_map) == 0: commfunc.printerr("field_map empty") return None line_dict = {} for k, v in fields_map.items(): if v >= array_len: commfunc.printerr( 'index out of range: line:%s, field:%s field index:%d' % (line, k, v)) return None line_dict[k] = line_array[v] return line_dict
def ParseLine(line, fields_map={}, sep=""): if len(line) == 0: return None if line[-1] == os.linesep: line_array = line[:-1].split(sep) else: line_array = line.split(sep) array_len = len(line_array) if len(line_array) == 0: commfunc.printerr("line:%s, sep:%s split failed" % (line, sep)) return None if len(fields_map) == 0: commfunc.printerr("field_map empty") return None line_dict = {} for k, v in fields_map.items(): if v >= array_len: commfunc.printerr('index out of range: line:%s, field:%s field index:%d' % (line, k, v)) return None line_dict[k] = line_array[v] return line_dict
def initLog(config, logmodule="sys"): "init a log for use" if not isinstance(config, CLogConfig): printerr("InitLog Invalid parameter, need parameter CLogConfig buf now %s" % type(config)) return None if not config.checkConfig(): printerr("config.checkConfig() failed") return None logger = logging.getLogger(logmodule) logger.setLevel(config._level) handler = RotatingFileHandler(config._path, maxBytes=4 * 1024 * 1024 * 1024, backupCount=100) if 'file' != config._handler: handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter(config._formatter)) logger.addHandler(handler) return logger
kv_dict[k] = v return kv_dict def demo(filename, sep): f = open(filename, 'r') title = f.readline() fields_map = ParseTitle(title, sep) if None == fields_map: commfunc.printerr("parsetitle failed") sys.exit(-1) for l in f: fields = ParseLine(l, fields_map, sep) if None == fields: commfunc.printerr("parseline fields failed") sys.exit(-1) print fields sys.exit(0) if "__main__" == __name__: if len(sys.argv) != 2: commfunc.printerr("please use like %s full_filepath" % sys.argv[0]) sys.exit(-1) filename = sys.argv[1] demo(filename, '\t')