appn-logging 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ # --- Secrets and credentials: never commit (§3.3, §3.9) ---
2
+ .env
3
+ .env.*
4
+ !.env.example
5
+ *.pem
6
+ *.key
7
+ *.p12
8
+ *.pfx
9
+ credentials.json
10
+ service-account*.json
11
+ .netrc
12
+
13
+ # --- Research data and large artefacts ---
14
+ # Data lives in object storage, not git. Use Git LFS only where versioning a
15
+ # binary is genuinely required.
16
+ data/
17
+ *.csv
18
+ *.parquet
19
+ *.sqlite
20
+ *.db
21
+ *.dump
22
+ *.sql.gz
23
+ !tests/**/*.csv
24
+ !tests/**/*.parquet
25
+
26
+ # --- Local environment and tooling ---
27
+ .direnv/
28
+ .tool-versions.local
29
+
30
+ # --- Editors and OS ---
31
+ .vscode/
32
+ !.vscode/extensions.json
33
+ !.vscode/settings.shared.json
34
+ .idea/
35
+ *.swp
36
+ *.swo
37
+ .DS_Store
38
+ Thumbs.db
39
+
40
+ # --- Logs and scratch ---
41
+ *.log
42
+ logs/
43
+ tmp/
44
+ scratch/
45
+
46
+ # Python-specific. Append the shared .gitignore (secrets, data, editors, OS)
47
+ # from templates/shared/.gitignore.
48
+
49
+ # --- Byte-compiled and caches ---
50
+ __pycache__/
51
+ *.py[cod]
52
+ *$py.class
53
+ .pytest_cache/
54
+ .ruff_cache/
55
+ .mypy_cache/
56
+ .dmypy.json
57
+ .pytype/
58
+ .cython_debug/
59
+
60
+ # --- Distribution and build ---
61
+ build/
62
+ dist/
63
+ sdist/
64
+ wheels/
65
+ *.egg-info/
66
+ *.egg
67
+ .eggs/
68
+ MANIFEST
69
+
70
+ # --- Environments ---
71
+ .venv/
72
+ venv/
73
+ env/
74
+ ENV/
75
+ .python-version.local
76
+
77
+ # --- Coverage and profiling ---
78
+ .coverage
79
+ .coverage.*
80
+ coverage.xml
81
+ htmlcov/
82
+ .prof
83
+
84
+ # --- Notebooks (§4.4: clear outputs before committing) ---
85
+ .ipynb_checkpoints/
86
+ *-checkpoint.ipynb
87
+
88
+ # --- Tooling ---
89
+ .tox/
90
+ .nox/
91
+ .hypothesis/
92
+ .pdm-python
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,346 @@
1
+ Metadata-Version: 2.5
2
+ Name: appn-logging
3
+ Version: 0.1.0
4
+ Summary: Structured logging for Australian Plant Phenomics Network software: JSON for machines, text for people.
5
+ Project-URL: Homepage, https://github.com/aus-plant-phenomics-network/appn-core/tree/main/appn_logging
6
+ Project-URL: Repository, https://github.com/aus-plant-phenomics-network/appn-core
7
+ Project-URL: Issues, https://github.com/aus-plant-phenomics-network/appn-core/issues
8
+ Project-URL: Changelog, https://github.com/aus-plant-phenomics-network/appn-core/blob/main/CHANGELOG.md
9
+ Author-email: Australian Plant Phenomics Network <alisha.aneja@adelaide.edu.au>
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: appn,json,logging,plant-phenomics,structured-logging
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: System :: Logging
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.11
22
+ Provides-Extra: dev
23
+ Requires-Dist: build>=1.2; extra == 'dev'
24
+ Requires-Dist: hypothesis>=6.100; extra == 'dev'
25
+ Requires-Dist: mypy>=1.13; extra == 'dev'
26
+ Requires-Dist: pip-tools>=7.4; extra == 'dev'
27
+ Requires-Dist: pre-commit>=4.0; extra == 'dev'
28
+ Requires-Dist: pytest-cov>=6.0; extra == 'dev'
29
+ Requires-Dist: pytest>=8.3; extra == 'dev'
30
+ Requires-Dist: ruff>=0.8; extra == 'dev'
31
+ Requires-Dist: twine>=6.0; extra == 'dev'
32
+ Description-Content-Type: text/markdown
33
+
34
+ # appn-logging
35
+
36
+ Logging for Australian Plant Phenomics Network (APPN) software. It makes every
37
+ APPN program write its logs the same way, with the same field names, so logs
38
+ from different programs can be searched and joined together. Logs are written
39
+ as JSON for machines or as plain text for people, and whoever runs a program
40
+ chooses which. It has no dependencies beyond the Python standard library.
41
+
42
+ ## Status
43
+
44
+ **Active** (alpha: the API may change before 1.0), maintained by the APPN data
45
+ team. Contact: **alisha.aneja@adelaide.edu.au**
46
+
47
+ ## Install
48
+
49
+ Requires **Python 3.11 or later**.
50
+
51
+ ```bash
52
+ pip install appn-logging
53
+ ```
54
+
55
+ Or as a dependency in `pyproject.toml`:
56
+
57
+ ```toml
58
+ dependencies = ["appn-logging>=0.1,<1.0"]
59
+ ```
60
+
61
+ ## Use
62
+
63
+ ### Quick start
64
+
65
+ Add two lines to the start of your script:
66
+
67
+ ```python
68
+ from appn_logging import configure_logging
69
+
70
+ configure_logging()
71
+ ```
72
+
73
+ Then log with Python's own `logging`, as you would anyway:
74
+
75
+ ```python
76
+ import logging
77
+
78
+ logger = logging.getLogger(__name__)
79
+ logger.info("Read %d rows", 120)
80
+ ```
81
+
82
+ To read the logs yourself, ask for text when you run the script:
83
+
84
+ ```bash
85
+ APPN_LOGGING_FORMAT=text python my_script.py
86
+ ```
87
+
88
+ ```text
89
+ 2026-11-10T03:12:45.120+00:00 INFO __main__: Read 120 rows
90
+ ```
91
+
92
+ That is all most scripts need. The rest of this section is reference.
93
+
94
+ ### In a program
95
+
96
+ Configure logging **once**, at the program's entry point:
97
+
98
+ ```python
99
+ import logging
100
+
101
+ from appn_logging import configure_logging, log_context, new_run_id
102
+
103
+ logger = logging.getLogger(__name__)
104
+
105
+
106
+ def main() -> None:
107
+ configure_logging(service="appn-ingest", environment="production", version="1.4.0")
108
+ with log_context(run_id=new_run_id()):
109
+ logger.info("Fetched page %d", 3, extra={"record_count": 100})
110
+ ```
111
+
112
+ That writes this line to stderr (wrapped here for reading):
113
+
114
+ ```json
115
+ {"timestamp": "2026-11-10T03:12:45.120+00:00", "level": "INFO",
116
+ "logger": "appn_ingest.cli", "message": "Fetched page 3",
117
+ "service": "appn-ingest", "environment": "production", "version": "1.4.0",
118
+ "run_id": "0b9f0c0e-6f1d-4a43-9d39-6b1f6c1f2f77", "record_count": 100,
119
+ "log_schema_version": 1}
120
+ ```
121
+
122
+ Everywhere else, write log lines with Python's built-in `logging`. You do not
123
+ need to import `appn_logging` to log:
124
+
125
+ ```python
126
+ logger = logging.getLogger(__name__)
127
+ logger.info("Fetched page %d", page, extra={"record_count": 100})
128
+ ```
129
+
130
+ - **`extra=`** fields become fields in the line.
131
+ - **`log_context(...)`** adds fields to every line logged inside the `with`
132
+ block. It is safe with threads and asyncio: concurrent runs do not see each
133
+ other's fields.
134
+ - **Crashes and warnings are log lines too.** An exception nobody caught is
135
+ logged at `CRITICAL` with its traceback, in the main thread and in other
136
+ threads, and the program still exits with an error. Python warnings are
137
+ logged at `WARNING` by the `py.warnings` logger.
138
+ - **Importing `appn_logging` changes nothing.** Only `configure_logging()`
139
+ touches logging configuration, and only a program's entry point should call
140
+ it. A library must never call it.
141
+ - **stdout is left alone**, so a command-line tool can be piped without log
142
+ lines mixed into its output.
143
+
144
+ ### JSON or text
145
+
146
+ | Format | For | Looks like |
147
+ |---|---|---|
148
+ | `json` (default) | Servers, pipelines, anything whose logs are collected and searched | One JSON object per line |
149
+ | `text` | A person watching a terminal | `timestamp LEVEL logger: message key=value ...` |
150
+
151
+ Both carry the same information and the same redaction. Text leaves out the
152
+ fields that are identical on every line (`service`, `environment`, `version`,
153
+ `log_schema_version`).
154
+
155
+ Choose the format with `APPN_LOGGING_FORMAT`, or with `log_format=` for a
156
+ program's default. Leave the default as `json` for anything that runs
157
+ unattended: the text layout is for reading and may change between versions.
158
+
159
+ ### Reading JSON logs: `appn-logs`
160
+
161
+ Logs that were written as JSON can be shown as text afterwards. Installing
162
+ this package adds the `appn-logs` command:
163
+
164
+ ```bash
165
+ appn-ingest 2>&1 | appn-logs # as the program runs
166
+ appn-logs run.log # from a file
167
+ appn-logs --level WARNING run.log # only warnings and worse
168
+ ```
169
+
170
+ Lines that are not JSON log lines are shown unchanged, so it is safe on mixed
171
+ output.
172
+
173
+ ### Collecting records for a report
174
+
175
+ Some log records are also wanted somewhere else: the problems to list in a
176
+ report for the user, or whether a run produced any warnings.
177
+ `collect_records()` gathers the records logged inside a block. The code logs
178
+ as normal, and the records are still written to the log.
179
+
180
+ ```python
181
+ from appn_logging import collect_records
182
+
183
+ with collect_records(key="issue_type") as issues:
184
+ build_vocabulary()
185
+
186
+ for issue in issues:
187
+ report.write(f"{issue.levelname}: {issue.getMessage()}\n")
188
+ ```
189
+
190
+ ```python
191
+ # Anywhere inside build_vocabulary():
192
+ logger.error("Sheet %s has no name column", sheet, extra={"issue_type": "no_name"})
193
+ ```
194
+
195
+ | Argument | Collects |
196
+ |---|---|
197
+ | none | Every record |
198
+ | `key="issue_type"` | Records carrying that field in `extra=`. The name is yours to choose |
199
+ | `level="WARNING"` | Records at that level or above |
200
+ | `match=function` | Records for which the function returns true |
201
+ | `logger="appn_ingest"` | Records from that logger and the loggers below it |
202
+
203
+ A record must satisfy every argument given. Two things to know:
204
+
205
+ - Only records from the current thread or asyncio task are collected, so two
206
+ runs collecting at the same time do not receive each other's records.
207
+ Records from threads started inside the block are not collected.
208
+ - A collector sees only what logging lets through: a `DEBUG` record is not
209
+ collected while the level is `INFO`.
210
+
211
+ ## Configure
212
+
213
+ Every setting has a default, so `configure_logging()` with no arguments works.
214
+ A program sets its own defaults with keyword arguments:
215
+
216
+ | Argument | Default | Purpose |
217
+ |---|---|---|
218
+ | `service` | The name the program was started with | Name of the program, written to every line |
219
+ | `level` | `"INFO"` | Root logger level, as a name or a number |
220
+ | `log_format` | `"json"` | `"json"` or `"text"` |
221
+ | `stream` | `sys.stderr` | Where lines are written |
222
+ | `environment` | not written | Deployment environment, e.g. `"production"` |
223
+ | `version` | not written | Version of the program |
224
+ | `quiet_loggers` | `httpx`, `httpcore`, `urllib3` | Loggers raised to WARNING because they log every request at INFO |
225
+ | `logger_levels` | none | Levels for individual loggers, e.g. `{"appn_ingest.fetch": "DEBUG"}`. Wins over `level` and `quiet_loggers` |
226
+ | `extra_sensitive_keys` | none | Extra key names to redact (see below) |
227
+
228
+ Whoever runs the program can then change its logging without editing it.
229
+ `configure_logging()` always reads these environment variables, and **a
230
+ variable that is set wins over the argument**:
231
+
232
+ | Variable | Required | Default | Purpose |
233
+ |---|---|---|---|
234
+ | `APPN_LOGGING_LEVEL` | No | `INFO` | Logging verbosity: `DEBUG`, `INFO`, `WARNING`, `ERROR` or `CRITICAL` |
235
+ | `APPN_LOGGING_FORMAT` | No | `json` | `json` or `text` |
236
+ | `APPN_LOGGING_SERVICE` | No | The name the program was started with | Name of the program |
237
+ | `APPN_LOGGING_ENVIRONMENT` | No | not written | Deployment environment |
238
+ | `APPN_LOGGING_LOGGER_LEVELS` | No | none | Levels for individual loggers, as comma-separated `name=LEVEL` pairs. Added to `logger_levels` |
239
+
240
+ Values may be in any letter case. An unset or empty variable is ignored.
241
+
242
+ `APPN_LOGGING_LEVEL` sets the level for everything. `APPN_LOGGING_LOGGER_LEVELS`
243
+ then changes it for named loggers, and for the loggers below them: `appn_ingest`
244
+ also covers `appn_ingest.fetch`. For example, to see everything one module of
245
+ your own program does and silence a noisy library:
246
+
247
+ ```bash
248
+ export APPN_LOGGING_LEVEL=INFO
249
+ export APPN_LOGGING_LOGGER_LEVELS="appn_ingest.fetch=DEBUG,botocore=ERROR"
250
+ ```
251
+
252
+ A logger's name is the `logger` field of the lines it writes. A value that is
253
+ not recognised stops the program at startup with an error that names the
254
+ variable, for example
255
+ `APPN_LOGGING_FORMAT: unknown log format: 'xml' (expected one of: json, text)`.
256
+
257
+ There are no secrets in this configuration.
258
+
259
+ ## Redaction
260
+
261
+ A value is replaced with `[REDACTED]` when its key contains any of `token`,
262
+ `password`, `passwd`, `secret`, `authorization`, `api_key` / `apikey`,
263
+ `private_key`, `credential` or `cookie`, in any letter case. This applies to
264
+ `extra=` fields, `log_context()` fields, and dictionaries nested inside them,
265
+ including dictionaries inside lists. It applies to both output formats.
266
+
267
+ ```python
268
+ logger.info("Request sent", extra={"headers": {"Authorization": "Bearer abc"}})
269
+ # ... "headers": {"Authorization": "[REDACTED]"} ...
270
+ ```
271
+
272
+ Add your own key names with `extra_sensitive_keys=["orcid"]`.
273
+
274
+ Redaction is a safety net, not a licence. It looks at **key names only**: a
275
+ secret inside the log message text, or under an innocent-looking key, is
276
+ written as is. Never pass a secret to a logger.
277
+
278
+ The match is deliberately broad, so a harmless field such as `token_count` is
279
+ redacted too. Rename the field (`n_tokens`, say) if you need its value.
280
+
281
+ ## The log line format
282
+
283
+ This is the JSON format, and the contract that log aggregation depends on.
284
+
285
+ | Field | Always present | Meaning |
286
+ |---|---|---|
287
+ | `timestamp` | Yes | When the record was created: ISO 8601, UTC, milliseconds |
288
+ | `level` | Yes | `DEBUG`, `INFO`, `WARNING`, `ERROR` or `CRITICAL` |
289
+ | `logger` | Yes | Name of the logger, normally the module's dotted path |
290
+ | `message` | Yes | The formatted message |
291
+ | `service` | Yes | From `configure_logging(service=...)` |
292
+ | `environment` | If configured | From `configure_logging(environment=...)` |
293
+ | `version` | If configured | From `configure_logging(version=...)` |
294
+ | `exception` | If logged with an exception, and for uncaught exceptions | The traceback, as one string |
295
+ | `log_schema_version` | Yes | Version of this table. Currently `1` |
296
+ | anything else | — | Fields from `log_context()` and `extra=` |
297
+
298
+ - A caller's field with the same name as one in this table is written with an
299
+ `extra_` prefix (`extra_service`), so it cannot overwrite the real one.
300
+ - **A logging call never fails because of the value it was given.** This is
301
+ tested with thousands of generated values on every run of the test suite.
302
+ A value that cannot be written is replaced with `[UNPRINTABLE]`.
303
+ - **Numbers stay numbers**, including numpy's: `numpy.int64(5)` is written as
304
+ `5`. Dates and times are written in ISO 8601. `NaN` and infinity, which JSON
305
+ does not have, are written as strings. Anything else is written as its
306
+ printed form, so a `Path` is its path.
307
+ - **Size is limited**, so one careless call cannot write an enormous line. What
308
+ is cut is always marked with `[TRUNCATED]`:
309
+
310
+ | What | Limit |
311
+ |---|---|
312
+ | The message | 10,000 characters |
313
+ | One string value, or one printed object such as a table | 2,000 characters |
314
+ | One list or dictionary | 100 items |
315
+ | Nesting | 8 levels, then `[TOO DEEP]` |
316
+ - Renaming or removing a field in this table increases `log_schema_version` and
317
+ is released as a new **major** version of the package. Adding a field is a
318
+ minor version.
319
+
320
+ ## Test
321
+
322
+ From this directory, in a virtual environment:
323
+
324
+ ```bash
325
+ python3 -m venv .venv
326
+ source .venv/bin/activate
327
+ make install # locked dev tools, this package (editable), pre-commit hooks
328
+ make check # lint, type check, tests, format check: the same as CI
329
+ ```
330
+
331
+ Rerun `make install` after `requirements-dev.txt` changes. Code changes need no
332
+ reinstall. Do not set `PYTHONPATH`; the editable install handles imports.
333
+
334
+ ## Release
335
+
336
+ Releases are published to [PyPI](https://pypi.org/project/appn-logging/) by the
337
+ `Release` GitHub Actions workflow when a tag named `v<version>` is pushed. The steps are in
338
+ [CONTRIBUTING.md](https://github.com/aus-plant-phenomics-network/appn-core/blob/main/CONTRIBUTING.md#releasing).
339
+
340
+ ## Licence and citation
341
+
342
+ Licensed under Apache-2.0. See
343
+ [LICENSE](https://github.com/aus-plant-phenomics-network/appn-core/blob/main/appn_logging/LICENSE).
344
+
345
+ If you use this software in published work, please cite it using the metadata in
346
+ [CITATION.cff](https://github.com/aus-plant-phenomics-network/appn-core/blob/main/CITATION.cff).