python-coloured-logger 0.1.1__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.
- python_coloured_logger-0.1.1/LICENSE +21 -0
- python_coloured_logger-0.1.1/PKG-INFO +198 -0
- python_coloured_logger-0.1.1/README.md +183 -0
- python_coloured_logger-0.1.1/pyproject.toml +25 -0
- python_coloured_logger-0.1.1/setup.cfg +4 -0
- python_coloured_logger-0.1.1/src/coloured_logger/__init__.py +3 -0
- python_coloured_logger-0.1.1/src/coloured_logger/formatter.py +119 -0
- python_coloured_logger-0.1.1/src/python_coloured_logger.egg-info/PKG-INFO +198 -0
- python_coloured_logger-0.1.1/src/python_coloured_logger.egg-info/SOURCES.txt +10 -0
- python_coloured_logger-0.1.1/src/python_coloured_logger.egg-info/dependency_links.txt +1 -0
- python_coloured_logger-0.1.1/src/python_coloured_logger.egg-info/top_level.txt +1 -0
- python_coloured_logger-0.1.1/tests/test_formatter.py +31 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Skulldorom
|
|
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,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-coloured-logger
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Simple coloured logging for Flask and FastAPI apps
|
|
5
|
+
Author: Skulldorom
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: logging,flask,fastapi,color
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# python-coloured-logger
|
|
17
|
+
|
|
18
|
+
A lightweight Python package that provides clean, coloured logging output for Flask, FastAPI, and any standard Python application.
|
|
19
|
+
|
|
20
|
+
It extends Python’s built-in `logging` module with readable coloured console output and simple setup helpers.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- Coloured console logging
|
|
27
|
+
- Works with standard Python `logging`
|
|
28
|
+
- Flask integration
|
|
29
|
+
- FastAPI / Uvicorn integration
|
|
30
|
+
- Simple setup API
|
|
31
|
+
- Lightweight with minimal configuration
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Install from PyPI:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install python-coloured-logger
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from coloured_logger import get_logger
|
|
49
|
+
|
|
50
|
+
logger = get_logger("my-app")
|
|
51
|
+
|
|
52
|
+
logger.debug("Debug message")
|
|
53
|
+
logger.info("Server started")
|
|
54
|
+
logger.success("Database connected")
|
|
55
|
+
logger.warning("High memory usage")
|
|
56
|
+
logger.error("Request failed")
|
|
57
|
+
logger.critical("System crash")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Example output:
|
|
61
|
+
|
|
62
|
+
```text
|
|
63
|
+
[2026-05-27 12:00:00] INFO Server started
|
|
64
|
+
[2026-05-27 12:00:01] SUCCESS Database connected
|
|
65
|
+
[2026-05-27 12:00:02] WARNING High memory usage
|
|
66
|
+
[2026-05-27 12:00:03] ERROR Request failed
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Flask Integration
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from flask import Flask
|
|
75
|
+
from coloured_logger import setup_logging
|
|
76
|
+
|
|
77
|
+
app = Flask(__name__)
|
|
78
|
+
|
|
79
|
+
setup_logging(logger_name="flask.app")
|
|
80
|
+
|
|
81
|
+
@app.route("/")
|
|
82
|
+
def index():
|
|
83
|
+
app.logger.info("Request received")
|
|
84
|
+
return "Hello World"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## FastAPI Integration
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from fastapi import FastAPI
|
|
93
|
+
from coloured_logger import setup_logging
|
|
94
|
+
|
|
95
|
+
app = FastAPI()
|
|
96
|
+
|
|
97
|
+
setup_logging(logger_name="uvicorn")
|
|
98
|
+
|
|
99
|
+
@app.get("/")
|
|
100
|
+
async def root():
|
|
101
|
+
return {"message": "Hello World"}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Standard Python Logging
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from coloured_logger import setup_logging
|
|
110
|
+
import logging
|
|
111
|
+
|
|
112
|
+
setup_logging()
|
|
113
|
+
|
|
114
|
+
logger = logging.getLogger("example")
|
|
115
|
+
|
|
116
|
+
logger.info("Using standard logging")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Environment Variables
|
|
122
|
+
|
|
123
|
+
### Enable or Disable Colours
|
|
124
|
+
|
|
125
|
+
By default, colours are enabled.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
COLOURED_LOGGER_COLOR=True
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
or for Flask-specific configuration:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
FLASK_LOG_COLOR=True
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Disable colours:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
COLOURED_LOGGER_COLOR=False
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### Custom Date Format
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
COLOURED_LOGGER_DATE_FORMAT=%Y-%m-%d %H:%M:%S
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
or:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
FLASK_LOG_DATE_FORMAT=%H:%M:%S
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Supported Log Levels
|
|
160
|
+
|
|
161
|
+
| Level | Description |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `debug` | Debug information |
|
|
164
|
+
| `info` | General information |
|
|
165
|
+
| `success` | Successful operations |
|
|
166
|
+
| `warning` | Warning messages |
|
|
167
|
+
| `error` | Error messages |
|
|
168
|
+
| `critical` | Critical failures |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
## Requirements
|
|
174
|
+
|
|
175
|
+
- Python 3.9+
|
|
176
|
+
- Flask (optional)
|
|
177
|
+
- FastAPI / Uvicorn (optional)
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
MIT License
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Contributing
|
|
188
|
+
|
|
189
|
+
Pull requests, issues, and suggestions are welcome.
|
|
190
|
+
|
|
191
|
+
If you find a bug or want to improve the package, feel free to open an issue or submit a PR.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Links
|
|
196
|
+
|
|
197
|
+
- PyPI: `https://pypi.org/project/python-coloured-logger/`
|
|
198
|
+
- GitHub: `https://github.com/Skulldorom/python-coloured-logger`
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# python-coloured-logger
|
|
2
|
+
|
|
3
|
+
A lightweight Python package that provides clean, coloured logging output for Flask, FastAPI, and any standard Python application.
|
|
4
|
+
|
|
5
|
+
It extends Python’s built-in `logging` module with readable coloured console output and simple setup helpers.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Coloured console logging
|
|
12
|
+
- Works with standard Python `logging`
|
|
13
|
+
- Flask integration
|
|
14
|
+
- FastAPI / Uvicorn integration
|
|
15
|
+
- Simple setup API
|
|
16
|
+
- Lightweight with minimal configuration
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Install from PyPI:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install python-coloured-logger
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from coloured_logger import get_logger
|
|
34
|
+
|
|
35
|
+
logger = get_logger("my-app")
|
|
36
|
+
|
|
37
|
+
logger.debug("Debug message")
|
|
38
|
+
logger.info("Server started")
|
|
39
|
+
logger.success("Database connected")
|
|
40
|
+
logger.warning("High memory usage")
|
|
41
|
+
logger.error("Request failed")
|
|
42
|
+
logger.critical("System crash")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Example output:
|
|
46
|
+
|
|
47
|
+
```text
|
|
48
|
+
[2026-05-27 12:00:00] INFO Server started
|
|
49
|
+
[2026-05-27 12:00:01] SUCCESS Database connected
|
|
50
|
+
[2026-05-27 12:00:02] WARNING High memory usage
|
|
51
|
+
[2026-05-27 12:00:03] ERROR Request failed
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Flask Integration
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from flask import Flask
|
|
60
|
+
from coloured_logger import setup_logging
|
|
61
|
+
|
|
62
|
+
app = Flask(__name__)
|
|
63
|
+
|
|
64
|
+
setup_logging(logger_name="flask.app")
|
|
65
|
+
|
|
66
|
+
@app.route("/")
|
|
67
|
+
def index():
|
|
68
|
+
app.logger.info("Request received")
|
|
69
|
+
return "Hello World"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## FastAPI Integration
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from fastapi import FastAPI
|
|
78
|
+
from coloured_logger import setup_logging
|
|
79
|
+
|
|
80
|
+
app = FastAPI()
|
|
81
|
+
|
|
82
|
+
setup_logging(logger_name="uvicorn")
|
|
83
|
+
|
|
84
|
+
@app.get("/")
|
|
85
|
+
async def root():
|
|
86
|
+
return {"message": "Hello World"}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Standard Python Logging
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from coloured_logger import setup_logging
|
|
95
|
+
import logging
|
|
96
|
+
|
|
97
|
+
setup_logging()
|
|
98
|
+
|
|
99
|
+
logger = logging.getLogger("example")
|
|
100
|
+
|
|
101
|
+
logger.info("Using standard logging")
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Environment Variables
|
|
107
|
+
|
|
108
|
+
### Enable or Disable Colours
|
|
109
|
+
|
|
110
|
+
By default, colours are enabled.
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
COLOURED_LOGGER_COLOR=True
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
or for Flask-specific configuration:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
FLASK_LOG_COLOR=True
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Disable colours:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
COLOURED_LOGGER_COLOR=False
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### Custom Date Format
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
COLOURED_LOGGER_DATE_FORMAT=%Y-%m-%d %H:%M:%S
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
or:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
FLASK_LOG_DATE_FORMAT=%H:%M:%S
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Supported Log Levels
|
|
145
|
+
|
|
146
|
+
| Level | Description |
|
|
147
|
+
|---|---|
|
|
148
|
+
| `debug` | Debug information |
|
|
149
|
+
| `info` | General information |
|
|
150
|
+
| `success` | Successful operations |
|
|
151
|
+
| `warning` | Warning messages |
|
|
152
|
+
| `error` | Error messages |
|
|
153
|
+
| `critical` | Critical failures |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
## Requirements
|
|
159
|
+
|
|
160
|
+
- Python 3.9+
|
|
161
|
+
- Flask (optional)
|
|
162
|
+
- FastAPI / Uvicorn (optional)
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT License
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Contributing
|
|
173
|
+
|
|
174
|
+
Pull requests, issues, and suggestions are welcome.
|
|
175
|
+
|
|
176
|
+
If you find a bug or want to improve the package, feel free to open an issue or submit a PR.
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Links
|
|
181
|
+
|
|
182
|
+
- PyPI: `https://pypi.org/project/python-coloured-logger/`
|
|
183
|
+
- GitHub: `https://github.com/Skulldorom/python-coloured-logger`
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "python-coloured-logger"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Simple coloured logging for Flask and FastAPI apps"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Skulldorom" }]
|
|
14
|
+
keywords = ["logging", "flask", "fastapi", "color"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[tool.setuptools]
|
|
22
|
+
package-dir = { "" = "src" }
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from typing import Optional, TextIO
|
|
4
|
+
|
|
5
|
+
SUCCESS_LEVEL = 25
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _env_bool(name: str, default: bool) -> bool:
|
|
9
|
+
value = os.getenv(name)
|
|
10
|
+
if value is None:
|
|
11
|
+
return default
|
|
12
|
+
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _resolve_use_color(use_color: Optional[bool]) -> bool:
|
|
16
|
+
if use_color is not None:
|
|
17
|
+
return use_color
|
|
18
|
+
if os.getenv("FLASK_LOG_COLOR") is not None:
|
|
19
|
+
return _env_bool("FLASK_LOG_COLOR", True)
|
|
20
|
+
return _env_bool("COLOURED_LOGGER_COLOR", True)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _resolve_datefmt(datefmt: Optional[str]) -> str:
|
|
24
|
+
if datefmt:
|
|
25
|
+
return datefmt
|
|
26
|
+
return os.getenv("FLASK_LOG_DATE_FORMAT") or os.getenv("COLOURED_LOGGER_DATE_FORMAT") or "%d/%b/%Y %H:%M:%S"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ColouredFormatter(logging.Formatter):
|
|
30
|
+
RESET = "\033[0m"
|
|
31
|
+
DEFAULT_COLORS = {
|
|
32
|
+
logging.DEBUG: "\033[36m",
|
|
33
|
+
logging.INFO: "\033[34m",
|
|
34
|
+
SUCCESS_LEVEL: "\033[32m",
|
|
35
|
+
logging.WARNING: "\033[33m",
|
|
36
|
+
logging.ERROR: "\033[31m",
|
|
37
|
+
logging.CRITICAL: "\033[35m",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
fmt: Optional[str] = None,
|
|
43
|
+
datefmt: Optional[str] = None,
|
|
44
|
+
use_color: Optional[bool] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
self.use_color = _resolve_use_color(use_color)
|
|
47
|
+
super().__init__(
|
|
48
|
+
fmt=fmt or "[%(asctime)s] [%(levelname)s] %(message)s",
|
|
49
|
+
datefmt=_resolve_datefmt(datefmt),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def format(self, record: logging.LogRecord) -> str:
|
|
53
|
+
message = super().format(record)
|
|
54
|
+
if not self.use_color:
|
|
55
|
+
return message
|
|
56
|
+
color = self.DEFAULT_COLORS.get(record.levelno)
|
|
57
|
+
if not color:
|
|
58
|
+
return message
|
|
59
|
+
return f"{color}{message}{self.RESET}"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _ensure_success_level() -> None:
|
|
63
|
+
if logging.getLevelName(SUCCESS_LEVEL) != "SUCCESS":
|
|
64
|
+
logging.addLevelName(SUCCESS_LEVEL, "SUCCESS")
|
|
65
|
+
|
|
66
|
+
if hasattr(logging.Logger, "success"):
|
|
67
|
+
return
|
|
68
|
+
|
|
69
|
+
def success(self: logging.Logger, msg: str, *args, **kwargs) -> None:
|
|
70
|
+
if self.isEnabledFor(SUCCESS_LEVEL):
|
|
71
|
+
self._log(SUCCESS_LEVEL, msg, args, **kwargs)
|
|
72
|
+
|
|
73
|
+
setattr(logging.Logger, "success", success)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def setup_logging(
|
|
77
|
+
logger_name: Optional[str] = None,
|
|
78
|
+
level: int = logging.INFO,
|
|
79
|
+
stream: Optional[TextIO] = None,
|
|
80
|
+
use_color: Optional[bool] = None,
|
|
81
|
+
datefmt: Optional[str] = None,
|
|
82
|
+
fmt: Optional[str] = None,
|
|
83
|
+
) -> logging.Logger:
|
|
84
|
+
_ensure_success_level()
|
|
85
|
+
logger = logging.getLogger(logger_name)
|
|
86
|
+
logger.setLevel(level)
|
|
87
|
+
|
|
88
|
+
managed_handler_exists = any(
|
|
89
|
+
isinstance(handler, logging.StreamHandler) and isinstance(handler.formatter, ColouredFormatter)
|
|
90
|
+
for handler in logger.handlers
|
|
91
|
+
)
|
|
92
|
+
if not managed_handler_exists:
|
|
93
|
+
handler = logging.StreamHandler(stream)
|
|
94
|
+
handler.setFormatter(ColouredFormatter(fmt=fmt, datefmt=datefmt, use_color=use_color))
|
|
95
|
+
logger.addHandler(handler)
|
|
96
|
+
|
|
97
|
+
return logger
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_logger(name: Optional[str] = None, **kwargs) -> logging.Logger:
|
|
101
|
+
return setup_logging(logger_name=name, **kwargs)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class log:
|
|
105
|
+
def __init__(self, *messages) -> None:
|
|
106
|
+
self.string = "".join(str(message) for message in messages)
|
|
107
|
+
self.logger = get_logger(__name__)
|
|
108
|
+
|
|
109
|
+
def success(self) -> None:
|
|
110
|
+
self.logger.success(self.string)
|
|
111
|
+
|
|
112
|
+
def info(self) -> None:
|
|
113
|
+
self.logger.info(self.string)
|
|
114
|
+
|
|
115
|
+
def warning(self) -> None:
|
|
116
|
+
self.logger.warning(self.string)
|
|
117
|
+
|
|
118
|
+
def error(self) -> None:
|
|
119
|
+
self.logger.error(self.string)
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-coloured-logger
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Simple coloured logging for Flask and FastAPI apps
|
|
5
|
+
Author: Skulldorom
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: logging,flask,fastapi,color
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# python-coloured-logger
|
|
17
|
+
|
|
18
|
+
A lightweight Python package that provides clean, coloured logging output for Flask, FastAPI, and any standard Python application.
|
|
19
|
+
|
|
20
|
+
It extends Python’s built-in `logging` module with readable coloured console output and simple setup helpers.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- Coloured console logging
|
|
27
|
+
- Works with standard Python `logging`
|
|
28
|
+
- Flask integration
|
|
29
|
+
- FastAPI / Uvicorn integration
|
|
30
|
+
- Simple setup API
|
|
31
|
+
- Lightweight with minimal configuration
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Install from PyPI:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install python-coloured-logger
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from coloured_logger import get_logger
|
|
49
|
+
|
|
50
|
+
logger = get_logger("my-app")
|
|
51
|
+
|
|
52
|
+
logger.debug("Debug message")
|
|
53
|
+
logger.info("Server started")
|
|
54
|
+
logger.success("Database connected")
|
|
55
|
+
logger.warning("High memory usage")
|
|
56
|
+
logger.error("Request failed")
|
|
57
|
+
logger.critical("System crash")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Example output:
|
|
61
|
+
|
|
62
|
+
```text
|
|
63
|
+
[2026-05-27 12:00:00] INFO Server started
|
|
64
|
+
[2026-05-27 12:00:01] SUCCESS Database connected
|
|
65
|
+
[2026-05-27 12:00:02] WARNING High memory usage
|
|
66
|
+
[2026-05-27 12:00:03] ERROR Request failed
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Flask Integration
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from flask import Flask
|
|
75
|
+
from coloured_logger import setup_logging
|
|
76
|
+
|
|
77
|
+
app = Flask(__name__)
|
|
78
|
+
|
|
79
|
+
setup_logging(logger_name="flask.app")
|
|
80
|
+
|
|
81
|
+
@app.route("/")
|
|
82
|
+
def index():
|
|
83
|
+
app.logger.info("Request received")
|
|
84
|
+
return "Hello World"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## FastAPI Integration
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from fastapi import FastAPI
|
|
93
|
+
from coloured_logger import setup_logging
|
|
94
|
+
|
|
95
|
+
app = FastAPI()
|
|
96
|
+
|
|
97
|
+
setup_logging(logger_name="uvicorn")
|
|
98
|
+
|
|
99
|
+
@app.get("/")
|
|
100
|
+
async def root():
|
|
101
|
+
return {"message": "Hello World"}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Standard Python Logging
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from coloured_logger import setup_logging
|
|
110
|
+
import logging
|
|
111
|
+
|
|
112
|
+
setup_logging()
|
|
113
|
+
|
|
114
|
+
logger = logging.getLogger("example")
|
|
115
|
+
|
|
116
|
+
logger.info("Using standard logging")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Environment Variables
|
|
122
|
+
|
|
123
|
+
### Enable or Disable Colours
|
|
124
|
+
|
|
125
|
+
By default, colours are enabled.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
COLOURED_LOGGER_COLOR=True
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
or for Flask-specific configuration:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
FLASK_LOG_COLOR=True
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Disable colours:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
COLOURED_LOGGER_COLOR=False
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### Custom Date Format
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
COLOURED_LOGGER_DATE_FORMAT=%Y-%m-%d %H:%M:%S
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
or:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
FLASK_LOG_DATE_FORMAT=%H:%M:%S
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Supported Log Levels
|
|
160
|
+
|
|
161
|
+
| Level | Description |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `debug` | Debug information |
|
|
164
|
+
| `info` | General information |
|
|
165
|
+
| `success` | Successful operations |
|
|
166
|
+
| `warning` | Warning messages |
|
|
167
|
+
| `error` | Error messages |
|
|
168
|
+
| `critical` | Critical failures |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
## Requirements
|
|
174
|
+
|
|
175
|
+
- Python 3.9+
|
|
176
|
+
- Flask (optional)
|
|
177
|
+
- FastAPI / Uvicorn (optional)
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
MIT License
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Contributing
|
|
188
|
+
|
|
189
|
+
Pull requests, issues, and suggestions are welcome.
|
|
190
|
+
|
|
191
|
+
If you find a bug or want to improve the package, feel free to open an issue or submit a PR.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Links
|
|
196
|
+
|
|
197
|
+
- PyPI: `https://pypi.org/project/python-coloured-logger/`
|
|
198
|
+
- GitHub: `https://github.com/Skulldorom/python-coloured-logger`
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/coloured_logger/__init__.py
|
|
5
|
+
src/coloured_logger/formatter.py
|
|
6
|
+
src/python_coloured_logger.egg-info/PKG-INFO
|
|
7
|
+
src/python_coloured_logger.egg-info/SOURCES.txt
|
|
8
|
+
src/python_coloured_logger.egg-info/dependency_links.txt
|
|
9
|
+
src/python_coloured_logger.egg-info/top_level.txt
|
|
10
|
+
tests/test_formatter.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
coloured_logger
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import logging
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
from coloured_logger import ColouredFormatter, setup_logging
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FormatterTests(unittest.TestCase):
|
|
9
|
+
def test_formatter_without_color(self):
|
|
10
|
+
formatter = ColouredFormatter(use_color=False, datefmt="%Y")
|
|
11
|
+
record = logging.LogRecord("test", logging.INFO, __file__, 1, "hello", (), None)
|
|
12
|
+
output = formatter.format(record)
|
|
13
|
+
self.assertIn("[INFO] hello", output)
|
|
14
|
+
self.assertNotIn("\033[", output)
|
|
15
|
+
|
|
16
|
+
def test_logger_adds_success_level_output(self):
|
|
17
|
+
stream = io.StringIO()
|
|
18
|
+
logger = setup_logging(
|
|
19
|
+
logger_name="coloured_logger_test",
|
|
20
|
+
level=logging.DEBUG,
|
|
21
|
+
stream=stream,
|
|
22
|
+
use_color=False,
|
|
23
|
+
datefmt="%Y",
|
|
24
|
+
)
|
|
25
|
+
logger.success("done")
|
|
26
|
+
value = stream.getvalue()
|
|
27
|
+
self.assertIn("[SUCCESS] done", value)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
unittest.main()
|