Skip to content

Commit 1a87e3b

Browse files
committed
logging documentation
1 parent 9cb2ad8 commit 1a87e3b

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Flask-RESTPlus with Flask.
5151
errors
5252
mask
5353
swagger
54+
logging
5455
postman
5556
scaling
5657
example

doc/logging.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
Logging
2+
===============
3+
4+
Flask-RESTPlus extends `Flask's logging <https://flask.palletsprojects.com/en/1.1.x/logging/>`_
5+
by providing each ``API`` and ``Namespace`` it's own standard Python :class:`logging.Logger` instance.
6+
This allows separation of logging on a per namespace basis to allow more fine-grained detail and configuration.
7+
8+
By default, these loggers inherit configuration from the Flask application object logger.
9+
10+
.. code-block:: python
11+
12+
import logging
13+
14+
import flask
15+
16+
from flask_restplus import Api, Resource
17+
18+
# configure root logger
19+
logging.basicConfig(level=logging.INFO)
20+
21+
app = flask.Flask(__name__)
22+
23+
api = Api(app)
24+
25+
26+
# each of these loggers uses configuration from app.logger
27+
ns1 = api.namespace('api/v1', description='test')
28+
ns2 = api.namespace('api/v2', description='test')
29+
30+
31+
@ns1.route('/my-resource')
32+
class MyResource(Resource):
33+
def get(self):
34+
# will log
35+
ns1.logger.info("hello from ns1")
36+
return {"message": "hello"}
37+
38+
39+
@ns2.route('/my-resource')
40+
class MyNewResource(Resource):
41+
def get(self):
42+
# won't log due to INFO log level from app.logger
43+
ns2.logger.debug("hello from ns2")
44+
return {"message": "hello"}
45+
46+
47+
Loggers can be configured individually to override the configuration from the Flask
48+
application object logger. In the above example, ``ns2`` log level can be set to
49+
``DEBUG`` individually:
50+
51+
.. code-block:: python
52+
53+
# ns1 will have log level INFO from app.logger
54+
ns1 = api.namespace('api/v1', description='test')
55+
56+
# ns2 will have log level DEBUG
57+
ns2 = api.namespace('api/v2', description='test')
58+
ns2.logger.setLevel(logging.DEBUG)
59+
60+
61+
@ns1.route('/my-resource')
62+
class MyResource(Resource):
63+
def get(self):
64+
# will log
65+
ns1.logger.info("hello from ns1")
66+
return {"message": "hello"}
67+
68+
69+
@ns2.route('/my-resource')
70+
class MyNewResource(Resource):
71+
def get(self):
72+
# will log
73+
ns2.logger.debug("hello from ns2")
74+
return {"message": "hello"}
75+
76+
77+
Adding additional handlers:
78+
79+
80+
.. code-block:: python
81+
82+
# configure a file handler for ns1 only
83+
ns1 = api.namespace('api/v1')
84+
fh = logging.FileHandler("v1.log")
85+
ns1.logger.addHandler(fh)
86+
87+
ns2 = api.namespace('api/v2')
88+
89+
90+
@ns1.route('/my-resource')
91+
class MyResource(Resource):
92+
def get(self):
93+
# will log to *both* v1.log file and app.logger handlers
94+
ns1.logger.info("hello from ns1")
95+
return {"message": "hello"}
96+
97+
98+
@ns2.route('/my-resource')
99+
class MyNewResource(Resource):
100+
def get(self):
101+
# will log to *only* app.logger handlers
102+
ns2.logger.info("hello from ns2")
103+
return {"message": "hello"}

0 commit comments

Comments
 (0)