Commit 6884b59
[core] support DictConfigurator prefixes for rename_fields and static_fields (#45)
Python logging config supports access to
[external](https://docs.python.org/3/library/logging.config.html#access-to-external-objects)
and
[internal](https://docs.python.org/3/library/logging.config.html#access-to-internal-objects)
objects via
[DictConfiguator](https://docs.python.org/3/library/logging.config.html#configuration-functions)
prefixes.
This works internally by warpping the dict with a
[ConvertingDict](https://github.com/python/cpython/blob/main/Lib/logging/config.py#L337)
that requires an explicit call to it's
[\_\_getitem\_\_](https://github.com/python/cpython/blob/main/Lib/logging/config.py#L340)
dunder method for the
[conversion](https://github.com/python/cpython/blob/main/Lib/logging/config.py#L307)
to take place (using the specific converter for the specified prefix).
This allows, for example, having a *log_config.yaml* file like:
```yaml
version: 1
disable_existing_loggers: False
formatters:
default:
"()": pythonjsonlogger.json.JsonFormatter
format: "%(asctime)s %(levelname)s %(name)s %(module)s %(funcName)s %(lineno)s %(message)s"
rename_fields:
"asctime": "timestamp"
"levelname": "status"
static_fields:
"service": ext://logging_config.PROJECT_NAME
"env": ext://logging_config.ENVIRONMENT
"version": ext://logging_config.PROJECT_VERSION
"app_log": "true"
handlers:
default:
formatter: default
class: logging.StreamHandler
stream: ext://sys.stderr
access:
formatter: default
class: logging.StreamHandler
stream: ext://sys.stdout
loggers:
uvicorn.error:
level: INFO
handlers:
- default
propagate: no
uvicorn.access:
level: INFO
handlers:
- access
propagate: no
```
where *service*, *env* and *version* values are taken from the external
resource *logging_config.py*.
The content for *logging_config.py* could be something like (for getting
some values from project metadata or environment variables):
```python
import importlib.metadata
import os
def get_version_metadata():
# https://stackoverflow.com/a/78082532
version = importlib.metadata.version(PROJECT_NAME)
return version
PROJECT_NAME = 'test-api'
PROJECT_VERSION = get_version_metadata()
ENVIRONMENT = os.environ.get('ENVIRONMENT', 'dev')
```
---------
Co-authored-by: Nicholas Hairs <info@nicholashairs.com>1 parent 9e6cb67 commit 6884b59
File tree
5 files changed
+149
-26
lines changed- docs
- src/pythonjsonlogger
- tests
5 files changed
+149
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
7 | 15 | | |
8 | 16 | | |
9 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
147 | 180 | | |
148 | | - | |
149 | | - | |
| 181 | + | |
150 | 182 | | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
155 | 186 | | |
156 | | - | |
157 | | - | |
158 | 187 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
164 | 192 | | |
165 | | - | |
166 | | - | |
167 | 193 | | |
168 | | - | |
169 | | - | |
170 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
171 | 197 | | |
172 | 198 | | |
173 | 199 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
219 | 229 | | |
220 | | - | |
221 | 230 | | |
222 | 231 | | |
223 | 232 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
0 commit comments