pythonLogs 5.0.3__tar.gz → 6.0.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.
@@ -0,0 +1,160 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ .idea/
157
+
158
+ # Custom
159
+ /junit.xml
160
+ *.prof
@@ -0,0 +1,422 @@
1
+ Metadata-Version: 2.4
2
+ Name: pythonLogs
3
+ Version: 6.0.1
4
+ Summary: High-performance Python logging library with file rotation and optimized caching for better performance
5
+ Project-URL: Homepage, https://pypi.org/project/pythonLogs
6
+ Project-URL: Repository, https://github.com/ddc/pythonLogs
7
+ Author-email: Daniel Costa <danieldcsta@gmail.com>
8
+ Maintainer: Daniel Costa
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: log,log-utils,logger,logging,logutils,python,python-3,python3,pythonLogs
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Environment :: Other Environment
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Natural Language :: English
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Requires-Python: >=3.12
24
+ Requires-Dist: pydantic-settings>=2.11.0
25
+ Provides-Extra: test
26
+ Requires-Dist: poethepoet>=0.40.0; extra == 'test'
27
+ Requires-Dist: psutil>=7.2.2; extra == 'test'
28
+ Requires-Dist: pytest-cov>=7.0.0; extra == 'test'
29
+ Requires-Dist: pytest>=9.0.2; extra == 'test'
30
+ Description-Content-Type: text/markdown
31
+
32
+ <h1 align="center">
33
+ <img src="https://raw.githubusercontent.com/ddc/pythonLogs/main/assets/pythonLogs-icon.svg" alt="pythonLogs" width="150">
34
+ <br>
35
+ pythonLogs
36
+ </h1>
37
+
38
+ <p align="center">
39
+ <a href="https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ"><img src="https://img.shields.io/badge/Donate-PayPal-brightgreen.svg?style=plastic" alt="Donate"/></a>
40
+ <a href="https://github.com/sponsors/ddc"><img src="https://img.shields.io/static/v1?style=plastic&label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4" alt="Sponsor"/></a>
41
+ <br>
42
+ <a href="https://github.com/astral-sh/uv"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json?style=plastic" alt="uv"/></a>
43
+ <a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json?style=plastic" alt="Ruff"/></a>
44
+ <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=plastic" alt="Code style: black"/></a>
45
+ <br>
46
+ <a href="https://www.python.org/downloads"><img src="https://img.shields.io/pypi/pyversions/pythonLogs.svg?style=plastic&logo=python&cacheSeconds=3600" alt="Python"/></a>
47
+ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg?style=plastic" alt="License: MIT"/></a>
48
+ <a href="https://pepy.tech/projects/pythonLogs"><img src="https://static.pepy.tech/badge/pythonLogs?style=plastic" alt="PyPI Downloads"/></a>
49
+ <a href="https://pypi.python.org/pypi/pythonLogs"><img src="https://img.shields.io/pypi/v/pythonLogs.svg?style=plastic&logo=python&cacheSeconds=3600" alt="PyPi"/></a>
50
+ <br>
51
+ <a href="https://github.com/ddc/pythonLogs/issues"><img src="https://img.shields.io/github/issues/ddc/pythonLogs?style=plastic" alt="issues"/></a>
52
+ <a href="https://codecov.io/gh/ddc/pythonLogs"><img src="https://codecov.io/gh/ddc/pythonLogs/graph/badge.svg?token=XWB53034GI&style=plastic" alt="codecov"/></a>
53
+ <a href="https://sonarcloud.io/dashboard?id=ddc_pythonLogs"><img src="https://sonarcloud.io/api/project_badges/measure?project=ddc_pythonLogs&metric=alert_status&style=plastic" alt="Quality Gate Status"/></a>
54
+ <a href="https://github.com/ddc/pythonLogs/actions/workflows/workflow.yml"><img src="https://github.com/ddc/pythonLogs/actions/workflows/workflow.yml/badge.svg?style=plastic" alt="CI/CD Pipeline"/></a>
55
+ <a href="https://actions-badge.atrox.dev/ddc/pythonLogs/goto?ref=main"><img src="https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/pythonLogs/badge?ref=main&label=build&logo=none&style=plastic" alt="Build Status"/></a>
56
+ </p>
57
+
58
+ <p align="center">High-performance Python logging library with file rotation and optimized caching for better performance</p>
59
+
60
+
61
+ # Table of Contents
62
+
63
+ - [Features](#features)
64
+ - [Installation](#installation)
65
+ - [Logger Types](#logger-types)
66
+ - [Basic Logger](#basic-logger)
67
+ - [Size Rotating Logger](#size-rotating-logger)
68
+ - [Timed Rotating Logger](#timed-rotating-logger)
69
+ - [Context Manager Support](#context-manager-support)
70
+ - [Using With Multiple Log Levels and Files](#using-with-multiple-log-levels-and-files)
71
+ - [Environment Variables](#env-variables-optional)
72
+ - [Settings Cache Management](#settings-cache-management)
73
+ - [Flexible Configuration Options](#flexible-configuration-options)
74
+ - [Development](#development)
75
+ - [Create DEV Environment, Running Tests and Building Wheel](#create-dev-environment-running-tests-and-building-wheel)
76
+ - [Optionals](#optionals)
77
+ - [License](#license)
78
+ - [Support](#support)
79
+
80
+
81
+
82
+ # Features
83
+
84
+ ✨ **Factory Pattern** - Easy logger creation with centralized configuration
85
+ 🚀 **High Performance** - Optimized caching for 90%+ performance improvements
86
+ 🔄 **File Rotation** - Automatic rotation by size or time with compression
87
+ 🎯 **Type Safety** - Enum-based configuration with IDE support
88
+ ⚙️ **Flexible Configuration** - Environment variables, direct parameters, or defaults
89
+ 📍 **Location Tracking** - Optional filename and line number in logs
90
+ 🌍 **Timezone Support** - Full timezone handling including `localtime` and `UTC`
91
+ 💾 **Memory Efficient** - Logger registry and settings caching
92
+ 🔒 **Context Manager Support** - Automatic resource cleanup and exception safety
93
+ 🧵 **Thread Safe** - Concurrent access protection for all operations
94
+ 🔧 **Resource Management** - Automatic handler cleanup and memory leak prevention
95
+
96
+
97
+ # Installation
98
+
99
+ ```shell
100
+ pip install pythonLogs
101
+ ```
102
+
103
+
104
+ # Logger Types
105
+
106
+ > **Tip:** All logger types support both string values (e.g., `level="debug"`) and type-safe enums (e.g., `level=LogLevel.DEBUG`). \
107
+ > See [Flexible Configuration Options](#flexible-configuration-options) for all available enums.
108
+
109
+ ## Basic Logger
110
+
111
+ Console-only logging without file output. Perfect for development and simple applications.
112
+
113
+ ### Usage
114
+
115
+ ```python
116
+ from pythonLogs import BasicLog
117
+
118
+ logger = BasicLog(
119
+ name="my_app",
120
+ level="debug", # "debug", "info", "warning", "error", "critical"
121
+ timezone="America/Sao_Paulo",
122
+ showlocation=False
123
+ )
124
+ logger.warning("This is a warning example")
125
+ ```
126
+
127
+ ### Example Output
128
+
129
+ `[2024-10-08T19:08:56.918-0300]:[WARNING]:[my_app]:This is a warning example`
130
+
131
+
132
+
133
+
134
+
135
+ ## Size Rotating Logger
136
+
137
+ File-based logging with automatic rotation when files reach a specified size. Rotated files are compressed as `.gz`.
138
+
139
+ - **Rotation**: Based on file size (`maxmbytes` parameter)
140
+ - **Naming**: Rotated logs have sequence numbers: `app.log_1.gz`, `app.log_2.gz`
141
+ - **Cleanup**: Old logs deleted based on `daystokeep` (default: 30 days)
142
+
143
+ ### Usage
144
+
145
+ ```python
146
+ from pythonLogs import SizeRotatingLog
147
+
148
+ logger = SizeRotatingLog(
149
+ name="my_app",
150
+ level="debug", # "debug", "info", "warning", "error", "critical"
151
+ directory="/app/logs",
152
+ filenames=["main.log", "app1.log"],
153
+ maxmbytes=5,
154
+ daystokeep=7,
155
+ timezone="America/Chicago",
156
+ streamhandler=True,
157
+ showlocation=False
158
+ )
159
+ logger.warning("This is a warning example")
160
+ ```
161
+
162
+ ### Example Output
163
+
164
+ `[2024-10-08T19:08:56.918-0500]:[WARNING]:[my_app]:This is a warning example`
165
+
166
+
167
+
168
+
169
+
170
+ ## Timed Rotating Logger
171
+
172
+ File-based logging with automatic rotation based on time intervals. Rotated files are compressed as `.gz`.
173
+
174
+ - **Rotation**: Based on time (`when` parameter, defaults to `midnight`)
175
+ - **Naming**: Rotated logs have date suffix: `app_20240816.log.gz`
176
+ - **Cleanup**: Old logs deleted based on `daystokeep` (default: 30 days)
177
+ - **Supported Intervals**: `midnight`, `hourly`, `daily`, `W0-W6` (weekdays, 0=Monday)
178
+
179
+ ### Usage
180
+
181
+ ```python
182
+ from pythonLogs import TimedRotatingLog
183
+
184
+ logger = TimedRotatingLog(
185
+ name="my_app",
186
+ level="debug", # "debug", "info", "warning", "error", "critical"
187
+ directory="/app/logs",
188
+ filenames=["main.log", "app2.log"],
189
+ when="midnight", # "midnight", "H", "D", "W0"-"W6"
190
+ daystokeep=7,
191
+ timezone="UTC",
192
+ streamhandler=True,
193
+ showlocation=False
194
+ )
195
+ logger.warning("This is a warning example")
196
+ ```
197
+
198
+ ### Example Output
199
+
200
+ `[2024-10-08T19:08:56.918-0000]:[WARNING]:[my_app]:This is a warning example`
201
+
202
+
203
+
204
+
205
+
206
+ # Context Manager Support
207
+
208
+ All logger types support context managers for automatic resource cleanup and exception safety.
209
+
210
+ ## Usage Examples
211
+
212
+ ```python
213
+ from pythonLogs import LogLevel
214
+ from pythonLogs.basic_log import BasicLog
215
+ from pythonLogs.size_rotating import SizeRotatingLog
216
+ from pythonLogs.timed_rotating import TimedRotatingLog
217
+
218
+ # Automatic cleanup with context managers
219
+ with BasicLog(name="app", level=LogLevel.INFO) as logger:
220
+ logger.info("This is automatically cleaned up")
221
+ # Handlers are automatically closed on exit
222
+
223
+ with SizeRotatingLog(name="app", directory="/logs", filenames=["app.log"]) as logger:
224
+ logger.info("File handlers cleaned up automatically")
225
+ # File handlers closed and resources freed
226
+
227
+ # Exception safety - cleanup happens even if exceptions occur
228
+ try:
229
+ with TimedRotatingLog(name="app", directory="/logs") as logger:
230
+ logger.error("Error occurred")
231
+ raise ValueError("Something went wrong")
232
+ except ValueError:
233
+ pass # Logger was still cleaned up properly
234
+ ```
235
+
236
+
237
+
238
+
239
+
240
+ # Using With Multiple Log Levels and Files
241
+
242
+ ```python
243
+ from pythonLogs import SizeRotatingLog, TimedRotatingLog, LogLevel, RotateWhen
244
+
245
+ # Application logger
246
+ app_logger = SizeRotatingLog(
247
+ name="production_app",
248
+ directory="/var/log/myapp",
249
+ filenames=["app.log"],
250
+ maxmbytes=50, # 50MB files
251
+ daystokeep=30, # Keep 30 days
252
+ level=LogLevel.INFO,
253
+ streamhandler=True, # Also log to console
254
+ showlocation=True, # Show file:function:line
255
+ timezone="UTC"
256
+ )
257
+
258
+ # Error logger with longer retention
259
+ error_logger = SizeRotatingLog(
260
+ name="production_errors",
261
+ directory="/var/log/myapp",
262
+ filenames=["errors.log"],
263
+ maxmbytes=10,
264
+ daystokeep=90, # Keep errors longer
265
+ level=LogLevel.ERROR,
266
+ streamhandler=False
267
+ )
268
+
269
+ # Audit logger with daily rotation
270
+ audit_logger = TimedRotatingLog(
271
+ name="audit_log",
272
+ directory="/var/log/myapp",
273
+ filenames=["audit.log"],
274
+ when=RotateWhen.MIDNIGHT,
275
+ level=LogLevel.INFO
276
+ )
277
+
278
+ # Use the loggers
279
+ app_logger.info("Application started")
280
+ error_logger.error("Database connection failed")
281
+ audit_logger.info("User admin logged in")
282
+ ```
283
+
284
+
285
+
286
+
287
+ # Env Variables (Optional)
288
+
289
+ The .env variables file can be used by leaving all options blank when calling the class.\
290
+ If not specified inside the .env file, it will use the default value.\
291
+ This is a good approach for production environments, since options can be changed easily.
292
+ ```python
293
+ from pythonLogs import TimedRotatingLog
294
+ log = TimedRotatingLog()
295
+ ```
296
+
297
+ ```
298
+ LOG_LEVEL=DEBUG
299
+ LOG_TIMEZONE=UTC
300
+ LOG_ENCODING=UTF-8
301
+ LOG_APPNAME=app
302
+ LOG_FILENAME=app.log
303
+ LOG_DIRECTORY=/app/logs
304
+ LOG_DAYS_TO_KEEP=30
305
+ LOG_DATE_FORMAT=%Y-%m-%dT%H:%M:%S
306
+ LOG_STREAM_HANDLER=True
307
+ LOG_SHOW_LOCATION=False
308
+ LOG_MAX_LOGGERS=50
309
+ LOG_LOGGER_TTL_SECONDS=1800
310
+
311
+ # SizeRotatingLog
312
+ LOG_MAX_FILE_SIZE_MB=10
313
+
314
+ # TimedRotatingLog
315
+ LOG_ROTATE_WHEN=midnight
316
+ LOG_ROTATE_AT_UTC=True
317
+ LOG_ROTATE_FILE_SUFIX="%Y%m%d"
318
+ ```
319
+
320
+ ## Settings Cache Management
321
+
322
+ Use `get_log_settings()` to inspect current configuration and `clear_settings_cache()` to reload configuration from environment variables:
323
+
324
+ ```python
325
+ from pythonLogs import get_log_settings, clear_settings_cache
326
+
327
+ # Inspect current settings
328
+ settings = get_log_settings()
329
+ print(settings.level) # Current log level
330
+ print(settings.timezone) # Current timezone
331
+
332
+ # Clear cache and reload .env on next access (default)
333
+ clear_settings_cache()
334
+
335
+ # Clear cache but keep current .env values
336
+ clear_settings_cache(reload_env=False)
337
+ ```
338
+
339
+
340
+
341
+
342
+
343
+
344
+ # Flexible Configuration Options
345
+
346
+ You can use either enums (for type safety) or strings (for simplicity):
347
+
348
+ ```python
349
+ from pythonLogs import LogLevel, RotateWhen
350
+
351
+ # Option 1: Type-safe enums (recommended)
352
+ LogLevel.DEBUG # "DEBUG"
353
+ LogLevel.INFO # "INFO"
354
+ LogLevel.WARNING # "WARNING"
355
+ LogLevel.ERROR # "ERROR"
356
+ LogLevel.CRITICAL # "CRITICAL"
357
+
358
+ # Option 2: String values (case-insensitive)
359
+ "debug" # Same as LogLevel.DEBUG
360
+ "info" # Same as LogLevel.INFO
361
+ "warning" # Same as LogLevel.WARNING
362
+ "warn" # Same as LogLevel.WARN (alias)
363
+ "error" # Same as LogLevel.ERROR
364
+ "critical" # Same as LogLevel.CRITICAL
365
+ "crit" # Same as LogLevel.CRIT (alias)
366
+ # Also supports: "DEBUG", "Info", "Warning", etc.
367
+
368
+ # RotateWhen values
369
+ RotateWhen.MIDNIGHT # "midnight"
370
+ RotateWhen.HOURLY # "H"
371
+ RotateWhen.DAILY # "D"
372
+ RotateWhen.MONDAY # "W0"
373
+ # ... through SUNDAY # "W6"
374
+ # String equivalents: "midnight", "H", "D", "W0"-"W6"
375
+ ```
376
+
377
+
378
+
379
+
380
+
381
+ # Development
382
+
383
+ Must have [UV](https://uv.run/docs/getting-started/installation),
384
+ [Black](https://black.readthedocs.io/en/stable/getting_started.html),
385
+ [Ruff](https://docs.astral.sh/ruff/installation/), and
386
+ [Poe the Poet](https://poethepoet.naber.dev/installation) installed.
387
+
388
+ ## Create DEV Environment, Running Tests and Building Wheel
389
+
390
+ ```shell
391
+ uv sync --all-extras
392
+ poe linter
393
+ poe test
394
+ poe build
395
+ ```
396
+
397
+ ## Optionals
398
+
399
+ ### Create a cprofile.prof file from unit tests
400
+ ```shell
401
+ poe profile
402
+ ```
403
+
404
+
405
+
406
+
407
+
408
+ # License
409
+
410
+ Released under the [MIT License](LICENSE)
411
+
412
+
413
+
414
+
415
+
416
+ # Support
417
+
418
+ If you find this project helpful, consider supporting development:
419
+
420
+ - [GitHub Sponsor](https://github.com/sponsors/ddc)
421
+ - [ko-fi](https://ko-fi.com/ddcsta)
422
+ - [PayPal](https://www.paypal.com/ncp/payment/6G9Z78QHUD4RJ)