Skip to content

Commit 7c37ec8

Browse files
Modified the new writing/logging.rst according to comments on the initial pull request
1 parent 95ecb66 commit 7c37ec8

File tree

1 file changed

+66
-55
lines changed

1 file changed

+66
-55
lines changed

docs/writing/logging.rst

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ Logging
33

44
The :mod:`logging` module has been a part of Python's Standard Library since
55
version 2.3. It is succinctly described in :pep:`282`. The documentation
6-
is notoriously hard to read, except for the `basic logging tutorial`_,
7-
and often less useful than simply reading the source code.
6+
is notoriously hard to read, except for the `basic logging tutorial`_.
87

98
Logging serves two purposes:
109

@@ -16,8 +15,8 @@ Logging serves two purposes:
1615
reports or to optimize a business goal.
1716

1817

19-
... or Print Statements?
20-
------------------------
18+
... or Print?
19+
-------------
2120

2221
The only time that ``print`` is a better option than logging is when
2322
the goal is to display a help statement for a command line application.
@@ -73,23 +72,28 @@ this in your ``__init__.py``
7372
Logging in an Application
7473
-------------------------
7574

76-
The `twelve factor app's <http://12factor.net>`_, an authoritative reference
75+
The `twelve factor app <http://12factor.net>`_, an authoritative reference
7776
for good practice in application development, contains a section on
7877
`logging best practice <http://12factor.net/logs>`_. It emphatically
7978
advocates for treating log events as an event stream, and for
8079
sending that event stream to standard output to be handled by the
81-
application environment. Do that.
80+
application environment.
8281

8382

8483
There are at least three ways to configure a logger:
8584

86-
- using a file (recommended)
85+
- using a file
8786
- using a dictionary
8887
- using code
8988

90-
Here is how with a file -- let us say it is named ``logging_config.txt``:
89+
Example Configuration via an INI File
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9191

92-
.. code-block:: none
92+
Let us say the file is named ``logging_config.ini``.
93+
More details for the file format are in the `logging configuration`_
94+
section of the `logging tutorial`_.
95+
96+
.. code-block:: ini
9397
9498
[loggers]
9599
keys=root
@@ -126,55 +130,62 @@ Then use :meth:`logging.config.fileConfig` in the code:
126130
logger.debug('often makes a very good meal of %s', 'visiting tourists')
127131
128132
129-
..
130-
As of Python 2.7, you can use a dictionary with configuration details:
131-
132-
.. code-block:: python
133-
134-
import logging
135-
from logging.config import dictConfig
136-
137-
logging_config = dict(
138-
version = 1,
139-
formatters = {
140-
'f': {'format':
141-
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'}
142-
},
143-
handlers = {
144-
'h': {'class': 'logging.StreamHandler',
145-
'formatter': 'f',
146-
'level': logging.DEBUG}
147-
},
148-
loggers = {
149-
root : {'handlers': ['h'],
150-
'level': logging.DEBUG}
151-
}
152-
)
153-
154-
dictConfig(logging_config)
155-
156-
logger = logging.getLogger()
157-
logger.debug('often makes a very good meal of %s', 'visiting tourists')
158-
159-
160-
Or instantiate the logger directly in code:
161-
162-
.. code-block:: python
163-
164-
import logging
165-
166-
logger = logging.getLogger()
167-
handler = logging.StreamHandler()
168-
formatter = logging.Formatter(
169-
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
170-
handler.setFormatter(formatter)
171-
logger.addHandler(handler)
172-
logger.setLevel(logging.DEBUG)
173-
174-
logger.debug('often makes a very good meal of %s', 'visiting tourists')
133+
Example Configuration via a Dictionary
134+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135+
136+
As of Python 2.7, you can use a dictionary with configuration details.
137+
:pep:`319` contains a list of the mandatory and optional elements in
138+
the configuration dictionary.
139+
140+
.. code-block:: python
141+
142+
import logging
143+
from logging.config import dictConfig
144+
145+
logging_config = dict(
146+
version = 1,
147+
formatters = {
148+
'f': {'format':
149+
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'}
150+
},
151+
handlers = {
152+
'h': {'class': 'logging.StreamHandler',
153+
'formatter': 'f',
154+
'level': logging.DEBUG}
155+
},
156+
loggers = {
157+
root : {'handlers': ['h'],
158+
'level': logging.DEBUG}
159+
}
160+
)
161+
162+
dictConfig(logging_config)
163+
164+
logger = logging.getLogger()
165+
logger.debug('often makes a very good meal of %s', 'visiting tourists')
166+
167+
168+
Example Configuration Directly in Code
169+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170+
171+
.. code-block:: python
172+
173+
import logging
174+
175+
logger = logging.getLogger()
176+
handler = logging.StreamHandler()
177+
formatter = logging.Formatter(
178+
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
179+
handler.setFormatter(formatter)
180+
logger.addHandler(handler)
181+
logger.setLevel(logging.DEBUG)
182+
183+
logger.debug('often makes a very good meal of %s', 'visiting tourists')
175184
176185
177186
.. _basic logging tutorial: http://docs.python.org/howto/logging.html#logging-basic-tutorial
187+
.. _logging configuration: https://docs.python.org/howto/logging.html#configuring-logging
188+
.. _logging tutorial: http://docs.python.org/howto/logging.html
178189
.. _configuring logging for a library: https://docs.python.org/howto/logging.html#configuring-logging-for-a-library
179190
.. _log record: https://docs.python.org/library/logging.html#logrecord-attributes
180191
.. _requests source: https://github.com/kennethreitz/requests

0 commit comments

Comments
 (0)