Commit 3d9e9722 authored by Vinay Sajip's avatar Vinay Sajip

Corrected typo, added comment in cookbook recipe.

parent da4b5b82
...@@ -1595,7 +1595,7 @@ Implementing structured logging ...@@ -1595,7 +1595,7 @@ Implementing structured logging
Although most logging messages are intended for reading by humans, and thus not Although most logging messages are intended for reading by humans, and thus not
readily machine-parseable, there might be cirumstances where you want to output readily machine-parseable, there might be cirumstances where you want to output
messages in a structured format which *is* capable of being parsed by a program messages in a structured format which *is* capable of being parsed by a program
(without needed complex regular expressions to parse the log message). This is (without needing complex regular expressions to parse the log message). This is
straightforward to achieve using the logging package. There are a number of straightforward to achieve using the logging package. There are a number of
ways in which this could be achieved, but the following is a simple approach ways in which this could be achieved, but the following is a simple approach
which uses JSON to serialise the event in a machine-parseable manner:: which uses JSON to serialise the event in a machine-parseable manner::
...@@ -1620,6 +1620,9 @@ If the above script is run, it prints:: ...@@ -1620,6 +1620,9 @@ If the above script is run, it prints::
message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"} message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"}
Note that the order of items might be different according to the version of
Python used.
If you need more specialised processing, you can use a custom JSON encoder, If you need more specialised processing, you can use a custom JSON encoder,
as in the following complete example:: as in the following complete example::
...@@ -1628,6 +1631,7 @@ as in the following complete example:: ...@@ -1628,6 +1631,7 @@ as in the following complete example::
import json import json
import logging import logging
# This next bit is to ensure the script runs unchanged on 2.x and 3.x
try: try:
unicode unicode
except NameError: except NameError:
...@@ -1650,7 +1654,7 @@ as in the following complete example:: ...@@ -1650,7 +1654,7 @@ as in the following complete example::
s = Encoder().encode(self.kwargs) s = Encoder().encode(self.kwargs)
return '%s >>> %s' % (self.message, s) return '%s >>> %s' % (self.message, s)
_ = StructuredMessage _ = StructuredMessage # optional, to improve readability
def main(): def main():
logging.basicConfig(level=logging.INFO, format='%(message)s') logging.basicConfig(level=logging.INFO, format='%(message)s')
...@@ -1663,3 +1667,6 @@ When the above script is run, it prints:: ...@@ -1663,3 +1667,6 @@ When the above script is run, it prints::
message 1 >>> {"snowman": "\u2603", "set_value": [1, 2, 3]} message 1 >>> {"snowman": "\u2603", "set_value": [1, 2, 3]}
Note that the order of items might be different according to the version of
Python used.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment