Exemplo n.º 1
0
 def __init__(self, consumer_key, consumer_secret, access_key,
              access_secret):
     super(TwitterHandler, self).__init__()
     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
     auth.set_access_token(access_key, access_secret)
     self.api = tweepy.API(auth)
     Handler.setFormatter(self, TwitterFormatter())
def configure_logging(
    stderr_handler: logging.Handler,
    quiet: bool,
    debug: bool,
    enable_color: bool,
    timestamps: bool,
    base_logger: logging.Logger = _logger,
) -> None:
    """Configure logging."""
    rdflib_logger = logging.getLogger("rdflib.term")
    rdflib_logger.addHandler(stderr_handler)
    rdflib_logger.setLevel(logging.ERROR)
    if quiet:
        # Silence STDERR, not an eventual provenance log file
        stderr_handler.setLevel(logging.WARN)
    if debug:
        # Increase to debug for both stderr and provenance log file
        base_logger.setLevel(logging.DEBUG)
        stderr_handler.setLevel(logging.DEBUG)
        rdflib_logger.setLevel(logging.DEBUG)
    fmtclass = coloredlogs.ColoredFormatter if enable_color else logging.Formatter
    formatter = fmtclass("%(levelname)s %(message)s")
    if timestamps:
        formatter = fmtclass(
            "[%(asctime)s] %(levelname)s %(message)s", "%Y-%m-%d %H:%M:%S"
        )
    stderr_handler.setFormatter(formatter)
Exemplo n.º 3
0
    def update_logger(self
            , new_logging_level: int
            , new_logging_handler: lg.Handler = None
            , new_logging_formatter: lg.Formatter = None
            ) -> LoggableObject:
        current_logger = self.logger(extend_logger_name = False)

        if new_logging_level is not None:
            current_logger.setLevel(new_logging_level)

        if new_logging_handler is not None:
            if new_logging_level is not None:
                new_logging_handler.setLevel(new_logging_level)
            if new_logging_formatter is not None:
                new_logging_handler.setFormatter(new_logging_formatter)

            for h in current_logger.handlers:
                if str(h) == str(new_logging_handler):
                    current_logger.removeHandler(h)
                    current_logger.addHandler(new_logging_handler)
                    break
            else:
                current_logger.addHandler(new_logging_handler)

        return self
Exemplo n.º 4
0
def _register_handler(handler: Handler, level: int, tag: str = '_default_') -> None:
    assert tag not in handlers
    handlers[tag] = handler
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)
    logger.setLevel(level)
Exemplo n.º 5
0
def setup_logger(name: Optional[str] = 'default',
                 handler: logging.Handler = None) -> None:
    if handler is None:
        handler = logging.StreamHandler()
    fmt = logging.Formatter("{asctime} [{levelname}]: {message}", style='{')
    handler.setFormatter(fmt)
    logger = logging.getLogger(name)
    logger.addHandler(handler)
Exemplo n.º 6
0
 def _set_formatter(self, handler: logging.Handler, formatter_class: type,
                    formatter: Union[str, dict]):
     """
     Set the formatter with the handler and formatter class specified
     """
     args = self._get_formatter_args(formatter)
     formatter_object = formatter_class(**args)
     handler.setFormatter(formatter_object)
Exemplo n.º 7
0
def set_logging_formatter(handler: logging.Handler, logging_level):
    if logging_level == logging.DEBUG:
        formatter = logging.Formatter("[%(asctime)s] > [%(threadName)s] > "
                                      "[%(filename)s:%(lineno)d] %(message)s")
    else:
        formatter = logging.Formatter(
            '[%(asctime)s %(levelname)s] %(message)s', "%b %d %H:%M:%S")

    handler.setFormatter(formatter)
Exemplo n.º 8
0
 def __init__(self, username, password):
     Handler.__init__(self)
     self.username = username
     self.screen_name = username
     self.password = password
     self._oauth_token = None
     self._oauth_token_secret = None
     self._consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
     self._client = oauth2.Client(self._consumer)
     Handler.setFormatter(self, TwitterFormatter())
