Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 8bd547e

Browse files
author
Zakaria Zajac
authored
Merge pull request #52 from quodlibetor/add-examples-to-readme
Add examples to readme
2 parents 1aa7252 + 4ab18e6 commit 8bd547e

File tree

2 files changed

+106
-62
lines changed

2 files changed

+106
-62
lines changed

README.markdown

Lines changed: 100 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Pip:
1212

1313
Pypi:
1414

15-
https://pypi.python.org/pypi/python-json-logger
15+
https://pypi.python.org/pypi/python-json-logger
1616

1717
Manual:
1818

@@ -21,6 +21,8 @@ Manual:
2121
Usage
2222
=====
2323

24+
## Integrating with Python's logging framework
25+
2426
Json outputs are provided by the JsonFormatter logging formatter. You can add the customer formatter like below:
2527

2628
**Please note: version 0.1.0 has changed the import structure, please update to the following example for proper importing**
@@ -36,85 +38,122 @@ Json outputs are provided by the JsonFormatter logging formatter. You can add th
3638
logHandler.setFormatter(formatter)
3739
logger.addHandler(logHandler)
3840
```
39-
The fmt parser can also be overidden if you want to use an alternate from the default.
41+
42+
## Customizing fields
43+
44+
The fmt parser can also be overidden if you want to have required fields that differ from the default of just `message`.
45+
46+
These two invocations are equivalent:
4047

4148
```python
42-
class CustomJsonFormatter(jsonlogger.JsonFormatter):
43-
def parse(self):
44-
return eval(self._fmt)
49+
class CustomJsonFormatter(jsonlogger.JsonFormatter):
50+
def parse(self):
51+
return self._fmt.split(';')
52+
53+
formatter = CustomJsonFormatter('one;two')
54+
55+
# is equivalent to:
56+
57+
formatter = jsonlogger.JsonFormatter('(one) (two)')
4558
```
4659

47-
You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an extra={} argument.
60+
You can also add extra fields to your json output by specifying a dict in place of message, as well as by specifying an `extra={}` argument.
61+
4862
Contents of these dictionaries will be added at the root level of the entry and may override basic fields.
63+
64+
You can also use the `add_fields` method to add to or generally normalize the set of default set of fields, it is be called for every log event. For example, to unify default fields with those provided by [structlog](http://www.structlog.org/) you could do something like this:
65+
66+
```python
67+
class CustomJsonFormatter(jsonlogger.JsonFormatter):
68+
def add_fields(self, log_record, record, message_dict):
69+
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
70+
if not log_record.get('timestamp'):
71+
# this doesn't use record.created, so it is slightly off
72+
now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
73+
log_record['timestamp'] = now
74+
if log_record.get('level'):
75+
log_record['level'] = log_record['level'].upper()
76+
else:
77+
log_record['level'] = record.levelname
78+
79+
formatter = CustomJsonFormatter('(timestamp) (level) (name) (message)')
80+
```
81+
82+
Items added to the log record will be included in *every* log message, no matter what the format requires.
83+
84+
## Adding custom object serialization
85+
4986
For custom handling of object serialization you can specify default json object translator or provide a custom encoder
5087

5188
```python
52-
def json_translate(obj):
53-
if isinstance(obj, MyClass):
54-
return {"special": obj.special}
89+
def json_translate(obj):
90+
if isinstance(obj, MyClass):
91+
return {"special": obj.special}
5592

56-
formatter = jsonlogger.JsonFormatter(json_default=json_translate,
57-
json_encoder=json.JSONEncoder())
58-
logHandler.setFormatter(formatter)
93+
formatter = jsonlogger.JsonFormatter(json_default=json_translate,
94+
json_encoder=json.JSONEncoder())
95+
logHandler.setFormatter(formatter)
5996

60-
logger.info({"special": "value", "run": 12})
61-
logger.info("classic message", extra={"special": "value", "run": 12})
97+
logger.info({"special": "value", "run": 12})
98+
logger.info("classic message", extra={"special": "value", "run": 12})
6299
```
63100

64-
With a Config File
65-
------------------
101+
## Using a Config File
102+
66103
To use the module with a config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.jsonlogger.JsonFormatter`. Here is a sample config file.
67104

68-
[loggers]
69-
keys = root,custom
70-
71-
[logger_root]
72-
handlers =
73-
74-
[logger_custom]
75-
level = INFO
76-
handlers = custom
77-
qualname = custom
78-
79-
[handlers]
80-
keys = custom
81-
82-
[handler_custom]
83-
class = StreamHandler
84-
level = INFO
85-
formatter = json
86-
args = (sys.stdout,)
87-
88-
[formatters]
89-
keys = json
90-
91-
[formatter_json]
92-
format = %(message)s
93-
class = pythonjsonlogger.jsonlogger.JsonFormatter
94-
95-
Example
96-
=======
105+
```ini
106+
[loggers]
107+
keys = root,custom
108+
109+
[logger_root]
110+
handlers =
111+
112+
[logger_custom]
113+
level = INFO
114+
handlers = custom
115+
qualname = custom
116+
117+
[handlers]
118+
keys = custom
119+
120+
[handler_custom]
121+
class = StreamHandler
122+
level = INFO
123+
formatter = json
124+
args = (sys.stdout,)
125+
126+
[formatters]
127+
keys = json
128+
129+
[formatter_json]
130+
format = %(message)s
131+
class = pythonjsonlogger.jsonlogger.JsonFormatter
132+
```
133+
134+
Example Output
135+
==============
97136

98137
Sample JSON with a full formatter (basically the log message from the unit test). Every log message will appear on 1 line like a typical logger.
99138

100139
```json
101140
{
102-
"threadName": "MainThread",
103-
"name": "root",
104-
"thread": 140735202359648,
105-
"created": 1336281068.506248,
106-
"process": 41937,
107-
"processName": "MainProcess",
108-
"relativeCreated": 9.100914001464844,
109-
"module": "tests",
110-
"funcName": "testFormatKeys",
111-
"levelno": 20,
112-
"msecs": 506.24799728393555,
113-
"pathname": "tests/tests.py",
114-
"lineno": 60,
115-
"asctime": ["12-05-05 22:11:08,506248"],
116-
"message": "testing logging format",
117-
"filename": "tests.py",
141+
"threadName": "MainThread",
142+
"name": "root",
143+
"thread": 140735202359648,
144+
"created": 1336281068.506248,
145+
"process": 41937,
146+
"processName": "MainProcess",
147+
"relativeCreated": 9.100914001464844,
148+
"module": "tests",
149+
"funcName": "testFormatKeys",
150+
"levelno": 20,
151+
"msecs": 506.24799728393555,
152+
"pathname": "tests/tests.py",
153+
"lineno": 60,
154+
"asctime": ["12-05-05 22:11:08,506248"],
155+
"message": "testing logging format",
156+
"filename": "tests.py",
118157
"levelname": "INFO",
119158
"special": "value",
120159
"run": 12

src/pythonjsonlogger/jsonlogger.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def _default_json_handler(obj):
8686
self._skip_fields.update(RESERVED_ATTR_HASH)
8787

8888
def parse(self):
89-
"""Parses format string looking for substitutions"""
89+
"""
90+
Parses format string looking for substitutions
91+
92+
This method is responsible for returning a list of fields (as strings)
93+
to include in all log messages.
94+
"""
9095
standard_formatters = re.compile(r'\((.+?)\)', re.IGNORECASE)
9196
return standard_formatters.findall(self._fmt)
9297

0 commit comments

Comments
 (0)