Ejemplo n.º 1
0
 def flushTo(self, logger: logging.Logger) -> None:
     self.acquire()
     try:
         for record in self.buffer:
             logger.handle(record)
         self.buffer = []
     finally:
         self.release()
Ejemplo n.º 2
0
def intermittent_log(
    logger: logging.Logger,
    line: str,
    frequency: float = 60,
    level: int = logging.INFO,
    negative_level: Optional[int] = None,
    caller_extra_id: Any = None,
    fn_override: Optional[str] = None,
    line_override: Optional[int] = None,
    func_override: Optional[str] = None,
    _caller: Optional[str] = None,
    _last_logged: DefaultDict[Tuple[str, int], float] = defaultdict(float),
    _times_suppressed: DefaultDict[Tuple[str, int], float] = defaultdict(int),
) -> None:
    try:
        caller = None
        if _caller:
            frame_id = _caller, caller_extra_id
        else:
            try:
                caller = inspect.stack()[1]
                frame_id = caller.filename, caller.lineno, caller_extra_id
            except:
                frame_id = "???"

        output = negative_level
        if time.time() - _last_logged[frame_id] > frequency:
            _last_logged[frame_id] = time.time()
            if _times_suppressed[frame_id]:
                line += f" [log suppressed {_times_suppressed[frame_id]} times since last]"
            _times_suppressed[frame_id] = 0
            output = level
        else:
            _times_suppressed[frame_id] += 1
        if output and logger.isEnabledFor(output):
            if caller:
                co = caller.frame.f_code
                fn, lno, func, sinfo = (
                    co.co_filename,
                    caller.frame.f_lineno,
                    co.co_name,
                    None,
                )
                record = logger.makeRecord(
                    logger.name,
                    output,
                    fn_override or str(fn),
                    line_override or lno,
                    line,
                    {},
                    None,
                    func_override or func,
                    None,
                    sinfo,
                )
            else:
                record = logger.makeRecord(
                    logger.name,
                    output,
                    fn_override or "???",
                    line_override or 0,
                    line,
                    {},
                    None,
                    func_override,
                )
            logger.handle(record)
    except:
        # noinspection PyProtectedMember
        logger.log(level, line, ())