Пример #1
0
def setup_logging(app):
    """
    Skip /healthz and /metrics records from log (either plain
    text or structured)
    """
    class HealthCheckFilter(logging.Filter):
        def filter(self, record):

            if "healthz" in record.getMessage():
                return False

            if 'request_info' in record.__dict__:
                req = record.__dict__['request_info'].request
                return "healthz" not in req.path
            return True

    class MetricsFilter(logging.Filter):
        def filter(self, record):

            if "metrics" in record.getMessage():
                return False

            if 'request_info' in record.__dict__:
                req = record.__dict__['request_info'].request
                return "metrics" not in req.path
            return True

    json_logging.init_request_instrument(app)

    hdlr = logging.getLogger('connexion-request-logger')
    hdlr.propagate = False

    for h in hdlr.handlers:
        h.addFilter(HealthCheckFilter())
        h.addFilter(MetricsFilter())
Пример #2
0
    def __init__(self, app, name="flask-logger", level="info", suppress_app_logs=True):
        """
        Initialize a new flask logger.

        :param app: The flask application object.
        :param name: The name of the logger (printed with each output).
        :param level: Minimum log level to output (see FlaskLogger.levels).
        """
        if level not in FlaskLogger.levels.keys():
            raise Exception("invalid log level (see FlaskLogger.levels)")

        jl.ENABLE_JSON_LOGGING = True
        jl.init_flask(custom_formatter=Format)
        jl.init_request_instrument(app)

        if suppress_app_logs:
            logging.getLogger("flask-request-logger").disabled = True
            app.logger.disabled = True
            logging.getLogger('werkzeug').disabled = True
            os.environ['WERKZEUG_RUN_MAIN'] = 'true'

        self.name = name
        self.lg = BaseLogger(name)
        self.lg.setLevel(Logger.levels[level])
        self.lg.addHandler(logging.StreamHandler(sys.stdout))
Пример #3
0
def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_by_name[config_name])
    app.wsgi_app = ProxyFix(app.wsgi_app)

    if config_name == 'prod':

        @property
        def specs_url(self):
            return url_for(self.endpoint('specs'),
                           _external=True,
                           _scheme='https')

        Api.specs_url = specs_url

    db.init_app(app)
    flask_bcrypt.init_app(app)
    migrate.init_app(app, db)

    # Init JSON logging, only once to avoid exceptions during tests
    if json_logging._current_framework is None:
        json_logging.init_flask(enable_json=True)
        json_logging.init_request_instrument(app)

    with app.app_context():
        if app.config['PYBRAKE']['project_key']:
            pybrake.flask.init_app(app)
            app.logger.addHandler(
                pybrake.LoggingHandler(notifier=app.extensions['pybrake'],
                                       level=logging.ERROR))

    return app
Пример #4
0
    def __init__(
            self,
            import_name,
            static_url_path=None,
            static_folder="static",
            static_host=None,
            host_matching=False,
            subdomain_matching=False,
            template_folder="templates",
            instance_path=None,
            instance_relative_config=False,
            root_path=None,
    ):
        super().__init__(
            import_name,
            static_url_path,
            static_folder,
            static_host,
            host_matching,
            subdomain_matching,
            template_folder,
            instance_path,
            instance_relative_config,
            root_path
        )

        json_logging.ENABLE_JSON_LOGGING = True
        json_logging.init_flask(enable_json=True)
        json_logging.init_request_instrument(self)

        self.logger = logging.getLogger(self.name)
        self.logger.setLevel(int(os.environ.get("LOGGING_LEVEL", logging.INFO)))
        self.logger.addHandler(logging.StreamHandler(sys.stdout))
        self.logger.propagate = False

        self.logger_core = logging.getLogger("__core__")
        self.logger_core.setLevel(int(os.environ.get("CORE_LOGGING_LEVEL", logging.ERROR)))
        self.logger_core.addHandler(logging.StreamHandler(sys.stdout))
        self.logger_core.propagate = False

        self.logger_router = logging.getLogger("__router__")
        self.logger_router.setLevel(int(os.environ.get("ROUTER_LOGGING_LEVEL", logging.ERROR)))
        self.logger_router.addHandler(logging.StreamHandler(sys.stdout))
        self.logger_router.propagate = False

        self.req_logger = logging.getLogger("flask-request-logger")
        self.req_logger.setLevel(logging.ERROR)
        self.req_logger.propagate = False

        init()

        try:
            import env
            env.init()
        except ImportError:
            self.logger.warning("No app env defined!")

        subject_factory.override(providers.Factory(lambda *args, **kw: g_wrap(subject_factory.cls, *args, **kw)))
        self.router = event_router_factory()
