How to use the tomodachi.helpers.logging function in tomodachi

To help you get started, weโ€™ve selected a few tomodachi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kalaspuff / tomodachi / tomodachi / helpers / logging.py View on Github external
if not [x for x in logger.handlers if isinstance(x, CustomServiceLogHandler) and (level is None or level == x.level)]:
        try:
            wfh = CustomServiceLogHandler(filename=filename)
        except FileNotFoundError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid path ("{}")'.format(filename))
            raise e
        except PermissionError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid permissions ("{}")'.format(filename))
            raise e

        if level:
            wfh.setLevel(level)

        if formatter and type(formatter) is str:
            formatter = logging.Formatter(str(formatter))
        if formatter and type(formatter) is bool and formatter is True:
            formatter = logging.Formatter('%(asctime)s (%(name)s): %(message)s')

        if formatter and isinstance(formatter, logging.Formatter):
            wfh.setFormatter(formatter)

        logger.addHandler(wfh)

    return logger
github kalaspuff / tomodachi / tomodachi / helpers / logging.py View on Github external
def log_setup(service: Any, name: Optional[str] = None, level: Optional[Union[str, int]] = None, formatter: Optional[Union[logging.Formatter, str, bool]] = True, filename: Optional[str] = None) -> logging.Logger:
    if not name:
        name = 'log.{}'.format(service.name)
    if not filename:
        raise Exception('log_filename must be specified for logging setup')

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    if level and type(level) is str:
        level = getattr(logging, str(level))

    if not [x for x in logger.handlers if isinstance(x, CustomServiceLogHandler) and (level is None or level == x.level)]:
        try:
            wfh = CustomServiceLogHandler(filename=filename)
        except FileNotFoundError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid path ("{}")'.format(filename))
            raise e
        except PermissionError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid permissions ("{}")'.format(filename))
            raise e

        if level:
github kalaspuff / tomodachi / tomodachi / __init__.py View on Github external
def service(cls: Any) -> Any:
    setattr(cls, CLASS_ATTRIBUTE, True)
    if not getattr(cls, 'log', None):
        cls.log = tomodachi.helpers.logging.log
    if not getattr(cls, 'log_setup', None):
        cls.log_setup = tomodachi.helpers.logging.log_setup
    return cls
github kalaspuff / tomodachi / tomodachi / __init__.py View on Github external
def service(cls: Any) -> Any:
    setattr(cls, CLASS_ATTRIBUTE, True)
    if not getattr(cls, 'log', None):
        cls.log = tomodachi.helpers.logging.log
    if not getattr(cls, 'log_setup', None):
        cls.log_setup = tomodachi.helpers.logging.log_setup
    return cls
github kalaspuff / tomodachi / tomodachi / __init__.py View on Github external
_services = {}
_current_service = {}


def service(cls: Any) -> Any:
    setattr(cls, CLASS_ATTRIBUTE, True)
    if not getattr(cls, 'log', None):
        cls.log = tomodachi.helpers.logging.log
    if not getattr(cls, 'log_setup', None):
        cls.log_setup = tomodachi.helpers.logging.log_setup
    return cls


class Service(object):
    TOMODACHI_SERVICE_CLASS = True
    log = tomodachi.helpers.logging.log
    log_setup = tomodachi.helpers.logging.log_setup


def set_service(name: str, instance: Any) -> None:
    _services[name] = instance
    _current_service[0] = instance


def get_service(name: Optional[str] = None) -> Any:
    if name is None:
        if _current_service and len(_current_service):
            return _current_service[0]

        for k, v in _services.items():
            name = k
            break
github kalaspuff / tomodachi / tomodachi / __init__.py View on Github external
_current_service = {}


def service(cls: Any) -> Any:
    setattr(cls, CLASS_ATTRIBUTE, True)
    if not getattr(cls, 'log', None):
        cls.log = tomodachi.helpers.logging.log
    if not getattr(cls, 'log_setup', None):
        cls.log_setup = tomodachi.helpers.logging.log_setup
    return cls


class Service(object):
    TOMODACHI_SERVICE_CLASS = True
    log = tomodachi.helpers.logging.log
    log_setup = tomodachi.helpers.logging.log_setup


def set_service(name: str, instance: Any) -> None:
    _services[name] = instance
    _current_service[0] = instance


def get_service(name: Optional[str] = None) -> Any:
    if name is None:
        if _current_service and len(_current_service):
            return _current_service[0]

        for k, v in _services.items():
            name = k
            break