Skip to content

Commit 012f28c

Browse files
Add Styling Guide to documentation
This commit introduces a new Styling Guide to the project documentation. The guide (`docs/styling_guide.md`) is generated based on an analysis of the project's existing linter (Pylint) and formatter (Black) configurations (`pylintrc`, `pyproject.toml`) as well as Mypy settings. It summarizes key conventions, including: - Code formatting with Black (100-char line length). - Naming conventions (snake_case, PascalCase, etc.). - Indentation (4 spaces) and line endings (LF). - Docstring expectations. - Type checking enforcement with Mypy. The new guide has been linked from the Contributing page (`docs/contributing.md`) and added to the site navigation in `mkdocs.yml`.
1 parent 1f0cbd0 commit 012f28c

5 files changed

Lines changed: 44 additions & 24 deletions

File tree

docs/contributing.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The following are things that can be worked on without an existing issue:
2525

2626
### 2. Fork the repository and make your changes
2727

28-
We don't have styling documentation, so where possible try to match existing code. This includes the use of "headings" and "dividers" (this will make sense when you look at the code).
28+
Code style is enforced using `black` for formatting, `pylint` for linting, and `mypy` for type checking. Please ensure your changes pass these checks. For a summary of the main conventions, see the [Styling Guide](styling_guide.md). Otherwise, try to match existing code. This includes the use of "headings" and "dividers" (this will make sense when you look at the code).
2929

3030
All devlopment tooling can be installed (usually into a virtual environment), using the `dev` optional dependency:
3131

@@ -47,10 +47,14 @@ mypy src tests
4747
pytest
4848
```
4949
50-
If making changes to the documentation you can preview the changes locally using `mkdocs`. Changes to the README can be previewed using [`grip`](https://github.com/joeyespo/grip) (not included in `dev` dependencies).
50+
The above commands (`black`, `pylint`, `mypy`, `pytest`) should all be run before submitting a pull request.
51+
52+
If making changes to the documentation you can preview the changes locally using `mkdocs`. Changes to the `README.md` can be previewed using a tool like [`grip`](https://github.com/joeyespo/grip) (installable via `pip install grip`).
5153
5254
```shell
5355
mkdocs serve
56+
# For README preview (after installing grip):
57+
# grip
5458
```
5559
5660
!!! note

docs/cookbook.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ You can modify the `dict` of data that will be logged by overriding the `process
3232

3333
```python
3434
class SillyFormatter(JsonFormatter):
35-
def process_log_record(log_record):
35+
def process_log_record(self, log_record):
3636
new_record = {k[::-1]: v for k, v in log_record.items()}
3737
return new_record
3838
```
@@ -92,7 +92,7 @@ def generate_request_id():
9292

9393
class RequestIdFilter(logging.Filter):
9494
def filter(self, record):
95-
record.record_id = get_request_id()
95+
record.request_id = get_request_id() # Add request_id to the LogRecord
9696
return True
9797

9898
request_id_filter = RequestIdFilter()
@@ -137,9 +137,11 @@ def main_3():
137137
main_3()
138138
```
139139

140-
## Using `fileConfig`
140+
## Using Dictionary-based Configuration (e.g., from YAML)
141+
142+
While Python's [`logging.config.fileConfig`](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig) is designed for INI-style configuration files, [`logging.config.dictConfig`](https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig) is used for dictionary-based configurations, often loaded from YAML or JSON files.
141143

142-
To use the module with a yaml config file using the [`fileConfig` function](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig), use the class `pythonjsonlogger.json.JsonFormatter`. Here is a sample config file:
144+
To use `python-json-logger` with such a configuration, you specify the formatter class (e.g., `pythonjsonlogger.json.JsonFormatter`) in your dictionary. Here is a sample configuration loaded from a YAML file:
143145

144146
```yaml title="example_config.yaml"
145147
version: 1

docs/index.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,53 @@ This library assumes that you are famliar with the `logging` standard library pa
2323
- **Fully Customizable Output Fields:** Control required, excluded, and static fields including automatically picking up custom attributes on `LogRecord` objects. Fields can be renamed before they are output.
2424
- **Encode Any Type:** Encoders are customized to ensure that something sane is logged for any input including those that aren't supported by default. For example formatting UUID objects into their string representation and bytes objects into a base 64 encoded string.
2525

26-
## Quick Start
26+
## Getting Started
2727

28-
Follow our [Quickstart Guide](quickstart.md).
28+
Jump right in with our [Quickstart Guide](quickstart.md) to get `python-json-logger` integrated into your project quickly.
2929

