Python flask.current_app.logger() Examples

The following are 30 code examples of flask.current_app.logger(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.current_app , or try the search function .
Example #1
Source File: server.py    From zeus with Apache License 2.0 6 votes vote down vote up
def log_errors(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        async def tmp():
            try:
                return await func(*args, **kwargs)

            except asyncio.CancelledError:
                raise
            except Exception as e:
                current_app.logger.exception(str(e))
                raise

        return tmp()

    return wrapper 
Example #2
Source File: authentication.py    From cloudify-manager with Apache License 2.0 6 votes vote down vote up
def _authenticate_token(self, token):
        """Make sure that the token passed exists, is valid, is not expired,
        and that the user contained within it exists in the DB

        :param token: An authentication token
        :return: A tuple: (A user object, its hashed password)
        """
        self.logger.debug('Authenticating token')
        expired, invalid, user, data, error = \
            user_handler.get_token_status(token)

        if expired:
            raise_unauthorized_user_error('Token is expired')
        elif invalid or (not isinstance(data, list) or len(data) != 2):
            raise_unauthorized_user_error(
                'Authentication token is invalid:\n{0}'.format(error)
            )
        elif not user:
            raise_unauthorized_user_error('No authentication info provided')
        else:
            self._verify_token(token=data[1], user=user)

        return user 
Example #3
Source File: authentication.py    From cloudify-manager with Apache License 2.0 6 votes vote down vote up
def _http_auth(self, user, username, password):
        """Perform basic user authentication
        - Check that the password that was passed in the request can be
          verified against the password stored in the DB

        :param user: The DB user object
        :param username: The username from the request
        :param password: The password from the request
        :return: The DB user object
        """
        self.logger.debug('Running basic HTTP authentication')
        if not user:
            raise_unauthorized_user_error(
                'Authentication failed for '
                '<User username=`{0}`>'.format(username)
            )
        if not verify_password(password, user.password):
            self._increment_failed_logins_counter(user)
            raise_unauthorized_user_error(
                'Authentication failed for {0}.'
                ' Bad credentials or locked account'.format(user)
            )
        return user 
Example #4
Source File: authentication.py    From cloudify-manager with Apache License 2.0 6 votes vote down vote up
def authenticate(self, request):
        user = self._internal_auth(request)
        is_bootstrap_admin = user and user.is_bootstrap_admin
        if self.external_auth_configured \
                and not is_bootstrap_admin \
                and not self.token_based_auth:
            self.logger.debug('using external auth')
            user = user_handler.get_user_from_auth(request.authorization)
            response = self.external_auth.authenticate(request, user)
            if isinstance(response, Response):
                return response
            user = response
        if not user:
            raise_unauthorized_user_error('No authentication info provided')
        self.logger.debug('Authenticated user: {0}'.format(user))

        if request.authorization:
            # Reset the counter only when using basic authentication
            # (User + Password), otherwise the counter will be reset on
            # every UI refresh (every 4 sec) and accounts won't be locked.
            user.failed_logins_counter = 0
            user.last_login_at = datetime.now()
        user_datastore.commit()
        return user 
Example #5
Source File: rate_limit_quotas.py    From api with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _after_request_callback(self, response):
        """Consume quota and injects HTTP headers when responding to a request
        """
        log = current_app.logger
        try:
            assert response
            tdelta = time.monotonic() - self._request_start_time
            ipaddr = self._get_client_ipaddr()
            if not self._limiter.is_ipaddr_whitelisted(ipaddr):
                self._limiter.consume_quota(tdelta, ipaddr=ipaddr)
                q = self._limiter.get_minimum_across_quotas(ipaddr=ipaddr)
                response.headers.add("X-RateLimit-Remaining", q)

        except Exception as e:
            log.error(str(e), exc_info=True)

        finally:
            return response 
Example #6
Source File: request_manage.py    From listenbrainz-server with GNU General Public License v2.0 6 votes vote down vote up
def send_request_to_spark_cluster(message):
    with create_app().app_context():
        rabbitmq_connection = utils.connect_to_rabbitmq(
            username=current_app.config['RABBITMQ_USERNAME'],
            password=current_app.config['RABBITMQ_PASSWORD'],
            host=current_app.config['RABBITMQ_HOST'],
            port=current_app.config['RABBITMQ_PORT'],
            virtual_host=current_app.config['RABBITMQ_VHOST'],
            error_logger=current_app.logger,
        )
        try:
            channel = rabbitmq_connection.channel()
            channel.exchange_declare(exchange=current_app.config['SPARK_REQUEST_EXCHANGE'], exchange_type='fanout')
            channel.basic_publish(
                exchange=current_app.config['SPARK_REQUEST_EXCHANGE'],
                routing_key='',
                body=message,
                properties=pika.BasicProperties(delivery_mode=2,),
            )
        except Exception:
            # this is a relatively non critical part of LB for now, so just log the error and
            # move ahead
            current_app.logger.error('Could not send message to spark cluster: %s', ujson.dumps(message), exc_info=True) 
Example #7
Source File: replay_user.py    From listenbrainz-server with GNU General Public License v2.0 6 votes vote down vote up
def find_users(self):
        with self.app.app_context():
            self.ls = init_influx_connection(current_app.logger, {
                'REDIS_HOST': current_app.config['REDIS_HOST'],
                'REDIS_PORT': current_app.config['REDIS_PORT'],
                'REDIS_NAMESPACE': current_app.config['REDIS_NAMESPACE'],
                'INFLUX_HOST': current_app.config['INFLUX_HOST'],
                'INFLUX_PORT': current_app.config['INFLUX_PORT'],
                'INFLUX_DB_NAME': current_app.config['INFLUX_DB_NAME'],
            })

            for _ in range(CONNECTION_RETRY_COUNT):
                try:
                    users = db_user.get_all_users()
                    break
                except DatabaseError as e:
                    current_app.logger.error('Error while getting users list: %s', str(e), exc_info=True)
                    time.sleep(1)
            else:
                current_app.logger.critical("Cannot connect to PostgreSQL, exiting...")
                raise DatabaseError("Cannot connect to PostgreSQL, exiting")

            return [user['musicbrainz_id'] for user in users if self.condition(user['musicbrainz_id'])] 
Example #8
Source File: server.py    From zeus with Apache License 2.0 6 votes vote down vote up
def worker(channel, queue, tenant, repo_ids=None, build_ids=None):
    allowed_repo_ids = frozenset(tenant.access.keys())

    while await channel.wait_message():
        msg = await channel.get_json()
        data = msg.get("data")
        if data["repository"]["id"] not in allowed_repo_ids:
            continue

        if build_ids and data["id"] not in build_ids:
            continue

        if repo_ids and data["repository"]["id"] not in repo_ids:
            continue

        evt = Event(msg.get("id"), msg.get("event"), data)
        with sentry_sdk.Hub.current.start_span(
            op="pubsub.receive", description=msg.get("id")
        ):
            await queue.put(evt)
        current_app.logger.debug("pubsub.event.received qsize=%s", queue.qsize()) 
Example #9
Source File: rest_arrays.py    From pureelk with Apache License 2.0 6 votes vote down vote up
def delete_array(array_id):
    """
    Delete an array
    :param array_id:
    :return:
    """
    store = Store(array_config_path(), current_app.logger)

    arrays = store.load_arrays()

    array_deleted = None
    if array_id in arrays:
        array_deleted = arrays[array_id]
        store.remove_array_config(array_id)

    return make_rest_response(
        array_deleted.get_json() if array_deleted else None,
        200) 
Example #10
Source File: server.py    From zeus with Apache License 2.0 6 votes vote down vote up
def worker(db_pool, queue: asyncio.Queue):
    """
    Async worker to perform tasks like persisting revisions to the database.
    """

    while True:
        event, payload = await queue.get()
        try:
            if event == "revision":
                key = (payload["repo_id"], payload["revision"].sha)
                if key in _revision_cache:
                    continue
                async with db_pool.acquire() as conn:
                    await save_revision(conn, payload["repo_id"], payload["revision"])
                _revision_cache[key] = 1
            if event == "cleanup":
                repo_id = payload["repo_id"]
                async with db_pool.acquire() as conn:
                    await cleanup(conn, repo_id)
        except Exception:
            current_app.logger.error(
                "worker.event-error event=%s", event, exc_info=True
            )
        current_app.logger.debug("worker.event event=%s qsize=%s", event, queue.qsize()) 
Example #11
Source File: server.py    From zeus with Apache License 2.0 5 votes vote down vote up
def ping(loop, resp, client_guid):
    # periodically send ping to the browser. Any message that
    # starts with ":" colon ignored by a browser and could be used
    # as ping message.
    while True:
        await asyncio.sleep(15, loop=loop)
        current_app.logger.debug("pubsub.ping guid=%s", client_guid)
        with sentry_sdk.Hub.current.start_span(op="pubsub.ping"):
            resp.write(b": ping\r\n\r\n") 
Example #12
Source File: api.py    From simplecoin_multi with MIT License 5 votes vote down vote up
def api_error_handler(exc):
    # set some defaults
    log = 'info'
    msg = "Exception occurred in error handling"
    code = 500
    extra = {}
    end_user = {}
    try:
        six.reraise(type(exc), exc, tb=sys.exc_info()[2])
    except LeverException as e:
        code = e.code
        msg = str(e)
        end_user = e.end_user
        extra = e.extra
        extra.pop('tb', None)
    except Exception as e:
        current_app.logger.error(
            "Unhandled API error of type {0} raised".format(type(e)))

    if hasattr(exc, 'error_key'):
        end_user['error_key'] = e.error_key
    end_user['success'] = False

    # ensure the message of the exception gets passed on
    end_user['message'] = msg
    response = jsonify(**end_user)
    response.status_code = code

    # logging

    # log the message using flasks logger. In the future this will use
    # logstash and other methods
    message = ('Extra: {}\nEnd User: {}'
               .format(pformat(extra), pformat(end_user)))
    getattr(current_app.logger, log)(message, exc_info=True)

    return response 
Example #13
Source File: server.py    From zeus with Apache License 2.0 5 votes vote down vote up
def build_server(loop, host, port):
    app = Application(loop=loop, logger=current_app.logger, debug=current_app.debug)
    app.router.add_route("GET", "/", stream)
    app.router.add_route("GET", "/healthz", health_check)

    return await loop.create_server(app.make_handler(), host, port) 
Example #14
Source File: replay_user.py    From listenbrainz-server with GNU General Public License v2.0 5 votes vote down vote up
def start(self):
        with self.app.app_context():
            current_app.logger.info("Connecting to Influx...")
            self.ls = init_influx_connection(current_app.logger, {
                'REDIS_HOST': current_app.config['REDIS_HOST'],
                'REDIS_PORT': current_app.config['REDIS_PORT'],
                'REDIS_NAMESPACE': current_app.config['REDIS_NAMESPACE'],
                'INFLUX_HOST': current_app.config['INFLUX_HOST'],
                'INFLUX_PORT': current_app.config['INFLUX_PORT'],
                'INFLUX_DB_NAME': current_app.config['INFLUX_DB_NAME'],
            })
            current_app.logger.info("Done!")

            new_measurement_name = str(uuid.uuid4())
            current_app.logger.info("Temporary destination measurement: %s", new_measurement_name)

            current_app.logger.info("Copying listens from %s to temporary measurement...", self.user_name)
            self.copy_measurement(src=self.user_name, dest=new_measurement_name, apply_filter=True)
            current_app.logger.info("Done!")


            current_app.logger.info("Removing user measurement...")
            self.ls.delete(self.user_name)
            current_app.logger.info("Done!")

            current_app.logger.info("Copying listens back from temporary measurement to %s...", self.user_name)
            self.copy_measurement(src=new_measurement_name, dest=self.user_name, apply_filter=False)
            current_app.logger.info("Done!")

            current_app.logger.info("Removing temporary measurement...")
            self.ls.delete(new_measurement_name)
            current_app.logger.info("Done!") 
Example #15
Source File: server.py    From zeus with Apache License 2.0 5 votes vote down vote up
def build_app(loop=None) -> Application:
    app = Application(loop=loop, logger=current_app.logger, debug=current_app.debug)

    register_api_routes(app)

    app["db_pool"] = await create_db_pool()
    app["queue"] = asyncio.Queue(loop=loop)

    return app 
Example #16
Source File: upload_manager.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def _move_archive_to_uploaded_dir(self,
                                      data_id,
                                      root_path,
                                      archive_path,
                                      dest_file_name=None):
        if not os.path.exists(archive_path):
            raise RuntimeError("Archive [{0}] doesn't exist - Cannot move "
                               "archive to uploaded {1}s "
                               "directory".format(archive_path,
                                                  self._get_kind()))
        uploaded_dir = os.path.join(
            root_path,
            self._get_target_dir_path(),
            data_id)
        if not os.path.isdir(uploaded_dir):
            os.makedirs(uploaded_dir)
        current_app.logger.info('uploading archive to: {0}'
                                .format(uploaded_dir))
        if os.path.isfile(archive_path):
            if not dest_file_name:
                archive_type = self._get_archive_type(archive_path)
                dest_file_name = '{0}.{1}'.format(data_id, archive_type)
            shutil.move(archive_path,
                        os.path.join(uploaded_dir, dest_file_name))
        else:
            for item in os.listdir(archive_path):
                shutil.copy(os.path.join(archive_path, item), uploaded_dir) 
Example #17
Source File: main.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def connect_logger():
    log_level = current_app.config["LOG_LEVEL"]
    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    logger.addHandler(ch)

    current_app.logger = structlog.wrap_logger(logger)
    current_app.logger.info("Started") 
Example #18
Source File: manager.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def post(self):
        ldap_config = self._validate_set_ldap_request()

        if 'ldap_ca_cert' in ldap_config:
            destination = (
                config.instance.ldap_ca_path
                or LDAP_CA_PATH
            )
            with open(destination, 'w') as ca_handle:
                ca_handle.write(ldap_config['ldap_ca_cert'])
            ldap_config.pop('ldap_ca_cert')
            ldap_config['ldap_ca_path'] = destination

        from cloudify_premium.authentication.ldap_authentication \
            import LdapAuthentication

        # update current configuration
        config.instance.update_db(ldap_config)

        # assert LDAP configuration is valid (if credential supplied).
        if ldap_config['ldap_username']:
            auth = LdapAuthentication()
            auth.configure(current_app.logger)
            try:
                auth.authenticate_user(ldap_config.get('ldap_username'),
                                       ldap_config.get('ldap_password'))
            except UnauthorizedError:
                # reload previous configuration.
                config.instance.load_configuration()
                raise BadParametersError(
                    'Failed setting LDAP authenticator: Invalid parameters '
                    'provided.')

        # Restart the rest service so that each the LDAP configuration
        # be loaded to all flask processes.
        rest_utils.set_restart_task()

        ldap_config.pop('ldap_password')
        return ldap_config 
Example #19
Source File: lightningd.py    From Lightning with MIT License 5 votes vote down vote up
def before_request():
    """Setup g context"""
    g.config = current_app.config
    g.bit = g.config['bitcoind']
    secret = hashlib.sha256(g.config['secret']).digest()
    g.seckey = CBitcoinSecret.from_secret_bytes(secret)
    g.addr = 'http://localhost:%d/' % int(g.config['port'])
    g.logger = current_app.logger 
Example #20
Source File: logger.py    From Penny-Dreadful-Tools with GNU General Public License v3.0 5 votes vote down vote up
def logger() -> SupportsLogging:
    if has_app_context(): # type: ignore
        return current_app.logger # type: ignore
    return logging # type: ignore 
Example #21
Source File: authentication.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def _authenticate_password(self, user, auth):
        self.logger.debug('Authenticating username/password')
        is_bootstrap_admin = user and user.is_bootstrap_admin
        if self.external_auth_configured and not is_bootstrap_admin:
            # if external_auth is configured, use it
            return None
        username, password = auth.username, auth.password
        return self._http_auth(user, username, password) 
Example #22
Source File: authentication.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def logger(self):
        return current_app.logger 
Example #23
Source File: rate_limit_quotas.py    From api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _check_limits_callback(self):
        """Check rate limits before processing a request
        Refresh quota counters when needed
        """
        self._limiter.refresh_quota_counters_if_needed()
        ipaddr = self._get_client_ipaddr()
        # token = request.headers.get("Token", None)
        # if token:
        # check token validity
        if not self._limiter.is_quota_available(ipaddr=ipaddr):
            flask.abort(429)
        self._request_start_time = time.monotonic()
        log = current_app.logger
        log.error("_check_limits_callback called") 
Example #24
Source File: measurements.py    From api with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_one_fastpath_measurement(measurement_id, download):
    """Get one measurement from the fastpath table by measurement_id,
    fetching the file from the fastpath host
    """
    log = current_app.logger
    tid = measurement_id[len(FASTPATH_MSM_ID_PREFIX) :]

    path = "/measurements/{}.json.lz4".format(tid)
    log.info(
        "Incoming fastpath query %r. Fetching %s:%d%s",
        measurement_id,
        FASTPATH_SERVER,
        FASTPATH_PORT,
        path,
    )
    conn = http.client.HTTPConnection(FASTPATH_SERVER, FASTPATH_PORT)
    log.debug("Fetching %s:%d %r", FASTPATH_SERVER, FASTPATH_PORT, path)
    conn.request("GET", path)
    r = conn.getresponse()
    log.debug("Response status: %d", r.status)
    try:
        assert r.status == 200
        blob = r.read()
        conn.close()
        log.debug("Decompressing LZ4 data")
        blob = lz4framed.decompress(blob)
        response = make_response(blob)
        response.headers.set("Content-Type", "application/json")
        log.debug("Sending JSON response")
        return response
    except Exception:
        raise BadRequest("No measurement found") 
Example #25
Source File: __init__.py    From discreETLy with MIT License 5 votes vote down vote up
def get_descriptions():
    cache_ttl = app.config.get('TABLE_DESCRIPTIONS_TTL', DEFAULT_CACHE)
    descriptions = app.cache.get('table_descriptions')
    if descriptions is not None:
        return descriptions

    classname = app.config['TABLE_DESCRIPTIONS_SERVICE']
    params = app.config['TABLE_DESCRIPTIONS_SERVICE_PARAMS']
    params['logger'] = app.logger
    params['config'] = app.config
    params['tables'] = app.table_data_provider.tables

    descriptions = load_data_provider(classname, params).get_table_descriptions()
    app.cache.set('table_descriptions', descriptions, timeout=cache_ttl)
    return descriptions 
Example #26
Source File: __init__.py    From discreETLy with MIT License 5 votes vote down vote up
def init(app):
    dynamodb = boto3.client('dynamodb',
                            aws_access_key_id=app.config['ATHENA_USAGE_PARAMS']['aws_access_key_id'],
                            aws_secret_access_key=app.config['ATHENA_USAGE_PARAMS']['aws_secret_access_key'],
                            region_name=app.config['ATHENA_USAGE_PARAMS']['region_name'])

    app.athena_summary_provider = AthenaSummaryProvider(app.config['ATHENA_USAGE_PARAMS'], dynamodb, app.logger) 
Example #27
Source File: __init__.py    From discreETLy with MIT License 5 votes vote down vote up
def init(app):
    S3StatsRefresher(app.logger, app.config['S3_USAGE_PARAMS']).start()
    app.s3stats = S3Stats(app.logger) 
Example #28
Source File: rest_arrays.py    From pureelk with Apache License 2.0 5 votes vote down vote up
def get_arrays():
    """
    Gets all the arrays
    :return: List of arrays in the system
    """
    store = Store(array_config_path(), current_app.logger)
    array_dict = store.load_arrays()
    return [a.get_json() for a in array_dict.values()] 
Example #29
Source File: logger.py    From Penny-Dreadful-Tools with GNU General Public License v3.0 5 votes vote down vote up
def info(*args: Any) -> None:
    """Potentially interesting information that will not be logged in production but will be logged in dev."""
    logger().info('\n'.join(map(str, args))) 
Example #30
Source File: logger.py    From Penny-Dreadful-Tools with GNU General Public License v3.0 5 votes vote down vote up
def warning(*args: Any) -> None:
    """Potentially interesting information that will be logged in production."""
    logger().warning('\n'.join(map(str, args)))