Skip to content

Commit

Permalink
fix(logging): source code citation
Browse files Browse the repository at this point in the history
  • Loading branch information
DiTo97 committed May 23, 2024
1 parent b377467 commit d139480
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions scrapegraphai/utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""A centralized logging system for any library
source code inspired by https://github.com/huggingface/transformers/blob/main/src/transformers/utils/logging.py
source code inspired by https://gist.github.com/DiTo97/9a0377f24236b66134eb96da1ec1693f
"""

import logging
import os
import sys
Expand All @@ -25,16 +26,17 @@ def _set_library_root_logger() -> None:
global _default_handler

with _semaphore:
if _default_handler: return

if _default_handler:
return

_default_handler = logging.StreamHandler() # sys.stderr as stream

# https://github.com/pyinstaller/pyinstaller/issues/7334#issuecomment-1357447176
if sys.stderr is None:
sys.stderr = open(os.devnull, "w")

_default_handler.flush = sys.stderr.flush

library_root_logger = _get_library_root_logger()
library_root_logger.addHandler(_default_handler)
library_root_logger.setLevel(_default_logging_level)
Expand Down Expand Up @@ -74,8 +76,8 @@ def set_verbosity_error() -> None:

def set_verbosity_fatal() -> None:
set_verbosity(logging.FATAL)


def set_handler(handler: logging.Handler) -> None:
_set_library_root_logger()

Expand All @@ -86,31 +88,31 @@ def set_handler(handler: logging.Handler) -> None:

def set_default_handler() -> None:
set_handler(_default_handler)


def unset_handler(handler: logging.Handler) -> None:
_set_library_root_logger()

assert handler is not None

_get_library_root_logger().removeHandler(handler)


def unset_default_handler() -> None:
unset_handler(_default_handler)


def set_propagation() -> None:
_get_library_root_logger().propagate = True


def unset_propagation() -> None:
_get_library_root_logger().propagate = False


def set_formatting() -> None:
"""sets formatting for all handlers bound to the root logger
```
[levelname|filename|line number] time >> message
```
Expand All @@ -121,17 +123,17 @@ def set_formatting() -> None:

for handler in _get_library_root_logger().handlers:
handler.setFormatter(formatter)


def unset_formatting() -> None:
for handler in _get_library_root_logger().handlers:
handler.setFormatter(None)


@lru_cache(None)
def warning_once(self, *args, **kwargs):
"""emits warning logs with the same message only once"""
self.warning(*args, **kwargs)


logging.Logger.warning_once = warning_once
logging.Logger.warning_once = warning_once

0 comments on commit d139480

Please sign in to comment.