Skip to content

Commit d667c08

Browse files
authored
Merge pull request #29 from basepi/docs
Add official docs
2 parents ee288d8 + 7383073 commit d667c08

File tree

4 files changed

+232
-1
lines changed

4 files changed

+232
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,10 @@ dmypy.json
129129
.pyre/
130130

131131
# JUnit file
132-
junit-test.xml
132+
junit-test.xml
133+
134+
# VSCode
135+
.vscode/
136+
137+
# Doc build
138+
html_docs

docs/index.asciidoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
:ecs-repo-dir: {ecs-logging-root}/docs/
2+
3+
include::{docs-root}/shared/versions/stack/current.asciidoc[]
4+
include::{docs-root}/shared/attributes.asciidoc[]
5+
6+
ifdef::env-github[]
7+
NOTE: For the best reading experience,
8+
please view this documentation at https://www.elastic.co/guide/en/ecs-logging/python/current/index.html[elastic.co]
9+
endif::[]
10+
11+
= ECS Logging Python Reference
12+
13+
ifndef::env-github[]
14+
include::./intro.asciidoc[Introduction]
15+
include::./setup.asciidoc[Set up]
16+
endif::[]

docs/intro.asciidoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[intro]]
2+
== Introduction
3+
4+
ECS loggers are formatter/encoder plugins for your favorite logging libraries.
5+
They make it easy to format your logs into ECS-compatible JSON.
6+
7+
TIP: Want to learn more about ECS, ECS logging, and other available language plugins?
8+
See the {ecs-logging-ref}/intro.html[ECS logging guide].
9+
10+
Ready to jump into `ecs-logging-python`? <<installation,Get started>>.

docs/setup.asciidoc

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
[[installation]]
2+
== Installation
3+
4+
[source,cmd]
5+
----
6+
$ python -m pip install ecs-logging
7+
----
8+
9+
[float]
10+
[[gettingstarted]]
11+
== Getting Started
12+
13+
`ecs-logging-python` has formatters for the standard library
14+
https://docs.python.org/3/library/logging.html[`logging`] module
15+
and the https://www.structlog.org/en/stable/[`structlog`] package.
16+
17+
[float]
18+
[[logging]]
19+
=== Standard Library `logging` Module
20+
21+
[source,python]
22+
----
23+
import logging
24+
import ecs_logging
25+
26+
# Get the Logger
27+
logger = logging.getLogger("app")
28+
logger.setLevel(logging.DEBUG)
29+
30+
# Add an ECS formatter to the Handler
31+
handler = logging.StreamHandler()
32+
handler.setFormatter(ecs_logging.StdlibFormatter())
33+
logger.addHandler(handler)
34+
35+
# Emit a log!
36+
logger.debug("Example message!", extra={"http.request.method": "get"})
37+
----
38+
39+
[source,json]
40+
----
41+
{
42+
"@timestamp": "2020-03-20T18:11:37.895Z",
43+
"ecs": {
44+
"version": "1.6.0"
45+
},
46+
"http": {
47+
"request": {
48+
"method": "get"
49+
}
50+
},
51+
"log": {
52+
"level": "debug",
53+
"logger": "app",
54+
"origin": {
55+
"file": {
56+
"line": 14,
57+
"name": "test.py"
58+
},
59+
"function": "func"
60+
},
61+
"original": "Example message!"
62+
},
63+
"message": "Example message!"
64+
}
65+
----
66+
67+
[float]
68+
==== Excluding Fields
69+
70+
You can exclude fields from being collected by using the `exclude_fields` option
71+
in the `StdlibFormatter` constructor:
72+
73+
[source,python]
74+
----
75+
from ecs_logging import StdlibFormatter
76+
77+
formatter = StdlibFormatter(
78+
exclude_fields=[
79+
# You can specify individual fields to ignore:
80+
"log.original",
81+
# or you can also use prefixes to ignore
82+
# whole categories of fields:
83+
"process",
84+
"log.origin",
85+
]
86+
)
87+
----
88+
89+
[float]
90+
==== Limiting Stack Traces
91+
92+
The `StdlibLogger` automatically gathers `exc_info` into ECS `error.*` fields.
93+
If you'd like to control the number of stack frames that are included
94+
in `error.stack_trace` you can use the `stack_trace_limit` parameter
95+
(by default all frames are collected):
96+
97+
[source,python]
98+
----
99+
from ecs_logging import StdlibFormatter
100+
101+
formatter = StdlibFormatter(
102+
# Only collects 3 stack frames
103+
stack_trace_limit=3,
104+
)
105+
formatter = StdlibFormatter(
106+
# Disable stack trace collection
107+
stack_trace_limit=0,
108+
)
109+
----
110+
111+
[float]
112+
[[structlog]]
113+
=== Structlog Example
114+
115+
[source,python]
116+
----
117+
import structlog
118+
import ecs_logging
119+
120+
# Configure Structlog
121+
structlog.configure(
122+
processors=[ecs_logging.StructlogFormatter()],
123+
wrapper_class=structlog.BoundLogger,
124+
context_class=dict,
125+
logger_factory=structlog.PrintLoggerFactory(),
126+
)
127+
128+
# Get the Logger
129+
logger = structlog.get_logger("app")
130+
131+
# Add additional context
132+
logger = logger.bind(**{
133+
"http": {
134+
"version": "2",
135+
"request": {
136+
"method": "get",
137+
"bytes": 1337,
138+
},
139+
},
140+
"url": {
141+
"domain": "example.com",
142+
"path": "/",
143+
"port": 443,
144+
"scheme": "https",
145+
"registered_domain": "example.com",
146+
"top_level_domain": "com",
147+
"original": "https://example.com",
148+
}
149+
})
150+
151+
# Emit a log!
152+
logger.debug("Example message!")
153+
----
154+
155+
[source,json]
156+
----
157+
{
158+
"@timestamp": "2020-03-26T13:08:11.728Z",
159+
"ecs": {
160+
"version": "1.6.0"
161+
},
162+
"http": {
163+
"request": {
164+
"bytes": 1337,
165+
"method": "get"
166+
},
167+
"version": "2"
168+
},
169+
"log": {
170+
"level": "debug"
171+
},
172+
"message": "Example message!",
173+
"url": {
174+
"domain": "example.com",
175+
"original": "https://example.com",
176+
"path": "/",
177+
"port": 443,
178+
"registered_domain": "example.com",
179+
"scheme": "https",
180+
"top_level_domain": "com"
181+
}
182+
}
183+
----
184+
185+
[float]
186+
[[correlation]]
187+
== Elastic APM Log Correlation
188+
189+
`ecs-logging-python` supports automatically collecting https://www.elastic.co/guide/en/ecs/master/ecs-tracing.html[ECS tracing fields]
190+
from the https://github.com/elastic/apm-agent-python[Elastic APM Python agent] in order to
191+
https://www.elastic.co/guide/en/apm/agent/python/current/log-correlation.html[correlate logs to spans, transactions and traces] in Elastic APM.
192+
193+
[float]
194+
[[filebeat]]
195+
== Install Filebeat
196+
197+
The best way to collect the logs once they are ECS-formatted is with https://www.elastic.co/beats/filebeat[Filebeat]:
198+
199+
include::{ecs-repo-dir}/setup.asciidoc[tag=configure-filebeat]

0 commit comments

Comments
 (0)