|
1 | 1 | # Logguard |
2 | 2 |
|
3 | | -[](https://www.python.org/) |
4 | 3 | [](https://pypi.org/project/py-logguard/) |
5 | 4 | [](https://pypi.org/project/py-logguard/) |
6 | | - |
7 | | - |
8 | | -[](https://raw.githubusercontent.com/DMsuDev/logguard/main/LICENSE.rst) |
| 5 | +[](https://codecov.io/gh/DMsuDev/logguard) |
9 | 6 |
|
10 | 7 | Logguard is a lightweight logging and assertion library designed to make it easy to capture rich context and structured logs in Python applications. It provides a simple API for logging with automatic source capture, flexible configuration, and a semantic exception hierarchy. |
11 | 8 |
|
@@ -290,12 +287,34 @@ AppLogger.setup( |
290 | 287 | ) |
291 | 288 | ``` |
292 | 289 |
|
| 290 | +**`HandlerType` enum**: use instead of plain strings to select which handlers an operation targets: |
| 291 | + |
| 292 | +| Member | Targets | |
| 293 | +| --------------------- | ----------------------------- | |
| 294 | +| `HandlerType.ALL` | Every handler (default) | |
| 295 | +| `HandlerType.CONSOLE` | Console / RichHandler only | |
| 296 | +| `HandlerType.FILE` | RotatingFileHandler only | |
| 297 | +| `HandlerType.JSON` | JSON RotatingFileHandler only | |
| 298 | + |
293 | 299 | **Methods:** |
294 | 300 |
|
295 | | -- `get_logger(name: str | None = None)` - Get or create a logger instance |
296 | | -- `set_level(level: str, handler_type: str = "all")` - Change log level dynamically |
297 | | -- `silence_noisy_libraries(modules: list[str] | None = None)` - Suppress third-party logs |
298 | | -- `reset()` - Reset configuration (useful for testing) |
| 301 | +- `get_logger(name: str | None = None)`: Get or create a logger instance |
| 302 | +- `set_level(level, handler_type: HandlerType | str = HandlerType.ALL)`: Change log level on selected handlers at runtime |
| 303 | +- `set_format(format_string, handler_type: HandlerType | str = HandlerType.ALL, datefmt=None)`: Change the format string on selected handlers; RichHandler and JsonFormatter instances are skipped automatically |
| 304 | +- `silence_noisy_libraries(modules: list[str] | None = None)`: Suppress third-party logs |
| 305 | +- `reset()`: Reset configuration (useful for testing) |
| 306 | + |
| 307 | +```python |
| 308 | +from logguard import AppLogger, HandlerType |
| 309 | + |
| 310 | +AppLogger.setup(log_file="logs/app.log", console_level="INFO") |
| 311 | + |
| 312 | +# Drop file handler to DEBUG without touching the console |
| 313 | +AppLogger.set_level("DEBUG", HandlerType.FILE) |
| 314 | + |
| 315 | +# Apply a compact format to file logs only |
| 316 | +AppLogger.set_format("%(asctime)s | %(levelname)s | %(message)s", HandlerType.FILE) |
| 317 | +``` |
299 | 318 |
|
300 | 319 | </details> |
301 | 320 |
|
|
0 commit comments