Skip to content

Commit 3bb401d

Browse files
author
thang
committed
add JSON format example
1 parent 62f500e commit 3bb401d

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

README.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ If you're using Cloud Foundry, it worth to check out the library [SAP/cf-python-
1818
# 1. Features
1919
1. Lightweight, no dependencies. Tested with Python 2.7 & 3.5.
2020
2. 100% compatible with **logging** module. Minimal configuration needed.
21-
3. Emit JSON logs [\[0\]](#0-full-logging-format-references)
21+
3. Emit JSON logs, see here for what kind of log will be emitted. [\[0\]](#0-full-logging-format-references)
2222
4. Support **correlation-id** [\[1\]](#1-what-is-correlation-idrequest-id)
2323
5. Support request instrumentation. Built in support for [Flask](http://flask.pocoo.org/) & [Sanic](http://flask.pocoo.org/). Extensible to support others.
2424
6. Support adding extra properties to JSON log object.
@@ -169,16 +169,63 @@ pip3 install .
169169
```
170170
# 7. References
171171
## [0] Full logging format references
172+
2 types of logging statement will be emmited by this library:
173+
- Application log: normal logging statement
174+
e.g.:
175+
```
176+
{
177+
"type": "log",
178+
"written_at": "2017-12-23T16:55:37.280Z",
179+
"written_ts": 1514048137280721000,
180+
"component_id": "1d930c0xd-19-s3213",
181+
"component_name": "ny-component_name",
182+
"component_instance": 0,
183+
"logger": "test logger",
184+
"thread": "MainThread",
185+
"level": "INFO",
186+
"line_no": 22,
187+
"correlation_id": "1975a02e-e802-11e7-8971-28b2bd90b19a",
188+
"extra_property": "extra_value"
189+
}
190+
```
191+
- Request log: request instrumentation logging statement which recorded request information such as response time, request size, etc.
192+
```
193+
{
194+
"type": "request",
195+
"written_at": "2017-12-23T16:55:37.280Z",
196+
"written_ts": 1514048137280721000,
197+
"component_id": "-",
198+
"component_name": "-",
199+
"component_instance": 0,
200+
"correlation_id": "1975a02e-e802-11e7-8971-28b2bd90b19a",
201+
"remote_user": "user_a",
202+
"request": "/index.html",
203+
"referer": "-",
204+
"x_forwarded_for": "-",
205+
"protocol": "HTTP/1.1",
206+
"method": "GET",
207+
"remote_ip": "127.0.0.1",
208+
"request_size_b": 1234,
209+
"remote_host": "127.0.0.1",
210+
"remote_port": 50160,
211+
"request_received_at": "2017-12-23T16:55:37.280Z",
212+
"response_time_ms": 0,
213+
"response_status": 200,
214+
"response_size_b": "122",
215+
"response_content_type": "text/html; charset=utf-8",
216+
"response_sent_at": "2017-12-23T16:55:37.280Z"
217+
}
218+
```
219+
See following tables for detail format explanation:
172220
- Common field
173221

174222
Field | Description | Format | Example
175223
--- | --- | --- | ---
176224
written_at | The date when this log message was written. | ISO 8601 YYYY-MM-DDTHH:MM:SS.milliZ | 2017-12-23T15:14:02.208Z
177225
written_ts | The timestamp in nano-second precision when this request metric message was written. | long number | 1456820553816849408
178226
correlation_id | The timestamp in nano-second precision when this request metric message was written. | string | db2d002e-2702-41ec-66f5-c002a80a3d3f
179-
type | Type of message. "logs" or "request" | string | db2d002e-2702-41ec-66f5-c002a80a3d3f
180-
component_id | Uniquely identifies the software component that has processed the current
181-
request | string | 9e6f3ecf-def0-4baf-8fac-9339e61d5645
227+
type | Type of logging. "logs" or "request" | string |
228+
component_id | Uniquely identifies the software component that has processed the current request | string | 9e6f3ecf-def0-4baf-8fac-9339e61d5645
182229
component_name | A human-friendly name representing the software component | string | my-fancy-component
183230
component_instance | Instance's index of horizontally scaled service | string | 0
184231

@@ -207,6 +254,7 @@ remote_host | host name of the consumer (might be a proxy, might be the actual
207254
remote_port | Which TCP port is used by the consumer to establish a connection to the remote producer. | string | 1234
208255
remote_user | The username associated with the request | string | user_name
209256
request_size_b | The size in bytes of the requesting entity or "body" (e.g., in case of POST requests). | long | 1234
257+
response_size_b | The size in bytes of the response entity | long | 1234
210258
response_status | The status code of the response. | long | 200
211259
response_content_type | The MIME type associated with the entity of the response if available/specified | long | application/json
212260
referer | For HTTP requests, identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. | string | /index.html

json_logging/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def format(self, record):
213213
"request_received_at": record.request_info.request_received_at,
214214
"response_time_ms": record.request_info.response_time_ms,
215215
"response_status": record.request_info.response_status,
216-
"response_size_b": record.request_info.response_content_type,
216+
"response_size_b": record.request_info.response_size_b,
217217
"response_content_type": record.request_info.response_content_type,
218218
"response_sent_at": record.request_info.response_sent_at}
219219
return JSON_SERIALIZER(json_log_object)

0 commit comments

Comments
 (0)