def init_logging(logger, set_level = logging.INFO, console = True,log_file_path = None): logger.setLevel(logging.DEBUG) logger.propagate = False # it's parent will not print log (especially when client use a 'root' logger) for h in logger.handlers: logger.removeHandler(h) if console: fh = logging.StreamHandler() fh.setLevel(set_level) formatter = logging.Formatter("[%(levelname)s] %(message)s") fh.setFormatter(formatter) logger.addHandler(fh) if log_file_path: fh = logging.FileHandler(log_file_path) fh.setLevel(set_level) formatter = logging.Formatter("%(asctime)-15s %(levelname)s %(message)s") fh.setFormatter(formatter) logger.addHandler(fh)
def get_cur_info(self): try: raise Exception except: f = sys._getframe(2) moduleName = f.f_code.co_filename funcName = f.f_code.co_name lineNo = f.f_lineno #行号 return ("[%s.%s-%s]" % (moduleName, funcName, lineNo)) def debug(self, msg): self.sm_logger.debug("%s:%s" % (self.get_cur_info(), msg)) if isBAE: sm_logger = BAElogger() else: sm_logger = logging.getLogger("simple_debug") sm_logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() formatter = logging.Formatter( '[%(asctime)s - %(module)s.%(funcName)s-%(lineno)d](%(levelname)s): %(message)s' ) ch.setFormatter(formatter) sm_logger.addHandler(ch)