small-logger 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.
- small_logger/__init__.py +4 -0
- small_logger/core.py +134 -0
- small_logger/py.typed +1 -0
- small_logger-0.1.0.dist-info/METADATA +131 -0
- small_logger-0.1.0.dist-info/RECORD +8 -0
- small_logger-0.1.0.dist-info/WHEEL +5 -0
- small_logger-0.1.0.dist-info/licenses/LICENSE +21 -0
- small_logger-0.1.0.dist-info/top_level.txt +1 -0
small_logger/__init__.py
ADDED
small_logger/core.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# small_logger/core.py
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
small_logger: tiny, opinionated Loguru setup for applications.
|
|
5
|
+
|
|
6
|
+
Features
|
|
7
|
+
--------
|
|
8
|
+
- Console + file sinks with platformdirs
|
|
9
|
+
- Rotation/retention/compression for files
|
|
10
|
+
- Multiline alignment (like your version)
|
|
11
|
+
- Safe for PyInstaller (no __file__ assumptions)
|
|
12
|
+
- Unhandled exception hook
|
|
13
|
+
- Optional JSON logs for shipping/CI
|
|
14
|
+
- Env-driven configuration overrides
|
|
15
|
+
- Intercept stdlib logging (optional)
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
import sys
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import TYPE_CHECKING, Literal
|
|
21
|
+
from platformdirs import user_log_dir
|
|
22
|
+
from loguru import logger as _loguru_logger
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from loguru import Logger, Record
|
|
26
|
+
|
|
27
|
+
__all__ = ["init_logger", "get_logger"]
|
|
28
|
+
|
|
29
|
+
_logger_initialized: bool = False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _custom_format(record: Record) -> str:
|
|
33
|
+
# Loguru treats angle brackets as color tags, so <module> must be escaped.
|
|
34
|
+
function = record["function"].replace(r"<module>", r"\<module>")
|
|
35
|
+
|
|
36
|
+
# Loguru interprets { and } in the returned format string as placeholders.
|
|
37
|
+
record["message"] = record["message"].replace("{", "{{").replace("}", "}}")
|
|
38
|
+
|
|
39
|
+
left = (
|
|
40
|
+
f"<green>{record['time']:YYYY-MM-DD HH:mm:ss}</green> | "
|
|
41
|
+
f"<level>{record['level']:<8}</level> | "
|
|
42
|
+
f"<cyan>{record['name']}:{function}:{record['line']}</cyan>"
|
|
43
|
+
)
|
|
44
|
+
left_padded = left.ljust(100) + " | "
|
|
45
|
+
|
|
46
|
+
lines = record["message"].splitlines()
|
|
47
|
+
|
|
48
|
+
if not lines:
|
|
49
|
+
return f"{left_padded}<level></level>\n"
|
|
50
|
+
|
|
51
|
+
if len(lines) == 1:
|
|
52
|
+
return f"{left_padded}<level>{lines[0].rstrip()}</level>\n"
|
|
53
|
+
|
|
54
|
+
indent = " " * max(57, 36 + len((record["name"] or "") + record["function"] + str(record["line"])))
|
|
55
|
+
formatted_lines = [f"{left_padded}<level>{lines[0].rstrip()}</level>"]
|
|
56
|
+
for line in lines[1:]:
|
|
57
|
+
formatted_lines.append(f"{indent} <level>{line.rstrip()}</level>")
|
|
58
|
+
|
|
59
|
+
return "\n".join(formatted_lines).rstrip() + "\n"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def init_logger(
|
|
63
|
+
level: Literal["TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"] = "INFO",
|
|
64
|
+
app_name: str | None = None,
|
|
65
|
+
file: bool | None = None,
|
|
66
|
+
) -> None:
|
|
67
|
+
"""Initialize the global logger with console and optional file sinks. Call once at startup.
|
|
68
|
+
|
|
69
|
+
Parameters
|
|
70
|
+
----------
|
|
71
|
+
level:
|
|
72
|
+
The logging level. Default is "INFO".
|
|
73
|
+
app_name:
|
|
74
|
+
Application name used to determine the log file location. Default is "slogger".
|
|
75
|
+
file:
|
|
76
|
+
Whether to log to a file. Defaults to True when app_name is given, False otherwise.
|
|
77
|
+
"""
|
|
78
|
+
global _logger_initialized
|
|
79
|
+
if _logger_initialized:
|
|
80
|
+
return
|
|
81
|
+
|
|
82
|
+
_loguru_logger.remove()
|
|
83
|
+
|
|
84
|
+
if app_name is None:
|
|
85
|
+
app_name = "slogger"
|
|
86
|
+
if file is None:
|
|
87
|
+
file = False
|
|
88
|
+
else:
|
|
89
|
+
if file is None:
|
|
90
|
+
file = True
|
|
91
|
+
|
|
92
|
+
level_upper = level.upper()
|
|
93
|
+
|
|
94
|
+
_loguru_logger.add( # type: ignore
|
|
95
|
+
sink=sys.stdout,
|
|
96
|
+
level=level_upper,
|
|
97
|
+
format=_custom_format,
|
|
98
|
+
enqueue=True, # Non-blocking, thread-safe.
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
if file:
|
|
102
|
+
log_path: Path = Path(user_log_dir(appname=app_name)) / "app.log"
|
|
103
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
104
|
+
_loguru_logger.add(
|
|
105
|
+
sink=log_path,
|
|
106
|
+
level=level_upper,
|
|
107
|
+
format=_custom_format, # type: ignore
|
|
108
|
+
rotation="00:00", # New file each day at midnight.
|
|
109
|
+
retention="7 days", # Deletion happens on the next app start, not immediately.
|
|
110
|
+
compression="zip",
|
|
111
|
+
encoding="utf-8",
|
|
112
|
+
enqueue=True, # Non-blocking, thread-safe.
|
|
113
|
+
)
|
|
114
|
+
_loguru_logger.info(f"Logging to file: {log_path}")
|
|
115
|
+
|
|
116
|
+
_logger_initialized = True
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def get_logger(**extra) -> Logger:
|
|
120
|
+
"""Return a logger bound with the given contextual key-value pairs."""
|
|
121
|
+
return _loguru_logger.bind(**extra)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
if __name__ == "__main__":
|
|
125
|
+
init_logger(level="TRACE")
|
|
126
|
+
logger = get_logger()
|
|
127
|
+
logger.trace("trace message\ntest new line")
|
|
128
|
+
logger.debug("debug message")
|
|
129
|
+
logger.info("info message")
|
|
130
|
+
logger.success("success message")
|
|
131
|
+
logger.warning("warning message")
|
|
132
|
+
logger.error("error message")
|
|
133
|
+
logger.critical("critical message")
|
|
134
|
+
logger.debug("message with accolades: {'name'} {{}} {{{{}}}}")
|
small_logger/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: small-logger
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A tiny Loguru wrapper for styled logging
|
|
5
|
+
Author: Seppe Van Bogaert
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Seppe Van Bogaert
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Requires-Python: >=3.8
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Dist: loguru>=0.7.0
|
|
32
|
+
Requires-Dist: platformdirs>=3.0
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# small-logger
|
|
36
|
+
|
|
37
|
+
A tiny, opinionated [Loguru](https://github.com/Delgan/loguru) wrapper that gives your Python apps a consistent, readable log format in two lines of code.
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
2026-05-11 14:32:01 | INFO | myapp:main:42 | Server started on port 8080
|
|
41
|
+
2026-05-11 14:32:01 | ERROR | myapp:handle_request:87 | Connection refused
|
|
42
|
+
| Traceback shown below
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
uv add small-logger
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
pip install small-logger
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from small_logger import init_logger, get_logger
|
|
59
|
+
|
|
60
|
+
init_logger() # call once at the start of your app
|
|
61
|
+
|
|
62
|
+
logger = get_logger()
|
|
63
|
+
logger.info("Server started")
|
|
64
|
+
logger.warning("Disk usage above 80%")
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## File logging
|
|
68
|
+
|
|
69
|
+
Pass your app's name to enable automatic file logging:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
init_logger(app_name="my-app")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Logs are written to the platform-appropriate directory:
|
|
76
|
+
|
|
77
|
+
| Platform | Path |
|
|
78
|
+
|----------|------|
|
|
79
|
+
| Windows | `%LOCALAPPDATA%\my-app\Logs\app.log` |
|
|
80
|
+
| macOS | `~/Library/Logs/my-app/app.log` |
|
|
81
|
+
| Linux | `~/.local/state/my-app/log/app.log` |
|
|
82
|
+
|
|
83
|
+
Files rotate daily at midnight, are kept for 7 days, and are compressed to `.zip` on rotation.
|
|
84
|
+
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
### `init_logger`
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
init_logger(
|
|
91
|
+
level="INFO", # TRACE | DEBUG | INFO | SUCCESS | WARNING | ERROR | CRITICAL
|
|
92
|
+
app_name=None, # enables file logging when provided
|
|
93
|
+
file=None, # override file logging on/off explicitly
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| Parameter | Default | Description |
|
|
98
|
+
|-----------|---------|-------------|
|
|
99
|
+
| `level` | `"INFO"` | Minimum level to log. |
|
|
100
|
+
| `app_name` | `None` | App name for the log file path. When set, file logging is enabled by default. |
|
|
101
|
+
| `file` | `None` | Explicitly enable (`True`) or disable (`False`) file logging, regardless of `app_name`. |
|
|
102
|
+
|
|
103
|
+
Call this **once** at startup. Subsequent calls are no-ops.
|
|
104
|
+
|
|
105
|
+
### `get_logger`
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
logger = get_logger()
|
|
109
|
+
logger = get_logger(request_id="abc123", user_id=42)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Returns a Loguru logger bound with optional key-value context. The context is included in every message emitted through that logger instance.
|
|
113
|
+
|
|
114
|
+
## Multiline messages
|
|
115
|
+
|
|
116
|
+
Continuation lines are automatically indented to align with the first line:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
logger.info("Query returned 3 rows:\nrow1\nrow2\nrow3")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
2026-05-11 14:32:01 | INFO | myapp:run:10 | Query returned 3 rows:
|
|
124
|
+
| row1
|
|
125
|
+
| row2
|
|
126
|
+
| row3
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
small_logger/__init__.py,sha256=nB0WBqgTC7on90iiqiwTwry0_RSd61LRZwKuWkTOmBg,88
|
|
2
|
+
small_logger/core.py,sha256=GttMKdYFuGNBoV4HGw1k_Jab-4hItABN7poV5a5a6uA,4232
|
|
3
|
+
small_logger/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
4
|
+
small_logger-0.1.0.dist-info/licenses/LICENSE,sha256=jgOQWWOyZiJFG8HDKr6rj03SBzi3ElAQDuoOPk5DUqw,1074
|
|
5
|
+
small_logger-0.1.0.dist-info/METADATA,sha256=vU-J31sHQArBaLtbhypRU7B9TaNSptFRBADNAONFWgY,4400
|
|
6
|
+
small_logger-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
small_logger-0.1.0.dist-info/top_level.txt,sha256=BWmRBTX0lyUBOTTeq6WVVntzQ29YUKlpwwpyOWDCBT4,13
|
|
8
|
+
small_logger-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Seppe Van Bogaert
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
small_logger
|