Exemplo n.º 1
0
    def test_get_config_is_configured(self):
        """
        Return value of structlog.get_config() works as input for
        structlog.configure(). is_configured() reflects the state of
        configuration.
        """
        assert False is structlog.is_configured()

        structlog.configure(**structlog.get_config())

        assert True is structlog.is_configured()

        structlog.reset_defaults()

        assert False is structlog.is_configured()
Exemplo n.º 2
0
    def test_get_config_is_configured(self):
        """
        Return value of structlog.get_config() works as input for
        structlog.configure(). is_configured() reflects the state of
        configuration.
        """
        assert False is structlog.is_configured()

        structlog.configure(**structlog.get_config())

        assert True is structlog.is_configured()

        structlog.reset_defaults()

        assert False is structlog.is_configured()
Exemplo n.º 3
0
def get_logger(name: Optional[str] = None) -> Any:
    """
    Convenience function that returns a logger

    Returns: A proxy that creates a correctly configured logger bound to
    the __name__ of the calling module

    """
    del name
    caller_name = get_caller_name_from_frames()
    if not _CONFIGS.stdlib_logging_config_already_configured:
        set_logging_config(caller_name)
        _CONFIGS.stdlib_logging_config_already_configured = True
    if not structlog.is_configured():
        if (
            _feature_flags.is_prettified_output_formatting_requested()
            or _feature_flags.is_stdlib_based_structlog_configuration_requested()
        ):
            set_stdlib_based_structlog_config()
        else:
            set_optimized_structlog_config()
    logger = structlog.get_logger(caller_name).bind(logger=caller_name)
    if hasattr(logger, "setLevel"):  # stdlib-based logger
        logger.setLevel(_LOG_LEVEL)
    return logger
Exemplo n.º 4
0
    def load(cls, env_prefix: str = 'TG_ODESLI_BOT_'):
        """Load config merging default variables and environment variables.

        :param env_prefix: prefix for environment variables
        :return: filled config object
        """
        config = cls()
        # Load environment from .env file
        dotenv.load_dotenv()
        # Update config object with environment variables
        for env_var_name, value in os.environ.items():
            if env_var_name.startswith(env_prefix):
                var_name = env_var_name[len(env_prefix):]
                # Do not override config vars in testing mode
                if hasattr(config, 'TESTING') and hasattr(config, var_name):
                    continue
                setattr(config, var_name, value)
        if config.SENTRY_DSN:
            sentry_sdk.init(
                dsn=config.SENTRY_DSN,
                integrations=[AioHttpIntegration()],
                environment=config.SENTRY_ENVIRONMENT,
            )
        if not structlog.is_configured():
            config.init_logging()
        return config
Exemplo n.º 5
0
def configure_logging(
    dict_config: Dict = DEFAULT_CONFIG, file_config: Optional[Path] = None
) -> None:
    """Configures logging for the application.

    This is usually called by valiant.config.Config.__init__

    If a file_config is provided, the dict_config is ignored.

    If logging has already been configured further calls to this function will
    just be silently ignored.

    See: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig

    Args:
        dict_config: A dictionary acceptable to logging.config.dictConfig
        file_config: Path to a logging config file - see
            https://docs.python.org/3/library/logging.config.html#logging-config-fileformat

    Raises:
        ValueError: When the file provided in file_config does not exist.
    """
    from copy import deepcopy
    import structlog
    from structlog.stdlib import LoggerFactory

    if structlog.is_configured():
        return

    if file_config:
        if not file_config.exists():
            raise ValueError(
                (
                    f"The logging configuration file does not exist: {file_config}."
                    f" CWD is {Path.cwd()}."
                )
            )
        fileConfig(file_config)
    else:
        dictConfig(deepcopy(dict_config))

    # See: http://www.structlog.org/en/stable/standard-library.html#rendering-within-structlog
    structlog.configure_once(
        processors=[
            structlog.stdlib.filter_by_level,
            structlog.stdlib.add_logger_name,
            structlog.stdlib.add_log_level,
            structlog.stdlib.PositionalArgumentsFormatter(),
            structlog.processors.TimeStamper(fmt="iso"),
            structlog.processors.StackInfoRenderer(),
            structlog.processors.format_exc_info,
            structlog.processors.UnicodeDecoder(),
            structlog.processors.JSONRenderer(),
        ],
        context_class=dict,
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True,
    )
Exemplo n.º 6
0
def getLogger(logger=None):
    if not logger:
        logger = logging.getLogger()

    if not structlog.is_configured():
        configure_structlog()

    logger = structlog.wrap_logger(logger=logger)
    return logger
Exemplo n.º 7
0
def init_app(app, app_config: Config = None):
    _suppress_warnings()

    if app_config is None:
        app_config = _config_by_environment(app.env)

    app.config.from_object(app_config)
    if not structlog.is_configured():
        from property_app.logging import initialize_logging

        initialize_logging()
Exemplo n.º 8
0
    def get_logger(self):
        if not structlog.is_configured():
            structlog.configure(
                processors=self.chain,
                context_class=structlog.threadlocal.wrap_dict(dict),
                logger_factory=structlog.stdlib.LoggerFactory(),
                wrapper_class=structlog.stdlib.BoundLogger,
                cache_logger_on_first_use=True,
            )

        logger = structlog.get_logger(self.service_name).new()
        return logger.bind(**self.initial_values)
    def init_app(self, app: Flask) -> None:
        """init_app

        Patches the builtin Flask logger and replaces it with a structlog logger.

        :param app:
            The Flask app for which to patch the logger.
        """
        if not structlog.is_configured():
            app.logger.warning(
                'The structlog module is not configured yet. Refusing to patch the Flask logger.'
            )
            return None

        self._patch_flask_logger(app)
Exemplo n.º 10
0
import re
from collections import OrderedDict

import cachetools
import more_itertools
import requests
import structlog

from greynoise.__version__ import __version__
from greynoise.api.analyzer import Analyzer
from greynoise.api.filter import Filter
from greynoise.exceptions import RateLimitError, RequestFailure
from greynoise.util import configure_logging, load_config, validate_ip

if not structlog.is_configured():
    configure_logging()
LOGGER = structlog.get_logger()


def initialize_cache(cache_max_size, cache_ttl):
    """A function to initialize cache"""
    cache = cachetools.TTLCache(maxsize=cache_max_size, ttl=cache_ttl)
    return cache


class GreyNoise(object):

    """GreyNoise API client.

    :param api_key: Key use to access the API.
 def __init__(self):
     if not structlog.is_configured():
         configure_structlog()
 def __init__(self, get_response):
     self.get_response = get_response
     if not structlog.is_configured():
         configure_structlog()
Exemplo n.º 13
0
 def __init__(self, app):
     self.app = app
     if not structlog.is_configured():
         configure_structlog()
Exemplo n.º 14
0
def main():
    """GreyNoise CLI."""
    if not structlog.is_configured():
        configure_logging()