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