Пример #5
0
def create():
    app = connexion.FlaskApp(__name__, port=9090, specification_dir='openapi/')
    json_logging.init_connexion(enable_json=True)
    json_logging.init_request_instrument(app)

    app.add_api('helloworld-api.yaml',
                arguments={'title': 'Hello World Example'})
    return app
Пример #6
0
def create():
    app = connexion.FlaskApp(__name__, port=9090, specification_dir='openapi/')
    json_logging.ENABLE_JSON_LOGGING = True
    json_logging.init(framework_name='connexion')
    json_logging.init_request_instrument(app)

    app.add_api('helloworld-api.yaml',
                arguments={'title': 'Hello World Example'})
    return app
Пример #7
0
def get_logger(app):
    json_logging.ENABLE_JSON_LOGGING = True
    json_logging.init_flask()
    json_logging.init_request_instrument(app)

    # init the logger as usual
    logger = logging.getLogger("test-logger")
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler(sys.stdout))
    return logger
Пример #8
0
def __configure_logger(app: Flask):
    if not json_logging.ENABLE_JSON_LOGGING:
        "Prevent to call json_logging.init_flask() more than once."
        json_logging.ENABLE_JSON_LOGGING = True
        json_logging.init_flask()
        json_logging.init_request_instrument(app)

    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(app.config["LOGS_LEVEL"])
    app.logger.addHandler(ch)
Пример #9
0
def configure_logger(app):
    """Configure loggers."""
    enable_json = True if app.config.get(
        "JSON_LOGGING") == 'true' or app.config.get(
            "JSON_LOGGING") == True else False
    json_logging.init_flask(enable_json=enable_json)
    json_logging.init_request_instrument(app)

    handler = logging.StreamHandler(sys.stdout)
    if not app.logger.handlers:
        app.logger.addHandler(handler)
Пример #10
0
def create_app():
    app = Flask(__name__)
    setup_metrics(app)

    json_logging.ENABLE_JSON_LOGGING = True
    json_logging.init(framework_name='flask')
    json_logging.init_request_instrument(app)
    logger = logging.getLogger("app-logger")
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler(sys.stdout))

    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    api = Api(app)

    @app.route('/ping')
    def ping():
        logger.info("pinged", extra={'tags': ['role:web', 'env:prod']})
        return jsonify(ping='pong')

    @app.route("/healthz", methods=["GET"])
    def healthz():
        logger.info("health-checked", extra={'tags': ['role:web', 'env:prod']})
        return jsonify({"status": "SUCCESS"})

    @app.route('/metrics')
    def metrics():
        return Response(prometheus_client.generate_latest(),
                        mimetype=CONTENT_TYPE_LATEST)

    api.add_resource(TaskList, "/tasks")
    api.add_resource(Task, "/tasks/<int:id>")

    def initialize_tracer():
        config = Config(config={
            'sampler': {
                'type': 'const',
                'param': 1
            },
            'local_agent': {
                'reporting_host': "cicdnode-0.vdigital.io",
                'reporting_port': 6831
            }
        },
                        service_name='task-service')
        return config.initialize_tracer()  # also sets opentracing.tracer

    flask_tracer = FlaskTracer(initialize_tracer, True, app)

    return app