Exemplo n.º 9
0
def get_logger(name: str, log_level: int=logging.INFO,
               handler: logging.Handler=static_handler,
               formatter: logging.Formatter=colored_formatter):
    logger = logging.getLogger(name)
    logger.setLevel(log_level)
    if len(logger.handlers) is 0:
        logger.addHandler(handler)
    if formatter is not None and handler is not static_handler:
        handler.setFormatter(formatter)
    return logger
Exemplo n.º 10
0
def configure_handler(
    handler: logging.Handler,
    level: int,
    formatter: logging.Formatter,
    filter_: logging.Filter = None,
):
    handler.setLevel(level)
    handler.setFormatter(formatter)
    if filter_:
        handler.addFilter(filter_)
    return handler
Exemplo n.º 11
0
 def __init__(self, username, password):
     Handler.__init__(self)
     self.username = username
     self.screen_name = username
     self.password = password
     self._oauth_token = None
     self._oauth_token_secret = None
     self._consumer = oauth2.Consumer(CONSUMER_KEY,
                                      CONSUMER_SECRET)
     self._client = oauth2.Client(self._consumer)
     Handler.setFormatter(self, TwitterFormatter())
Exemplo n.º 12
0
 def add_handler(self, handler: logging.Handler, fmt=None, date_fmt=None):
     """
     Add a new log handler with format handlers.
     :param handler:
     :param fmt:
     :param date_fmt:
     :return:
     """
     fmt = fmt or self.default_fmt
     date_fmt = date_fmt or self.default_date_fmt
     handler.setFormatter(logging.Formatter(fmt=fmt, datefmt=date_fmt, style='{'))
     self._logger.addHandler(handler)
Exemplo n.º 13
0
def register_handler(handler: logging.Handler, logger: logging.Logger, level: int, format_string: str, record_filter: logging.Filter, verbose: bool = True) -> None:
    handler.setLevel(level)
    formatter = logging.Formatter(format_string)
    handler.setFormatter(formatter)
    if record_filter:
        handler.addFilter(record_filter)
    logger.addHandler(handler)
    if verbose:
        print("------------- New Logging Handler ---------------")
        print("Added handler: {}".format(str(handler)))
        print("To Logger: {}".format(str(logger)))
        if record_filter:
            print("With Filter: {}".format(str(record_filter)))
Exemplo n.º 14
0
def set_log_handler_formatter_prefix(handler: logging.Handler,
                                     log_formatter_prefix: str) -> None:
    if handler.formatter:
        if handler.formatter._fmt:
            handler.formatter._fmt = log_formatter_prefix + handler.formatter._fmt
            handler.formatter._style._fmt = log_formatter_prefix + handler.formatter._style._fmt
        else:
            handler.formatter._fmt = log_formatter_prefix + '%(message)s'
            handler.formatter._style._fmt = log_formatter_prefix + '%(message)s'
    else:
        datefmt = default_date_fmt
        formatter = logging.Formatter(log_formatter_prefix + '%(message)s',
                                      datefmt)
        handler.setFormatter(formatter)
Exemplo n.º 15
0
    def add_handler(
        self,
        name: str,
        handler: python_logging.Handler,
        formatter: Optional[python_logging.Formatter] = None,
    ) -> None:
        """Add handler and optional formatter to a module name.

        Args:
            name: module name
            handler: Logging Handler
            formatter: Logging Formatter
        """
        logger = python_logging.getLogger(name)
        if formatter is not None:
            handler.setFormatter(formatter)
        logger.addHandler(handler)
Exemplo n.º 16
0
def _add_handler(handler: logging.Handler,
                 logger: logging.Logger = logging.getLogger(),
                 name: str = 'log_handler',
                 level: int = logging.INFO,
                 fmt: str = default_fmt,
                 datefmt: str = default_date_fmt) -> logging.Handler:
    """
    >>> result = set_stream_handler()
    >>> result2 = set_stream_handler()

    """

    handler.addFilter(HostnameFilter())
    fmt = format_fmt(fmt)
    formatter = logging.Formatter(fmt, datefmt)
    handler.setFormatter(formatter)
    handler.setLevel(level)
    handler.name = name
    logger.addHandler(handler)
    return handler
