cham-logging 0.1.0__py3-none-any.whl
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.
cham_logging/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MicrosecondFormatter(logging.Formatter):
|
|
9
|
+
def formatTime( # noqa: N802
|
|
10
|
+
self,
|
|
11
|
+
record: logging.LogRecord,
|
|
12
|
+
datefmt: str | None = None,
|
|
13
|
+
) -> str:
|
|
14
|
+
dt = datetime.datetime.fromtimestamp(record.created, datetime.UTC)
|
|
15
|
+
return dt.strftime(datefmt) if datefmt else dt.isoformat()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
FORMATTER = MicrosecondFormatter("%(asctime)s - %(levelname)s - %(message)s")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def init_logger() -> None:
|
|
22
|
+
log_level = os.environ.get("LOG_LEVEL", "INFO").upper()
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger()
|
|
25
|
+
logger.setLevel(log_level)
|
|
26
|
+
|
|
27
|
+
handler = logging.StreamHandler()
|
|
28
|
+
handler.setLevel(log_level)
|
|
29
|
+
handler.setFormatter(FORMATTER)
|
|
30
|
+
logger.addHandler(handler)
|
|
31
|
+
|
|
32
|
+
log_file = os.environ.get("LOG_FILE")
|
|
33
|
+
if log_file:
|
|
34
|
+
file_handler = logging.FileHandler(log_file, encoding="utf-8")
|
|
35
|
+
file_handler.setLevel(log_level)
|
|
36
|
+
file_handler.setFormatter(FORMATTER)
|
|
37
|
+
logger.addHandler(file_handler)
|
cham_logging/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cham-logging
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small stdlib-only helper for configuring Python logging.
|
|
5
|
+
Author: Cham
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# cham-logging
|
|
12
|
+
|
|
13
|
+
A small stdlib-only helper for configuring Python logging.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Call `init_logger()` once near the start of your process.
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import logging
|
|
21
|
+
|
|
22
|
+
from cham_logging.bootstrap import init_logger
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
init_logger()
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
logger.info("service started")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Environment variables
|
|
32
|
+
|
|
33
|
+
`LOG_LEVEL` controls the root logger and handler level.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
LOG_LEVEL=DEBUG python -m your_app
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If `LOG_LEVEL` is not set, `INFO` is used.
|
|
40
|
+
|
|
41
|
+
`LOG_FILE` enables an additional UTF-8 file handler.
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
LOG_FILE=app.log python -m your_app
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Both variables can be used together.
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
LOG_LEVEL=DEBUG LOG_FILE=app.log python -m your_app
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Format
|
|
54
|
+
|
|
55
|
+
Logs use UTC with microsecond precision:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
2026-06-14T13:30:00.123456+00:00 - INFO - service started
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The timezone is fixed to UTC for now and cannot be configured.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
cham_logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
cham_logging/bootstrap.py,sha256=5-s5KAcyZwbns5s0lBuXhczZdkUEpamiJxXljiF8Lxo,1030
|
|
3
|
+
cham_logging/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
cham_logging-0.1.0.dist-info/METADATA,sha256=5rp1-ikUMBAO3iPRo3UTx3ABbxCsjNQ9CU4byJ-kEaQ,1124
|
|
5
|
+
cham_logging-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
6
|
+
cham_logging-0.1.0.dist-info/licenses/LICENSE,sha256=R2JKtKOoqKohwk31ygpA8Udqk6nZjU-sWPhtwYxj0M8,1061
|
|
7
|
+
cham_logging-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cham
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|