def startup_event():
    json_logging.CREATE_CORRELATION_ID_IF_NOT_EXISTS = True
    json_logging.CORRELATION_ID_GENERATOR = lambda: "data-service-" + str(
        uuid.uuid1())
    json_logging.init_fastapi(enable_json=True, custom_formatter=CustomJSONLog)
    json_logging.init_request_instrument(
        data_service_app, custom_formatter=CustomJSONRequestLogFormatter)

    logging.basicConfig(level=logging.INFO)
    json_logging.config_root_logger()

    log = logging.getLogger(__name__)

    log.info('Started data-service')
    log.info(config.get_settings().print())
Пример #12
0
def create_app(config_class: Type[Config] = Config) -> Flask:
    app = Flask(__name__)

    if os.environ["FLASK_ENV"] == "production":
        json_logging.init_flask(enable_json=True)
        json_logging.init_request_instrument(app)

    app.config.from_object(config_class)

    app.register_blueprint(bp)

    configure_openapi_with_flask(app)

    @app.teardown_appcontext
    def cleanup(resp_or_exc):
        session.remove()

    return app
Пример #13
0
def create_app():

    _app = Flask(__name__)

    CORS(_app, resources={r"/api/*": {"origin": "*"}})

    _app.register_blueprint(bp_excel)

    _app.config['JWT_SECRET_KEY'] = JWT_SECRET_KEY

    json_logging.ENABLE_JSON_LOGGING = True
    json_logging.init_flask(enable_json=True)
    json_logging.init_request_instrument(_app)

    request_logger = json_logging.get_request_logger()
    handler = RotatingFileHandler(filename='samoyed/log/log.json',
                                  maxBytes=5000000,
                                  backupCount=10)
    handler.setFormatter(json_logging.JSONRequestLogFormatter())
    request_logger.addHandler(handler)

    JWTManager(app=_app)

    return _app
        logger.error("Error occurred", exc_info=e)
        logger.exception("Error occurred", exc_info=e)
    return "Error occurred, check log for detail"


@app.get('/exclude_from_request_instrumentation')
def exclude_from_request_instrumentation():
    return "this request wont log request instrumentation information"


if __name__ == "__main__":
    import uvicorn
    logging_config = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'default_handler': {
                'class': 'logging.StreamHandler',
                'level': 'DEBUG',
            },
        },
        'loggers': {
            '': {
                'handlers': ['default_handler'],
            }
        }
    }
    json_logging.init_fastapi(enable_json=True)
    json_logging.init_request_instrument(app, exclude_url_patterns=[r'^/exclude_from_request_instrumentation'])
    uvicorn.run(app, host='0.0.0.0', port=5000, log_level="debug", log_config=logging_config)