Exemplo n.º 17
0
def add_handler(handler: logging.Handler,
                logger_name: Optional[str] = None,
                level: int = logging.DEBUG,
                log_format: Optional[str] = None):
    """
    Adds a new handler to an existing logger, with the specified formatter
    in ```init_logging```. If a new format is specified (is not None) then
    it will be used for this handler.

    :param handler: the new handler to be added.
    :param logger_name: the logger name to which add the handler.
    :param level: the logging level for that formatter.
    :param log_format: the log format used if not formatter was created.
    """
    logger = logging.getLogger(logger_name)
    fmt = logging.Formatter(log_format) if log_format else logging.Formatter(
        LOG_DEFAULT_FORMAT)
    handler.setFormatter(fmt)
    handler.setLevel(level)

    logger.addHandler(handler)
Exemplo n.º 18
0
def _configureHandler(logger: logging.Logger, handler: logging.Handler,
                      formatter: logging.Formatter,
                      level: typing.Optional[Level]) -> None:
    """Configures the handler

    #. Sets the level to the given one or, if None, to the logger's level
    #. Sets the formatter to the given one
    #. Adds this handler to the logger

    It is called by internally by this package's handler
    """
    if level is None:
        handler.setLevel(logger.level)
    else:
        if isinstance(level, Level):
            level = level.value
        elif isinstance(level, (int, str)):
            handler.setLevel(level)
        else:
            raise TypeError("Wrong level type: %s" % type(level))
    handler.setFormatter(formatter)
    logger.addHandler(handler)
Exemplo n.º 19
0
def configure_logging(
    args: argparse.Namespace,
    stderr_handler: logging.Handler,
    runtimeContext: RuntimeContext,
) -> None:
    rdflib_logger = logging.getLogger("rdflib.term")
    rdflib_logger.addHandler(stderr_handler)
    rdflib_logger.setLevel(logging.ERROR)
    if args.quiet:
        # Silence STDERR, not an eventual provenance log file
        stderr_handler.setLevel(logging.WARN)
    if runtimeContext.debug:
        # Increase to debug for both stderr and provenance log file
        _logger.setLevel(logging.DEBUG)
        stderr_handler.setLevel(logging.DEBUG)
        rdflib_logger.setLevel(logging.DEBUG)
    fmtclass = coloredlogs.ColoredFormatter if args.enable_color else logging.Formatter
    formatter = fmtclass("%(levelname)s %(message)s")
    if args.timestamps:
        formatter = fmtclass("[%(asctime)s] %(levelname)s %(message)s",
                             "%Y-%m-%d %H:%M:%S")
    stderr_handler.setFormatter(formatter)
Exemplo n.º 20
0
    def __init__(self,
                 name: str,
                 strFormat: str,
                 handler: logging.Handler = logging.StreamHandler()):
        """
        Parameters
        ----------
        name: str
            The name to give this logger.
        strFormat: str
            The formatting string to use.
        handler: logging.StreamHandler, optional
            The handler to use.
        """
        self.log: logging.Logger = logging.getLogger(name)
        self.log.setLevel(logging.INFO)

        handler: logging.Handler = handler
        handler.setLevel(logging.INFO)
        handler.setFormatter(logging.Formatter(strFormat))

        self.log.addHandler(handler)
Exemplo n.º 21
0
    def _config_handler(self,
                        handler: logging.Handler,
                        level: Union[str, int] = None,
                        formatter: str = None,
                        set_from_master: bool = False):
        """
        Configure the handler's level and formatter

        :param handler: logging.Handler type object
        :param level: the level of the handler
        :param formatter: the formatter of the handler

        :param set_from_master: Set *level* or *formatter* from obj.master_level and obj.master_formatter

        """
        # Set level
        if level:
            log_level = self._get_level(level)
            handler.setLevel(log_level)
        elif set_from_master:
            handler.setLevel(self.master_level)

        # Set formatter
        if type(handler) == logging.StreamHandler:
            formatter_class = partial(ColorFormatter,
                                      color_config=self.color_config)
        else:
            formatter_class = logging.Formatter

        if formatter:
            formatter_object = formatter_class(formatter, style='{')
            handler.setFormatter(formatter_object)
        elif set_from_master:
            formatter_object = formatter_class(self.master_formatter,
                                               style='{')
            handler.setFormatter(formatter_object)