30-
```python title="TLDR"
30+
Here's a small taste of what it looks like:
31+
32+
```python title="Example Usage"
3133
import logging
3234
from pythonjsonlogger.json import JsonFormatter
3335

3436
logger = logging.getLogger()
3537
logger.setLevel(logging.INFO)
3638

3739
handler = logging.StreamHandler()
40+
# Note: The JsonFormatter class is available from pythonjsonlogger.json,
41+
# pythonjsonlogger.orjson, and pythonjsonlogger.msgspec
3842
handler.setFormatter(JsonFormatter())
3943

4044
logger.addHandler(handler)
4145

42-
logger.info("Logging using pythonjsonlogger!", extra={"more_data": True})
43-
44-
# {"message": "Logging using pythonjsonlogger!", "more_data": true}
46+
logger.info("Logging using python-json-logger!", extra={"more_data": True})
47+
# Expected output:
48+
# {"message": "Logging using python-json-logger!", "more_data": true}
4549
```
4650

51+
## Where to Go Next
4752

48-
## Bugs, Feature Requests etc
49-
Please [submit an issue on github](https://github.com/nhairs/python-json-logger/issues).
53+
* **[Quickstart Guide](quickstart.md):** For installation and basic setup.
54+
* **[Cookbook](cookbook.md):** For more advanced usage patterns and recipes.
55+
* **API Reference:** Dive into the details of specific formatters, functions, and classes (see navigation menu).
56+
* **[Contributing Guidelines](contributing.md):** If you'd like to contribute to the project.
57+
* **[Changelog](changelog.md):** To see what's new in recent versions.
5058

51-
In the case of bug reports, please help us help you by following best practices [^1^](https://marker.io/blog/write-bug-report/) [^2^](https://www.chiark.greenend.org.uk/~sgtatham/bugs.html).
59+
## Project Information
5260

53-
In the case of feature requests, please provide background to the problem you are trying to solve so that we can a solution that makes the most sense for the library as well as your use case.
61+
### Bugs, Feature Requests, etc.
62+
Please [submit an issue on GitHub](https://github.com/nhairs/python-json-logger/issues).
5463

55-
## License
64+
In the case of bug reports, please help us help you by following best practices [^1^](https://marker.io/blog/write-bug-report/) [^2^](https://www.chiark.greenend.org.uk/~sgtatham/bugs.html).
5665

57-
This project is licensed under the BSD 2 Clause License - see [`LICENSE`](https://github.com/nhairs/python-json-logger/blob/main/LICENSE)
66+
In the case of feature requests, please provide background to the problem you are trying to solve so that we can find a solution that makes the most sense for the library as well as your use case.
5867

59-
## Authors and Maintainers
68+
### License
69+
This project is licensed under the BSD 2 Clause License - see the [LICENSE file](https://github.com/nhairs/python-json-logger/blob/main/LICENSE) on GitHub.
6070

61-
This project was originally authored by [Zakaria Zajac](https://github.com/madzak) and our wonderful [contributors](https://github.com/nhairs/python-json-logger/graphs/contributors)
71+
### Authors and Maintainers
72+
This project was originally authored by [Zakaria Zajac](https://github.com/madzak) and our wonderful [contributors](https://github.com/nhairs/python-json-logger/graphs/contributors).
6273

6374
It is currently maintained by:
64-
6575
- [Nicholas Hairs](https://github.com/nhairs) - [nicholashairs.com](https://www.nicholashairs.com)

docs/quickstart.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ formatter = JsonFormatter(
8989
defaults={"environment": "dev"}
9090
)
9191
# ...
92-
logger.info("this overwrites the environment field", extras={"environment": "dev"})
92+
# logger.info("this message will have environment=dev by default")
93+
logger.info("this overwrites the environment field", extra={"environment": "prod"})
9394
```
9495

9596
#### Static Fields

mkdocs.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ nav:
1616
- changelog.md
1717
- security.md
1818
- contributing.md
19+
- styling_guide.md
1920
- API Reference:
2021
- ... | reference/pythonjsonlogger/*
2122

@@ -107,8 +108,10 @@ plugins:
107108
unwrap_annotated: true
108109
show_source: false
109110
docstring_section_style: spacy
110-
- literate-nav:
111-
nav_file: SUMMARY.txt
111+
# literate-nav was configured with a missing SUMMARY.txt and might be redundant
112+
# due to the explicit nav structure and awesome-pages.
113+
# - literate-nav:
114+
# nav_file: SUMMARY.txt
112115
- mike:
113116
canonical_version: latest
114117

0 commit comments

Comments
 (0)