|
7 | 7 |
|
8 | 8 | app = flask.Flask(__name__) |
9 | 9 | json_logging.init_flask(enable_json=True) |
10 | | -json_logging.init_request_instrument( |
11 | | - app, exclude_url_patterns=[r'/ready', r'/alive']) |
| 10 | +json_logging.init_request_instrument(app, exclude_url_patterns=[r'/exclude_from_request_instrumentation']) |
12 | 11 |
|
13 | | -app.logger.setLevel(logging.getLevelName(env.get('LOGLEVEL', 'INFO').upper())) |
14 | | -app.logger.addHandler(logging.StreamHandler(sys.stdout)) |
15 | | -app.logger.removeHandler(default_handler) |
| 12 | +# init the logger as usual |
| 13 | +logger = logging.getLogger("test logger") |
| 14 | +logger.setLevel(logging.DEBUG) |
| 15 | +logger.addHandler(logging.StreamHandler(sys.stdout)) |
16 | 16 |
|
17 | | -app.config["MONGO_URI"] = env.get("MONGO_DSN") |
18 | 17 |
|
19 | | -client = MongoClient(app.config["MONGO_URI"]) |
20 | | -app.logger.debug(client.server_info()) |
| 18 | +@app.route('/') |
| 19 | +def home(): |
| 20 | + logger.info("test log statement") |
| 21 | + logger.info("test log statement with extra props", extra={'props': {"extra_property": 'extra_value'}}) |
| 22 | + logger.info("test log statement with custom correlation id", |
| 23 | + extra={'props': {'correlation_id': 'custom_correlation_id'}}) |
| 24 | + |
| 25 | + correlation_id = json_logging.get_correlation_id() |
| 26 | + return "hello world" \ |
| 27 | + "\ncorrelation_id : " + correlation_id |
| 28 | + |
| 29 | + |
| 30 | +@app.route('/exception') |
| 31 | +def exception(): |
| 32 | + try: |
| 33 | + raise RuntimeError |
| 34 | + except BaseException as e: |
| 35 | + logger.error("Error occurred", exc_info=e) |
| 36 | + logger.exception("Error occurred", exc_info=e) |
| 37 | + return "Error occurred, check log for detail" |
| 38 | + |
| 39 | + |
| 40 | +@app.route('/exclude_from_request_instrumentation') |
| 41 | +def exclude_from_request_instrumentation(): |
| 42 | + return "this request wont log request instrumentation information" |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + app.run(host='0.0.0.0', port=int(5000), use_reloader=False) |
0 commit comments