Exemplo n.º 22
0
def configure_loggers(app: Flask, default_handler: logging.Handler) -> None:
    """
    Adds logging handlers to the app instance and
    configures log formatting.
    :param default_handler:
    :param app: The flask app instance.
    """
    mail_handler = SMTPHandler(mailhost=(app.config["SMTP_SERVER"],
                                         app.config["SMTP_PORT"]),
                               fromaddr=app.config["SMTP_FROM_ADDRESS"],
                               toaddrs=[app.config["ADMIN_EMAIL"]],
                               subject='[Survey Tool] Application Error',
                               credentials=(app.config["SMTP_FROM_ADDRESS"],
                                            app.config["SMTP_PASSWORD"]),
                               secure=())
    log_formatter = LogFormatter(
        '[%(asctime)s] %(levelname)s during %(method)s %(url)s as %(current_user)s@%(remote_addr)s in %(module)s: %(message)s'
    )
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(log_formatter)
    default_handler.setFormatter(log_formatter)
    if not app.config['DEBUG']:
        # only use logging via email for production instances
        app.logger.addHandler(mail_handler)
Exemplo n.º 23
0
def get_logger(name: str = None,
               handler: logging.Handler = None,
               level=logging.INFO,
               formatting: str = DEFAULT_FORMAT_STR,
               propagate: bool = False,
               print_trace_id: bool = True):
    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.propagate = propagate

    if not handler:
        handler = logging.StreamHandler(sys.stdout)
        handler.setLevel(level)

        # If APP_ENV is `test` or unset we use a colorful formatter 🌈.
        # Else we use a plain formatter to avoid passing ANSI color characters
        # into staging/prod env logs
        app_env = os.getenv('APP_ENV', TEST_ENV)
        if app_env == TEST_ENV:
            formatter = ColorfulFormatter(formatting)
        else:
            formatter = logging.Formatter(formatting)

        handler.setFormatter(formatter)
        if logger.hasHandlers():
            # To prevent the same stream handler from being added multiple times to the
            # same logger. If the same handler (stdout in this case) is added multiple
            # times to the same logger then each log will show up more and more times in
            # that stream.
            logger.handlers.clear()

    logger.addHandler(handler)

    if print_trace_id:
        logger = KubricLogAdapter(logger, {})
    return logger
Exemplo n.º 24
0
 def addHandler(self, handler: logging.Handler, handlerLevel: int):
     handler.setLevel(handlerLevel)
     handler.setFormatter(logging.Formatter(self.formatStr))
     self.logger.addHandler(handler)
Exemplo n.º 25
0
 def add_handler(self, ch: logging.Handler, log_format: str):
     formatter = Formatter(log_format)
     formatter.set_formatter(self.formatter)
     ch.setFormatter(formatter)
     self.logger.addHandler(ch)
 def add_handler(self, handler: logging.Handler):
     handler.setFormatter(self.formatter)
     self._logging.addHandler(handler)
Exemplo n.º 27
0
 def _setHandlerDefaults(self, handler: logging.Handler) -> None:
     handler.setLevel(self.level)
     handler.setFormatter(self.formatter)
Exemplo n.º 28
0
 def __init__(self, consumer_key, consumer_secret, access_key, access_secret):
     super(TwitterHandler, self).__init__()
     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
     auth.set_access_token(access_key, access_secret)
     self.api = tweepy.API(auth)
     Handler.setFormatter(self, TwitterFormatter())
Exemplo n.º 29
0
 def add_handler(handler: logging.Handler, level: int) -> None:
     handler.setLevel(level)
     handler.setFormatter(formatter)
     logger.addHandler(handler)
 def setFormatter(self, fmt):
     LogHandler.setFormatter(self, fmt)
     self._handler.setFormatter(fmt)
Exemplo n.º 31
0
 def __add_handler(self, handler: logging.Handler,
                   add_colors: bool) -> NoReturn:
     handler.setLevel(self.log_level)
     handler.setFormatter(self.__get_formatter(add_colors=add_colors))
     self.__logger.addHandler(handler)
Exemplo n.º 32
0
Arquivo: log.py Projeto: liber145/nni
def _setup_logger(name: str, handler: Handler, level: int) -> None:
    handler.setFormatter(formatter)
    logger = logging.getLogger(name)
    logger.addHandler(handler)
    logger.setLevel(level)
    logger.propagate = False