|
| 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