Skip to content

Commit f8987c9

Browse files
author
Bob Bui
committed
fix: fix flask sample
1 parent db3aa25 commit f8987c9

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

example/flask_sample_app.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@
77

88
app = flask.Flask(__name__)
99
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'])
1211

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))
1616

17-
app.config["MONGO_URI"] = env.get("MONGO_DSN")
1817

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

Comments
 (0)