Пример #15
0
    """
    def __init__(self, request, **kwargs):
        super(CustomDefaultRequestResponseDTO,
              self).__init__(request, **kwargs)

    def on_request_complete(self, response):
        super(CustomDefaultRequestResponseDTO,
              self).on_request_complete(response)
        self.status = response.status


app = flask.Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(
    app,
    exclude_url_patterns=[r'/exclude_from_request_instrumentation'],
    custom_formatter=CustomRequestJSONLog,
    request_response_data_extractor_class=CustomDefaultRequestResponseDTO)

# init the logger as usual
logger = logging.getLogger("test logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/')
def home():
    logger.info("test log statement")
    logger.info("test log statement with extra props",
                extra={'props': {
                    "extra_property": 'extra_value'
Пример #16
0
def client_and_log_handler():
    import json_logging

    # Init app
    app = fastapi.FastAPI()

    # Init std logging
    logger = logging.getLogger(LOGGER_NAME)
    logger.setLevel(logging.DEBUG)
    handler = FormattedMessageCollectorHandler()
    logger.addHandler(handler)

    # Add json_logging
    json_logging.init_fastapi(enable_json=True)
    json_logging.init_request_instrument(
        app, exclude_url_patterns=["^/no-request-instrumentation"])

    # Prepare test endpoints
    @app.get("/log/levels/debug")
    async def log_debug():
        logger.debug("debug message")
        return {}

    @app.get("/log/levels/info")
    async def log_info():
        logger.info("info message")
        return {}

    @app.get("/log/levels/error")
    async def log_error():
        logger.error("error message")
        return {}

    @app.get("/log/extra_property")
    async def extra_property():
        logger.info(
            "test log statement with extra props",
            extra={
                "props": {
                    "extra_property": "extra_value"
                },
                "tags": ["app:name"],
                "extra_property": "extra_value2"
            },
        )
        return {}

    @app.get("/log/extra_property_no_props")
    async def extra_property_no_props():
        logger.info(
            "test log statement with extra and no 'props' property",
            extra={
                "tags": ["app:name"],
                "extra_property": "extra_value2"
            },
        )
        return {}

    @app.get("/log/exception")
    async def log_exception():
        try:
            raise RuntimeError()
        except BaseException as e:
            logger.exception("Error occurred", exc_info=e)
        return {}

    @app.get("/get-correlation-id")
    async def get_correlation_id():
        return {'correlation_id': json_logging.get_correlation_id()}

    @app.get('/no-request-instrumentation')
    async def excluded_from_request_instrumentation():
        return {}

    test_client = fastapi.testclient.TestClient(app)
    yield test_client, handler

    # Tear down test environment
    logger.removeHandler(handler)
    undo_imports_from_package(
        "json_logging")  # Necessary because of json-logging's global state
class CustomRequestJSONLog(json_logging.JSONLogWebFormatter):
    """
    Customized logger
    """

    def format(self, record):
        json_customized_log_object = ({
            "customized_prop": "customized value",
            "correlation_id": json_logging.get_correlation_id(),
        })
        return json.dumps(json_customized_log_object)


app = flask.Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app, exclude_url_patterns=[r'/exclude_from_request_instrumentation'],
                                     custom_formatter=CustomRequestJSONLog)

# init the logger as usual
logger = logging.getLogger("test logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


@app.route('/')
def home():
    logger.info("test log statement")
    logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
    correlation_id = json_logging.get_correlation_id()
    return "hello world" \
           "\ncorrelation_id                    : " + correlation_id
Пример #18
0
import datetime, logging, sys, json_logging, flask

app = flask.Flask(__name__)
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)

# init the logger as usual
logger = logging.getLogger("test-logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))

@app.route('/')
def home():
    logger.info("test log statement")
    logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}})
    correlation_id = json_logging.get_correlation_id()
    return "Hello world : " + str(datetime.datetime.now())

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(5000), use_reloader=False)
Пример #19
0
app.registry.register(app.request_counter)
app.scan_counter = Counter('scans', 'Number of overall virus scans.')
app.registry.register(app.scan_counter)
app.infection_counter = Counter('infections',
                                'Number of infected files found.')
app.registry.register(app.infection_counter)
app.scan_duration_histogram = Histogram('scan_duration',
                                        'Histogram over virus scan duration.')
app.registry.register(app.scan_duration_histogram)

# Configure logging
if app.config['LOGJSON']:
    do_not_log = ['/health', '/metrics']

    json_logging.init_quart(enable_json=True)
    json_logging.init_request_instrument(app, exclude_url_patterns=do_not_log)

logger = logging.getLogger('clamav-rest')
logger.setLevel(app.config['LOGLEVEL'])
logger.addHandler(logging.StreamHandler(sys.stdout))

# Configure clamd
try:
    cd = clamd.ClamdAsyncNetworkSocket(host=app.config['CLAMD_HOST'],
                                       port=app.config['CLAMD_PORT'])
except BaseException:
    logger.exception('error bootstrapping clamd for network socket')


# https://gitlab.com/pgjones/quart-auth/-/issues/6#note_460844029
def auth_required(func):